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

Re: xs:choice problem

From: Jeni Tennison <jeni@jenitennison.com>
Date: Mon, 15 Apr 2002 11:42:31 +0100
Message-ID: <1841134529006.20020415114231@jenitennison.com>
To: "Laurent Chaply" <lchaply@imrglobal.fr>
CC: xmlschema-dev@w3.org
Hi Laurent,

> I have a problem using xs:choice with the same element name but with
> different types
[snip]
> Has anybody an idea to explain how this example is processed by the
> validator, and if there is another solution to do the same thing. I
> can't use "xsi:type" because I'm creating XML Schemas from existing
> XML documents.

I'm afraid that you can't use XML Schema to say that an element can
have one of two types without using xsi:type in the instance document
to indicate which of the two types a particular instance element is.

The best you can do is create a type that encompasses both
possibilities, and then use extra processing if necessary (for example
Schematron) to state that only one of the two is possible. In your
example, you could do:

<xs:complexType name="T1">
  <xs:complexContent>
    <xs:extension base="T0">
      <xs:choice>
        <xs:element name="A" type="xs:string"/>
        <xs:element name="B" type="xs:string"/>
      </xs:choice>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

and then simply have:

<xs:element name="E0">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="E1" type="T1"
                  minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/
Received on Monday, 15 April 2002 07:28:23 GMT

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