Try an online XML Schema class for free!
Additional Resources

XML Schema Keys

In this lesson of the XML tutorial, you will learn...
  1. To require uniqueness.
  2. To work with keys.

Uniqueness

XML Schema provides a mechanism for requiring that each element be unique among like elements.

This is best illustrated with an example:

Code Sample: SchemaKeys/Demos/Unique.xsd

---- Code Omitted ----
<xs:element name="Artists"> <xs:complexType> <xs:sequence> <xs:element name="Artist" maxOccurs="unbounded"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="aID" type="xs:string" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Lyrics"> <xs:complexType> <xs:sequence> <xs:element name="Stanza" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="Line" type="xs:string" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="Artist" type="xs:string"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> <xs:unique name="ArtistKey"> <xs:selector xpath="Artists/Artist"/> <xs:field xpath="@aID"/> </xs:unique> </xs:element> </xs:schema>
Code Explanation

The Artist element has an aID attribute, which we would like to be able to use to uniquely identify the artist. The XML Schema xs:unique element is used to enforce this. It takes two children:

  • xs:selector - takes an xpath attribute which holds an XPath 1.0 expression (see footnote) referencing the elements affected by this constraint.
  • xs:field - takes an xpath attribute which holds an XPath 1.0 expression specifying the part of the selected elements that must be unique.

In the example above, the selector XPath identifies all Artist elements that are children of an Artists element. The field XPath identifies the aID attribute as the part of the Artist element that must be unique.

In the XML instance below, each Artist must have a unique aID attribute. Try making them the same and validating.

Code Sample: SchemaKeys/Demos/Unique.xml

<?xml version="1.0"?>
<Song xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Unique.xsd">
 <Title Type="duet">The Girl Is Mine</Title>
 <Year>1983</Year>
 <Length>Medium</Length>
 <Artists>
  <Artist aID="MJ">Michael Jackson</Artist>
  <Artist aID="PM">Paul McCartney</Artist>
 </Artists>
---- Code Omitted ----
</Song>

Keys

XML Schema also provides a mechanism for keys and key references - that is, for creating a relationship between elements through the value of an attribute or contained element. The xs:key and xs:keyref elements are used to create such a relationship.

Code Sample: SchemaKeys/Demos/Keys.xsd

---- Code Omitted ----
<xs:element name="Artists"> <xs:complexType> <xs:sequence> <xs:element name="Artist" maxOccurs="unbounded"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="aID" type="xs:string" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Lyrics"> <xs:complexType> <xs:sequence> <xs:element name="Stanza" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="Line" type="xs:string" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="Artist" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> <xs:key name="ArtistKey"> <xs:selector xpath="Artists/Artist"/> <xs:field xpath="@aID"/> </xs:key> <xs:keyref name="ArtistKeyRef" refer="ArtistKey"> <xs:selector xpath="Lyrics/Stanza"/> <xs:field xpath="@Artist"/> </xs:keyref> </xs:element> </xs:schema>
Code Explanation

Like the xs:unique element, the xs:key and xs:keyref elements each contain xs:selector and xs:field child elements.

The xs:key element is used to identify the elements being referenced by the elements specified by the xs:keyref element.

In the example above, the Artist attribute of the Stanza element must point to an Artist element's aID attribute, which must be unique.

In the XML instance below, each Artist must have a unique aID attribute and each Stanza element must have an Artist attribute with the same value as one of the Artist's aID attributes. Try making changing the value of a Stanza's Artist attribute to something arbitrary and validating.

Code Sample: SchemaKeys/Demos/Keys.xml

<?xml version="1.0"?>
<Song xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Keys.xsd">
 <Title Type="duet">The Girl Is Mine</Title>
 <Year>1983</Year>
 <Length>Medium</Length>
 <Artists>
  <Artist aID="MJ">Michael Jackson</Artist>
  <Artist aID="PM">Paul McCartney</Artist>
 </Artists>
 <Lyrics>
  <Stanza Artist="MJ">
   <Line>Every night she walks right in my dreams</Line>
   <Line>Every night she walks right in my dreams</Line>
---- Code Omitted ----
</Stanza> <Stanza Artist="PM"> <Line>I don't understand the way you think</Line> <Line>Saying that she's yours not mine</Line>
---- Code Omitted ----
</Stanza> <Stanza Artist="MJ"> <Line>I know she'll tell you I'm the one for her</Line> <Line>'Cause she said I blow her mind</Line>
---- Code Omitted ----
</Stanza> </Lyrics> </Song>

XML Schema Keys Conclusion

In this lesson of the XML tutorial, you have learned to work with unique values and keys.

Footnotes

  1. The xpath attribute only supports a subset of XPath 1.0. See http://www.w3.org/TR/2004/PER-xmlschema-1-20040318/structures.html#coss-identity-constraint for details.

To continue to learn XML go to the top of this page and click on the next lesson in this XML Tutorial's Table of Contents.
Last updated on 2009-02-06

Use of http://www.learn-xml-schema-tutorial.com (Website) implies agreement to the following:

Copyright Information

All pages and graphics on Website are the property of Webucator, Inc. unless otherwise specified.

None of the content on Website may be redistributed or reproduced in any way, shape, or form without written permission from Webucator, Inc.

No Printing or saving of pages or content on Website

This content may not be printed or saved. It is for online use only.


Linking to Website

You may link to any of the pages on Website; however, you may not include the content in a frame or iframe without written permission from Webucator, Inc.


Warranties

Website is provided without warranty of any kind. There are no guarantees that use of the site will not be subject to interruptions. All direct or indirect risk related to use of the site is borne entirely by the user. All code and explanations provided on this site are provided without warranties to correctness, performance, fitness, merchantability, and/or any other warranty (whether expressed or implied).


For individual private use only

You agree not to use this online manual to deliver or receive training. If you are delivering or attending a class that is making use of this online manual, you are in violation of our terms of service. Please report any abuse to courseware@webucator.com. If you would like to deliver or receive training using this manual, please fill out the form at http://www.webucator.com/Contact.cfm