You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by "Schawn E. Thropp" <th...@adlnet.org> on 2002/04/29 20:18:53 UTC

xsd:choice with xsd:group

I have a situation where I would like to define an XSD to allow to different "groups" of data.  I would
like to use the xsd:choice mechanism to allow Xerces to choose between the two groups during validation.
The groups contain the same makeup, however there is different values depending on the <source>.  For example:

Choice 1:
<element1>
   <source>foo</source>
   <value>bar</value>
</element1>

Choice 2:
<element1>
   <source>anystring</source>
   <value>anyvalue</value>
</element1>

So if <source> element has a value of "foo" I would like the <value> element to be forced to be part of an 
enumeration (foo, bar, foobar).  If the <source> element does not have a value of "foo" then I really don't care what the value
of <value> element is, it can be any string.  

Choice 3 is INVALID, since cat is not a valid enumeration for the value element if the source element is
set to foo.
<element1>
   <source>foo</source>
   <value>cat</value>
<element1>

Does anyone know how I can write my XSD to allow this situation? Can this be done with the current W3C XSD Recommendation?

Thanks for any help 
Schawn

Re: xsd:choice with xsd:group

Posted by Eddie Robertsson <er...@allette.com.au>.
Hi Schawn,

> So if <source> element has a value of "foo" I would like the <value>
> element to be forced to be part of an
> enumeration (foo, bar, foobar).  If the <source> element does not have
> a value of "foo" then I really don't care what the value
> of <value> element is, it can be any string.
> -------------------------------------------
> Questions specific to XML Schema are better asked on the
> xml-schema-dev list (xmlschema-dev@w3.org).
>
> To answer your question there is no direct way of doing this using XML
> Schema but you can come fairly close by using the xsi:type attribute.
> This means that you will have to add this attribute to you instance
> document to identify the type you want to use for the element. See [1]
> for more information on the xsi:type attribute.
>
> In you example this would be something like this:
>
> <element1 xsi:type="valueEnumType">
>    <source>foo</source>
>    <value>bar</value>
> </element1>
>
> or
>
> <element1 xsi:type="valueAnyType">
>    <source>anystring</source>
>    <value>anyvalue</value>
> </element1>
>
> Where valueEnumType and valueAnyType are defined in your XML Schema as
> having the two elements "source" and "value".  valueEnumType would
> have "source" to a fixed value of "foo" and "value" as an enumeration
> list while valueAnyType would have them both as xs:string or whatever
> type you want to use.
> So, this means that you can't use the value of the source element to
> trigger which type to use but if you can annotate the instance
> document with the xsi:type attribute then this would work.
>
> If you want to have the value of "source" to trigger validation of the
> "value" element you can embedd Schematron rules within your XML Schema
> which can check this for you. See [2] fro more information about this.
>
> Cheers,
> /Eddie
>
> [1] http://www.w3.org/TR/xmlschema-0/#UseDerivInInstDocs
> [2] http://www.topologi.com/public/Schtrn_XSD/Paper.html
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>   Choice 3 is INVALID, since cat is not a valid enumeration for the
> value element if the source element is
> set to foo.
> <element1>
>    <source>foo</source>
>    <value>cat</value>
> <element1> Does anyone know how I can write my XSD to allow this
> situation? Can this be done with the current W3C XSD
> Recommendation?Thanks for any helpSchawn


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org