ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 5 / 6 << | Prev | Next |
Contents > Unique and key > Unique and nillable

Unique and nillable

  1. XML Schema - unique and nillable
XML Schema keys: unique, nillable

1. XML Schema - unique and nillable

We will use "unique" element to define, that the elements "a" under "root" element must have unique value of "id" element. "Unique" does not require the value to exist, so the element can be "nil".

Valid document


<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <a>
    <id xsi:nil="true"/>
  </a>
  <a>
    <id>y</id>
  </a>
  <a>
    <id>z</id>
  </a>
</root>

Correct XML Schema (correct_0.xsd)


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

  <xsd:element name="root" type="myList">
    <xsd:unique name="myId">
      <xsd:selector xpath="./a"/>
      <xsd:field xpath="id"/>
    </xsd:unique>
  </xsd:element>

  <xsd:complexType name="myList">
    <xsd:sequence minOccurs="1">
      <xsd:element name="a" minOccurs="1" maxOccurs="unbounded">
        <xsd:complexType>
          <xsd:sequence minOccurs="1">
            <xsd:element name="id" type="xsd:NCName" nillable="true"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>