W3C home > Mailing lists > Public > xmlschema-dev@w3.org > December 2008

Re: Conditional Schema Processing

From: Andrew Welch <andrew.j.welch@gmail.com>
Date: Fri, 5 Dec 2008 17:44:44 +0000
Message-ID: <74a894af0812050944k5c0b3460iaf6c7d750e070b23@mail.gmail.com>
To: "Zac Harvey" <zachary.harvey@gmail.com>
Cc: xmlschema-dev@w3.org

2008/12/5 Zac Harvey <zachary.harvey@gmail.com>:
> This may be bad design, so if it is, then I guess what I'm looking for is an
> elegant alternative solution. However I don't believe the following to be a
> product of poor design, and so the following question should apply:
>
> I want a particular element, <widget> to force the user to define a foo
> attribute, which will be an enum type of three potential values: me, you or
> us. For example:
>
> <widget foo="me" />
> <widget foo="you" />
> <widget foo="us" />
>
> Would all be legal <widget> instantiations. And so I have the following so
> far in my XSD:
>
> <xs:element name="widget" minOccurs="1" maxOccurs="unbounded">
>     <xs:complexType>
>         <xs:attribute name="foo" type="xs:string" use="required">
>             <xs:simpleType>
>                 <xs:restriction base="xs:string">
>                     <xs:enumeration value="me" />
>                     <xs:enumeration value="you" />
>                     <xs:enumeration value="us" />
>                 </xs:restriction>
>             </xs:simpleType>
>         </xs:attribute>
>
>         <xs:sequence>
>             <!-- No definition yet ... -->
>         </xs:sequence>
>     </xs:complexType>
> </xs:element>
>
> What I am looking for is the ability to define <widget>'s sequence based on
> the value of foo.  If foo="me" I want it to contain, say, an <apple>
> element. Else if it is "you" or "us" I want it to contain an <orange> child
> element.  So for example:
>
> <widget foo="me">
>     <apple>Hello!</apple>
> </widget>
>
> <widget foo="us">
>     <orange>XSD!</orange>
> </widget>
>
> I have been pouring through the tutorials and cannot find a way to include
> this conditional functionality...

The only way you can do it in XSD 1.0 is to add an xsi:type to the
widget element to differentiate it eg:

<widget foo="you" xsi:type="you-widget">

<widget foo="me" xsi:type="me-widget">

...along with the specific types:

<xsl:complexType name="you-widget">

<xsl:complexType name="me-widget">

If you can use XSD 1.1 then you don't need the xsi:type attribute as
you can use xs:alternative:

http://ajwelch.blogspot.com/2008/07/validating-co-constrains-in-xml-schema.html



-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/
Received on Friday, 5 December 2008 17:45:20 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:15:09 GMT