[Home] [By Thread] [By Date] [Recent Entries]

  • From: Roger L Costello <costello@m...>
  • To: "xml-dev@l..." <xml-dev@l...>
  • Date: Fri, 21 Jan 2022 14:44:59 +0000

Hi Folks,

This XML Schema choice:

<xs:choice>
    <xs:sequence>
        <xs:element name="Title" type="xs:string"/>
        <xs:element name="ISBN" type="xs:string"/>
    </xs:sequence>
    <xs:sequence>
        <xs:element name="Title" type="xs:string"/>
        <xs:element name="Edition" type="xs:string"/>
    </xs:sequence>
</xs:choice>

results in this error: 

	Title and Title violate "Unique Particle Attribution".
	During validation against this schema, ambiguity 
	would be created for those two particles.

The branches of a choice cannot start with the same element name.

Suppose, for whatever reason, you need the choices to start with the Title element, what to do?

Here's a hack: Create a base complexType and for each branch of the choice create a subtype:

<xs:complexType name="Publication" abstract="true" />

<xs:complexType name="BookPublication">
    <xs:complexContent>
        <xs:extension base="Publication">
            <xs:sequence>
                <xs:element name="Title" type="xs:string"/>
                <xs:element name="ISBN" type="xs:string"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:complexType name="MagazinePublication">
    <xs:complexContent>
        <xs:extension base="Publication">
            <xs:sequence>
                <xs:element name="Title" type="xs:string"/>
                <xs:element name="Edition" type="xs:string"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

Declare an element with the base type, Publication, as its type:

<xs:element name="Item" type="Publication"/>

In an XML instance document, if you want the <Item> element to hold book data (Title, ISBN), do this:

<Item xsi:type="BookPublication">
    <Title>...</Title>
    <ISBN>...</ISBN>
</Item>

If you want the <Item> element to hold magazine data (Title, Edition), do this:

<Item xsi:type="MagazinePublication">
    <Title>...</Title>
    <Edition>...</Edition>
</Item>

TaDa!

/Roger


[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member