← Prev in month ← Prev in thread

Here's a hack to avoid the dreaded "Unique Particle Attribution" error

From
Roger L Costello <>
To
"" <>
Date
2022-01-21T14:44:59Z
ID
<>
Thread
Here's a hack to avoid the dreaded "Unique Particle Attribution" error
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
← Prev in month ← Prev in thread