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

Re: using xsd:choice + element value

From: Eddie Robertsson <erobertsson@allette.com.au>
Date: Mon, 27 May 2002 10:10:37 +1000
Message-ID: <3CF1797C.1CF53D52@allette.com.au>
To: mlong@phalanxsys.com
CC: xmlschema-dev@w3.org
Hi Matt,

No, you can't do that using the design you have in your schema. However,
if I understand your problem correct I think you can use the technique
of using xsi:type in the instance document to specify which type an
element should use from the instance document. Here is a modified
version of your schema where complexTypes are used instead of groups:

<xsd:element name=”whatIsIt” type=”baseType”/>

<!-- Empty type that is used as a common base for personType and
placeType -->
<xsd:complexType name="baseType"/>

<!-- Type defining the person -->
<xsd:complexType name=”personType”>
    <xsd:complexContent>
        <xsd:extension base="baseType">
            <xsd:sequence>
                <xsd:element name=”firstName” type=”xsd:string”/>
                <xsd:element name=”lastName” type=”xsd:string”/>
            </xsd:sequence>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

<!-- Type defining the place -->
<xsd:complexType name=”placeType”>
    <xsd:complexContent>
        <xsd:extension base="baseType">
            <xsd:sequence>
                <xsd:element name=”country” type=”xsd:string”/>
                <xsd:element name=”city” type=”xsd:string”/>
            </xsd:sequence>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

Note that the whatIsIt element have the type baseType. Also, I have
removed you qualifier element because instead of using an element to
qualify which type is used we're going to use xsi:type in the instance
to specify which of the derived types the whatIsIt element has. E.g.

<whatIsIt xsi:type="personType">
    <firstName>aaa</firstName>
    <lastName>bbb</lastName>
</whatIsIt>

This will only validate the whatIsIt element if it has subelements
matching the personType. The same applies for the placeType:

<whatIsIt xsi:type="placeType">
    <country>aaa</country>
    <city>bbb</city>
</whatIsIt>

Is this something that would fit your requirement?

Cheers,
/Eddie


> Is it possible to utilize ‘choice’ from an element value (not purely
> the existence of the element name)
>
> Example: a schema with a qualifier (1=person, 2=place)…so the question
> is how can one determine the group ref from the element value of
> ‘qualifier’ element (or is it simply not possible)?
>
> -Matt Long
>
> Phalanx Systems, LLC
>
> <xsd:element name=”whatIsIt” type=”z:infoType”/>
>
> <xsd:complexType name=”infoType”>
>
> <xsd:sequence>
>
> <xsd:element name=”qualifier” type=”z:myQualType”/>
>
> <!—how can ‘qualifier’ qualify the choice of group reference 
>
> <xsd:choice>
>
> <xsd:group ref=”z:personGroup”/>
>
> <xsd:group ref=”z:placeGroup”/>
>
> </xsd:choice>
>
> </xsd:sequence>
>
> </xsd:complexType>
>
> <xsd:group name=”personGroup”>
>
> <xsd:sequence>
>
> <xsd:element name=”firstName” type=”xsd:string”/>
>
> <xsd:element name=”lastName” type=”xsd:string”/>
>
> </xsd:sequence>
>
> </xsd:group>
>
> <xsd:group name=”placeGroup”>
>
> <xsd:sequence>
>
> <xsd:element name=”country” type=”xsd:string”/>
>
> <xsd:element name=”city” type=”xsd:string”/>
>
> </xsd:sequence>
>
> </xsd:group>
>
> <xsd:simpleType name=”myQualType”>
>
> <xsd:restriction base=”xsd:string”>
>
> <xsd:enumeration value=”1”/>
>
> <xsd:enumeration value=”2”/>
>
> </xsd:restriction>
>
> </xsd:simpleType>
>
Received on Sunday, 26 May 2002 19:57:45 UTC

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