W3C home > Mailing lists > Public > xmlschema-dev@w3.org > October 2002

Re: conditional element content?

From: Eddie Robertsson <erobertsson@allette.com.au>
Date: Fri, 01 Nov 2002 10:02:32 +1100
Message-ID: <3DC1B688.9060607@allette.com.au>
To: Vaios Papanagnou <v.papanagnou@ucl.ac.uk>
Cc: xmlschema-dev <xmlschema-dev@w3.org>

Hi Vaios,

>Would it be possible to specify that an element's content can change
>conditionally, in relation with an attribute's value?
Normally this is a constraint that can't be done in W3C XML Schema. 
RELAX-NG has more support for this and Schematron is the leading schema 
language for these constraints. An alternative is to embedd Schematron 
rules in your W3C XML Schema [1].
However, in the particular case you have described there is one solution 
available in W3C XML Schema that you possibly can use. This does require 
that instead of using you're own type attribute you use the predefined 
xsi:type attribute where you specify the name of the type you want the 
element to use. Example follows:

><element name="test" type="true">
><element name="test" type="false">
So, here you have two different types:

1) Only a "h1" element and a "name" attribute
2) Both "h1" and "h2" elements and a "name" attribute

<xs:complexType name="Type1">
      <xs:element name="h1" type="xs:string">
   <xs:attribute name="name" type="xs:string" use="required"/>

<xs:complexType name="Type2">
      <xs:extension base="Type1">
            <xs:element name="h2" type="xs:string">

Now, you define the "element" element in the schema as having Type1

<xs:element name="element" type="Type1"/>

In the instance document you can now control which type the "element" 
element can have by specifying the xsi:type attribute:

<!-- You don't really need the xsi:type attribute on this since it 
should have Type1 but it's there for clarity -->
<element name="test" xsi:type="Type1">

<element name="test" xsi:type="Type2">

This would be invalid:

<element name="test" xsi:type="Type2">

You can read more about this at [2].


[1] http://www.topologi.com/public/Schtrn_XSD/Paper.html
[2] http://www.w3.org/TR/xmlschema-0/#UseDerivInInstDocs
Received on Thursday, 31 October 2002 18:04:30 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 14:55:58 UTC