Search code examples
xmlxsdxsd-1.0

global key/unique attribute in xsd


I am trying those days to find a way to create a global attribute that will be used by all the elements in the schema and will serve as a key/unique attribute to them a well. look a the next example:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
attributeFormDefault="unqualified" targetNamespace="http://www.NameSpace/Family" xmlns:tns="http://www.NameSpace/Family">
<xs:attribute name="id" type="xs:string"/>
<xs:complexType name="parentType">
  <xs:sequence>
    <xs:element name="Name" type="xs:string"/>
    <xs:element name="Child" type="tns:childType" minOccurs="1" maxOccurs="unbounded"/>
  </xs:sequence>  
  <xs:attribute ref="tns:id" use="required"/>  
</xs:complexType>  
<xs:complexType name="childType">
  <xs:sequence>
    <xs:element name="Name" type="xs:string"/>
  </xs:sequence>  
  <xs:attribute ref="tns:id" use="required"/>  
</xs:complexType>
<xs:element name="Family">  
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Parent" type="tns:parentType" minOccurs="1" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

now lets say for example, I create 1 parent with 2 kids, I want do define a key/unique on the id attribute so that all the elements id's (parents an childs) will be different from each other.


Solution

  • You can declare id as having type xsd:ID like this:

    <xs:attribute name="id" type="xs:ID"/>
    

    Then you reference the declared attribute (like you already do):

    <xs:complexType name="parentType">
    ...
      <xs:attribute ref="tns:id" use="required"/>
    </xs:complexType>
    

    You could also just declare the id attribute on the types or elements directly.

    The xs:ID type imposes ID semantics globally on all ID-declared attributes in a document.