Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: conditional expression

From: George Cristian Bina <george@---------.--->
To: Debora Vanni <debora.vanni@------.--->
Date: 7/26/2006 7:42:00 PM
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
www.---.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
> 
> 
> 
> 

From Philippe.Poulard@s... Wed Jul 26 13:48:48 2006
Received: from maggie.w3.org ([193.51.208.68])
	by frink.w3.org with esmtp


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent