XML technologies / XML Schema / Defining uniqueness, keys and key references / xs:key and xs:unique for co-occurrence constraints

(Excerpt from “XML Schema” by Eric van der Vlist)

Dependencies (co-occurrence constraints) express dependencies between children or attributes, such as “if this element exists, that attribute must be absent”. Although poorly implemented in W3C XML Schema, they can work around the consistent-declaration rule forbidding two content models for an element at one instance location. A superset of both models can be constrained to exclude unwanted combinations. This helps vocabularies such as RDF, where properties may be attributes or elements. We extend our book example to accept ISBN and title in either form. Both documents below and the other two combinations are valid:

<book id="b3810518883" available="true">
   <isbn>
      3810518883
   </isbn>
   <title lang="de">
      Auf den Hund gekommen
   </title>
   ../..
</book>

or:

<book id="b3810518883" available="true" isbn="3810518883"
   title="Auf den Hund gekommen">
   .../...
</book>

The obvious approach defines element book as a choice between four valid content models. The consistent-declaration rule forbids this: only one model per element is allowed. Instead, define one accepting optional elements and attributes:

<xs:complexType>
    <xs:sequence>
        <xs:element ref="isbn" minOccurs="0"/>
        <xs:element ref="title" minOccurs="0"/>
        <xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute ref="id"/>
    <xs:attribute ref="available"/>
    <xs:attribute ref="isbn"/>
    <xs:attribute ref="title"/>
</xs:complexType>

This allows documents with title, or isbn, as both element and attribute, or documents without title or isbn. We need co-occurrence constraints. These cannot be expressed generally with W3C XML Schema, requiring embedded languages as shown in Documenting schemas. But xs:unique , xs:key and xs:keyref are special co-occurrence constraints usable here. To ensure exactly one title and ISBN, define an xs:key within element book itself:

<xs:key name="isbn">
    <xs:selector xpath="."/>
    <xs:field xpath="isbn|@isbn"/>
</xs:key>
<xs:key name="title">
    <xs:selector xpath="."/>
    <xs:field xpath="title|@title"/>
</xs:key>

These keys apply within each book, without outside effects. They invalidate the book if the XPath expression returns zero or several nodes. Using xs:unique instead of xs:key would still enforce at most one element or attribute but make the property optional. For completeness, the full definition of element book is:

<xs:element name="book">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="isbn" minOccurs="0"/>
            <xs:element ref="title" minOccurs="0"/>
            <xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
            <xs:element ref="character" minOccurs="0"
                maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute ref="id"/>
        <xs:attribute ref="available"/>
        <xs:attribute ref="isbn"/>
        <xs:attribute ref="title"/>
    </xs:complexType>
    <xs:key name="isbn">
        <xs:selector xpath="."/>
        <xs:field xpath="isbn|@isbn"/>
    </xs:key>
    <xs:key name="title">
        <xs:selector xpath="."/>
        <xs:field xpath="title|@title"/>
    </xs:key>
</xs:element>

   

 

 

 

back to top