W3C home > Mailing lists > Public > xmlschema-dev@w3.org > July 2006

Re: conditional expression

From: George Cristian Bina <george@oxygenxml.com>
Date: Wed, 26 Jul 2006 16:41:40 +0300
Message-ID: <44C77114.6030209@oxygenxml.com>
To: Debora Vanni <debora.vanni@tvblob.com>
Cc: xmlschema-dev@w3.org

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" 
   <xs:element name="test">
         <xs:element ref="special-tag"/>
         <xs:element ref="host" minOccurs="0"/>
         <xs:element ref="port" minOccurs="0"/>
   <xs:element name="special-tag">
         <pattern xmlns="http://www.ascc.net/xml/schematron" 
           <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 context="special-tag[@value='TCP']">
             <assert test="../host and ../port">host and post should be 
present because value is
       <xs:attribute name="type" use="required" type="xs:NCName"/>
       <xs:attribute name="value" use="required">
           <xs:restriction base="xs:NCName">
             <xs:enumeration value="TCP"/>
             <xs:enumeration value="OUR"/>
   <xs:element name="host">
       <xs:attribute name="type" use="required" type="xs:NCName"/>
       <xs:attribute name="value" use="required" type="xs:NMTOKEN"/>
   <xs:element name="port">
       <xs:attribute name="type" use="required" type="xs:NCName"/>
       <xs:attribute name="value" use="required" type="xs:integer"/>

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

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 Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger

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=""/>	
> <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

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 14:56:10 UTC