<xsd:key> ElementÂ
Specifies that an attribute or element value (or set of values) must be a key within the specified scope. The scope of a key is the containing element in an instance document. A key means that data should be unique within a specified scope, non-nillable, and always present.
<key
id = ID
name = NCName
{any attributes with non-schema Namespace}...>
Content: (annotation?, (selector, field+))
</key>
Attributes
id
The ID of this element. The id value must be of type ID and be unique within the document containing this element.Optional.
name
The name of the key element. The name must be a no-colon-name (NCName) as defined in the XML Namespaces specification.The name must be unique within an identity constraint set.
Required.
Element Information
Number of occurrences |
Unlimited |
Parent elements |
|
Contents |
Remarks
The key element must contain the following elements in order.
selector |
The selector element contains an XML Path Language (XPath) expression specifying the set of elements across which the values specified by field must be unique. There must be one and only one selector element. |
field |
Each field element contains an XPath expression specifying the values (attribute or element values) that must be unique for the set of elements specified by the selector element. If there is more than one field element, the combination of the field elements must be unique. In this case, the values for a single field element may or may not be unique across the selected elements but the combination of all the fields must be unique. There must be one or more field element(s). |
Example
The following example defines a keyref element that corresponds to the key element in this schema.
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="namespace1"
xmlns:r="namespace1"
elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="A" type="r:A" maxOccurs="unbounded">
<xs:keyref name="dummy" refer="r:pNumKey">
<xs:selector xpath="r:part"/>
<xs:field xpath="@ref-number"/>
</xs:keyref>
</xs:element>
<xs:element name="B" type="r:B"/>
</xs:sequence>
</xs:complexType>
<xs:key name="pNumKey">
<xs:selector xpath="r:B/r:part"/>
<xs:field xpath="@key-number"/>
</xs:key>
</xs:element>
<xs:complexType name="A">
<xs:sequence>
<xs:element name="part" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="ref-number" type="xs:integer"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="B">
<xs:sequence>
<xs:element name="part" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="key-number" type="xs:integer"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
Input: key.xml
<root xmlns="namespace1">
<A>
<!-- if the ref-number is not equal to one of the key-number, the validation will give error -->
<part ref-number="1"/>
</A>
<A>
<!-- if the ref-number is not equal to one of the key-number, the validation will give error -->
<part ref-number="2"/>
</A>
<B>
<part key-number="1"/>
<part key-number="2"/>
<part key-number="3"/>
</B>
</root>
Other Resources
For more information see the W3C XML Schema Part 1: Structures Recommendation at www.w3.org/TR/2001/REC-xmlschema-1-20010502\#element-all.