Unique values

Going far beyond the DTD concept of using a special attribute type (the ID type) to uniquely identify a single element within the whole document, this standard allows a set of selected elements to form a group, within which each element must have a unique value somewhere within it, though not necessarily an attribute value, and possibly a combination of values.

Unique elements

The Unique element can appear at the end of an Element definition, after the optional ComplexType or SimpleType sub-element. If the test is to be applied to the whole document instance, then this element should be placed in the definition of the root element, as in the Book element definition here (placement elsewhere is discussed later):

<element name="book">
  <complexType>...</complexType>
  <unique name="...">...</unique>
</element>

The Unique element also has a Name attribute, which can be used in 'key' references (described below).

The Unique element may first contain an Annotation element, then contains further elements that identify the structures that must be unique, and the values within these structures that should make them uniquely identifiable.

Element structure selectors

An XPath expression (see Chapter 13) identifies the elements that form a set of unique items. The Selector element has an XPath attribute, which has an implied path starting point of the element the Unique element is defined within (in this case the whole book, as represented by the Book element). In the following example, only the Chapter element children of the Book element are being considered:

<element name="book">
  <unique name="MyUniqueChaptersInBook">
    <annotation>
      <documentation>CHAPTERS ARE UNIQUE</documentation>
    </annotation>
    <selector xpath="chapter">
      <annotation>CHAPTER ELEMENTS IN BOOK</annotation>
    </selector>
    ...
  </unique>
</element>

   <book>
     <intro>...</intro>
     <chapter>...</chapter>
     <chapter>...</chapter>
     <chapter>...</chapter>
     <appendix>...</appendix>
   </book>

There are some limitations on the XPath expressions that can be used. The most significant thing to note is that it is not possible to navigate up the document structure, and that an attribute cannot be selected. The XPath expression can still have optional components, separated by '|' symbols, but each component is limited to starting with './/', '.', a qualified element name (such as 'ACME:chapter' or 'chapter'), the wildcard symbol ('*') or any element from a given namespace ('ACME:*'), and thereafter consists of '/' separated steps. The 'child::' prefix may also be used.

The XPath expression is namespace-sensitive; the relevant prefix defined in the schema document is used in the expression, but is mapped to the appropriate local document prefix when the document is being validated.

Unique identifier selectors

It is necessary to specify which attribute value or element content value forms the unique identifier. This is done using a Field element, which also has an XPath attribute, but this time the path it contains identifies the objects containing the unique values, and also assumes that the target object has already been selected, so the expression is relative to this position.

The XPath expression is the same as for selectors, as described above, except that it is possible to select attribute values. For example, if each chapter has an Id attribute, then the path '@id' selects the Id attribute of each Chapter element:

<unique name="MyUniqueChapters">
  <selector xpath="chapter" />
  <field xpath="@id" />
</unique>


   <book>
     <chapter id="Chapter1">...</chapter>
     <chapter id="Chapter2">...</chapter>
     <chapter id="Chapter3">...</chapter>
   </book>

Alternatively, the chapters could all have a unique Title child element:

<unique name="MyUniqueChapters">
  <selector xpath="chapter" />
  <field xpath="title" />
</unique>
   <chapter>
     <title>Chapter One</title>
     ...
   </chapter>
   <chapter>
     <title>Chapter Two/title>
     ...
   </chapter>
   <chapter>
     <title>Chapter Three</title>
     ...
   </chapter>

However, there can be one or more instances that have no value at all, without causing any errors. This is an important distinguishing feature between this feature and the 'key' feature (described later).

Compound values

There may be situations when a single attribute value or element content value is not guaranteed to be unique, but a combination of values would be.

For example, in a catalogue an item of clothing might be identified by a combination of its product code, its size and its colour. Multiple Field elements identify each piece of the unique value:

<unique name="UniqueClothesItem">
  <selector xpath="item" />
  <field xpath="@X123"/>
  <field xpath="size"/>
  <field xpath="colour"/>
</unique>


   <item code="X123">
     <title>Summer Dress</title>
     <size>10</size>
     <colour>red</colour>
     <price>...</price>
     <description>...</description>
   </item>

The document is invalid if the combined value is not unique. For example, if there are two items in the catalogue that have a code of 'X123', a size of '10' and a colour of 'red', then an error is reported.

Partial document coverage

The scope of the uniqueness test is limited to each instance of the element type that contains the definition. When defined in an element that is not the root element of a particular instance, the test is reapplied within each instance of that element.

For example, when specified within the Chapter element definition it applies separately to each chapter. So the Section element identifiers are considered unique in the example below, even though they are not unique within the book as a whole:

<element name="chapter">
  ...
  <unique name="SectionIDs">
    <selector xpath="section" />
    <field xpath="@id" />
  </unique>
</element>


   <book>
     <chapter>
              <!-- THIS IS UNIQUE -->
       <section id="SEC1">...</section>
       <section id="SEC2">...</section>
       <section id="SEC3">...</section>
     </chapter>

     <chapter>
              <!-- THIS IS UNIQUE -->
       <section id="SEC1">...</section>
       <section id="SEC2">...</section>
       <section id="SEC3">...</section>
     </chapter>

     <chapter>
              <!-- THIS IS UNIQUE -->
       <section id="SEC1">...</section>
       <section id="SEC2">...</section>
       <section id="SEC3">...</section>
     </chapter>
   </book>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.111.1