Question

I want to write a XSD Schema for the following XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<siss-statusquery xmlns="http://www.example.de/test">
    <myhash id="1">DG5F6DFG13DFG5641DG5F6DFG13DFG56411337AS</myhash>
    <myhash id="2">123AWDFG13DFG5641DG5F6DFG13GFG56411337AS</myhash>
    <myhash id="3">DG5F6DFG13DFG5641DG5F6325DFG13DFG5641143</myhash>
</siss-statusquery>

Conditions:

  • <myhash /> tag, minoccurs: 1 , maxoccurs: unbounded
  • id attribute, type: unsignedInt, is unique, is required
  • <myhash /> tag value pattern: [0-9A-Z]{40,40}

My attempt:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified"
           elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.example.de/test" 
           xmlns:lhs="http://www.example.de/test">

  <xs:simpleType name="myhashType">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9A-Z]{40,40}" />
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="siss-statusquery">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="myhash"
                    maxOccurs="unbounded"
                    minOccurs="1">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="lhs:myhashType">
                <xs:attribute type="xs:unsignedInt" name="id" use="required" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

How can I constrict/restrict the id attribute as unique?

Was it helpful?

Solution

XML Schema has a type for globally unique IDs - xs:ID - but you can't use that here because values of this type have to be valid XML names (so in particular they can't start with a digit).

The way to apply more general uniqueness constraints is to use xs:unique:

<xs:element name="siss-statusquery">
    <xs:complexType>
       <!-- as before -->
    </xs:complexType>
    <xs:unique name="uniqueId">
        <xs:selector xpath="lhs:myhash" />
        <xs:field xpath="@id" />
    </xs:unique>
</xs:element>

The way to read this is that within the scope of the containing element, all the elements matched by the selector must have different values for their field. So in this case, within the siss-statusquery, all the myhash elements must have different id attributes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top