Re: conditional expression

Hi Debora,

You cannot do that with XML Schema alone, but you can do that with XML 
Schema with embedded Schematron rules. You can find below a working example:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified">
   <xs:element name="test">
     <xs:complexType>
       <xs:sequence>
         <xs:element ref="special-tag"/>
         <xs:element ref="host" minOccurs="0"/>
         <xs:element ref="port" minOccurs="0"/>
       </xs:sequence>
     </xs:complexType>
   </xs:element>
   <xs:element name="special-tag">
     <xs:annotation>
       <xs:appinfo>
         <pattern xmlns="http://www.ascc.net/xml/schematron" 
name="testSpecialTag">
           <rule context="special-tag[@value='OUR']">
             <assert test="not(../host) and not(../port)">host and post 
should not be present because
               value is 'OUR'</assert>
           </rule>
           <rule context="special-tag[@value='TCP']">
             <assert test="../host and ../port">host and post should be 
present because value is
               'TCP'</assert>
           </rule>
         </pattern>
       </xs:appinfo>
     </xs:annotation>
     <xs:complexType>
       <xs:attribute name="type" use="required" type="xs:NCName"/>
       <xs:attribute name="value" use="required">
         <xs:simpleType>
           <xs:restriction base="xs:NCName">
             <xs:enumeration value="TCP"/>
             <xs:enumeration value="OUR"/>
           </xs:restriction>
         </xs:simpleType>
       </xs:attribute>
     </xs:complexType>
   </xs:element>
   <xs:element name="host">
     <xs:complexType>
       <xs:attribute name="type" use="required" type="xs:NCName"/>
       <xs:attribute name="value" use="required" type="xs:NMTOKEN"/>
     </xs:complexType>
   </xs:element>
   <xs:element name="port">
     <xs:complexType>
       <xs:attribute name="type" use="required" type="xs:NCName"/>
       <xs:attribute name="value" use="required" type="xs:integer"/>
     </xs:complexType>
   </xs:element>
</xs:schema>


On an invalid instance document like
<test>
     <special-tag type="string" value="OUR"/>
     <host type="string" value="192.168.72.26"/>
     <port type="int" value="10202"/>
</test>

you will get an error

SystemID: C:\george\workspace\oXygen\samples\test.xml
Location: 2:0
Description: host and post should not be present because value is 'OUR' 
(not(../host) and not(../port))

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


Debora Vanni wrote:
> Hi all!
> I have this problem on validation with xsd.
> The xml file has a particular tag: when his value is "TCP" the xml file
> must have other two tags, when his value is "OUR" there should be no
> other tags.
> So the xml should be either
> 
> <special-tag  type="string" value="TCP"/>
> <host type="string" value="192.168.72.26"/>	
> <port type="int" value="10202"/>
> 
> or
> <special-tag  type="string" value="OUR"/>
> 
> Is there some way to do this with xsd?
> Thank you very much for your help!
> Debora
> 
> 
> 
> 

Received on Wednesday, 26 July 2006 13:38:54 UTC