XSD unique constraint on attribute of sibling elements of a specific type

hanylord

New Member
I have an XML document structured as Q&A which follows the following format (edited for clarity):\[code\]<question> <answer id="1"> <question> <answer id="1"/> <answer id="2"/> <answer id="3"/> </question> </answer> <answer id="2"> <question> <answer id="1"/> <answer id="2"/> </question> </answer></question>\[/code\]My XSD looks like this:\[code\]<xs:element name="question"> <xs:complexType> <xs:sequence> <xs:element name="answer" type="answerType" minOccurs="2" maxOccurs="unbounded"> </xs:element> </xs:sequence> </xs:complexType> <xs:unique name="AnswerIdUnique"> <xs:selector xpath="./*" /> <xs:field xpath="@id" /> </xs:unique></xs:element><xs:complexType name="answerType"> <xs:sequence> <xs:element ref="question" minOccurs="0" maxOccurs="1" /> </xs:sequence> <xs:attribute name="id" type="xs:token" use="required" /></xs:complexType>\[/code\]There is, of course, more to it than what you see above but this illustrates my problem.I need for the \[code\]id\[/code\] attribute on \[code\]answer\[/code\] elements to be unique among siblings. The XSD defined above enforces uniqueness of \[code\]id\[/code\] attributes among sibling elements, but it does not discriminate on element type. I've tried a variety of selectors and fields in the unique constraint, but have not found a combination that works.Any suggestions?
 
Back
Top