How to specify the xml schema unique restriction for elements of different levels. For example the ID and ItemA in the xml file:

 <root> <ID>1</ID> <Items> <Item> <ItemA>1</ItemA> <ItemB>2</ItemB> <ItemC>3</ItemC> </Item> <Item> <ItemA>4</ItemA> <ItemB>5</ItemB> <ItemC>6</ItemC> </Item> <Item> <ItemA>7</ItemA> <ItemB>8</ItemB> <ItemC>9</ItemC> </Item> </Items> </root> 

    1 answer 1

    For the given xml, you can use, for example, the following scheme:

     <?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root"> <xs:complexType> <xs:sequence> <xs:element name="ID" type="xs:unsignedByte" /> <xs:element name="Items"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded" name="Item"> <xs:complexType> <xs:sequence> <xs:element name="ItemA" type="xs:unsignedByte" /> <xs:element name="ItemB" type="xs:unsignedByte" /> <xs:element name="ItemC" type="xs:unsignedByte" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> <xs:unique name="global_unique"> <xs:selector xpath="ID | Items/Item/*"/> <xs:field xpath="."/> </xs:unique> </xs:element> </xs:schema> 

    The XPath to all elements that must be unique is specified in the selector node using the union operator | .

    • Thank you for your reply. - prunum
    • @prunum, if you are given an exhaustive answer, please mark it as accepted (“tick” to the left of the answer). - aleksandr barakin