Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an attribute ID in XML

Tags:

xml

xsd

I'm writing some XML and an XSD as an assignment... In my XML i have a tag called a ( not actual name) and attribute called id. Part of my XML is shown below:

    <a id="1">
    ...........
    </a>
    <a id="1">
    ............
    </a>

When I validate using XSD it doesn't give an error....

    <xsd:attribute name="id" type="xsd:string" />

I tried to use xsd:ID as a data type of attribute id but it gave me an error; I couldn't figure out what the problem is.

How can I do this?

like image 745
Zeemaan Avatar asked Mar 26 '12 17:03

Zeemaan


2 Answers

You should go back to using type="xsd:ID". What this does in addition to making sure that the value is unique is that it will also allow you to use xsd:IDREF for referencing.

The error you're getting when you try to use xsd:ID is that an ID value must start with a letter. If you change your ID's to something like "ID-1"/"ID-2" or "a1"/"a2", it will work fine.

Example Schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xsd:element name="doc">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element maxOccurs="unbounded" ref="a"/>
        <xsd:element maxOccurs="unbounded" ref="b"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="a">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="xsd:string">
          <xsd:attribute name="id" use="required" type="xsd:ID"/>
        </xsd:extension>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="b">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="xsd:string">
          <xsd:attribute name="idref" use="required" type="xsd:IDREF"/>
        </xsd:extension>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Example XML:

<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="Untitled1.xsd">
  <a id="ID-1">
    ...........
  </a>
  <a id="ID-2">
    ............
  </a>
  <b idref="ID-1"/>
</doc>
like image 145
Daniel Haley Avatar answered Nov 08 '22 11:11

Daniel Haley


"1" is a valid string, so validation does not return an error. If you want to specify some restriction (e.g. "id should starts with a letter"), you have to declare your type and specify the pattern:

<xsd:simpleType name="myID">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="[a-zA-Z].*"/>
    </xsd:restriction>
</xsd:simpleType>
....
  <xsd:attribute name="id" type="myID"/>
....

If you want to specify the uniqueness restriction, you can use the xsd:unique element like this:

<xsd:element name="root" type="myList">
  <xsd:unique name="myId">
    <xsd:selector xpath="./a"/>
    <xsd:field xpath="@id"/>
  </xsd:unique>
</xsd:element> 

This will mean that element "root" declared as some "myList" should contain subelements "a" with unique attributes "id"

like image 44
Alexey Pomelov Avatar answered Nov 08 '22 11:11

Alexey Pomelov