Reputation: 1143
I'm struggling with an XSD that should apply a unique constraint to all my categories.
Each of my categories can have children which, in turn, are categories. This is what the "Category" portion of my XSD looks like;
<xsd:element name="Categories">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Category" maxOccurs="unbounded" type="Category"/>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="CategoryUnique">
<xsd:selector xpath="Category"/>
<xsd:field xpath="ID"/>
</xsd:unique>
</xsd:element>
And the "Category" type:
<xsd:complexType name="Category">
<xsd:all>
<xsd:element name="ID" type="xsd:unsignedInt"/>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Children" minOccurs="0">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Category" type="Category" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="CategoryChildUnique">
<xsd:selector xpath="Category"/>
<xsd:field xpath="ID"/>
</xsd:unique>
</xsd:element>
</xsd:all>
</xsd:complexType>
This XSD handles the following things correctly:
How do I make sure ANY "Category" node can't have the same ID as any other "Category" node in the same parent container (the "Categories" node is contained in a wrapper)
Upvotes: 0
Views: 363
Reputation: 1143
I've solved this problem!
After a lot of attempts with all additions I could think of, I've figured out how to make the XSD check all the child nodes!
<xsd:unique name="CategoriesUnique">
<xsd:selector xpath=".//Category"/>
<xsd:field xpath="ID"/>
</xsd:unique>
This replaces the unique constraint in the "Categories" node and all subsequent category nodes are checked!
Check W3Schools for information about the Xpath syntax:
// Selects nodes in the document from the current node that match the selection no matter where they are.
. Selects the current node
Upvotes: 2