Is XML Schema's xs:ID type valid for an XML element, or only for attribuutes?

1.8k Views Asked by At

In the W3C Reco (here), I find that

[...] ID is a type of attribute and so it cannot be applied to attributes, elements or their content

As I can find some in the file I must deal with, I wonder if xsi:ID is really a valid xlm element type

My existing xsd mention :

<xsd:complexType name="customTypeName">
 <xsd:sequence>
  <xsd:element name="ID" type="xsd:ID"/>
  <xsd:element name="myElement" type="string"/>
 </xsd:sequence>
</xsd:complexType>

But according to my reading, I rather think that the valid declaration should rather be

<xsd:complexType name="customTypeName">
<xsd:element name="myElement" type="string"/>
<attribute name="ID" type="xsd:ID" use="required"/>  
</xsd:complexType>

But unfortunatelly, I can't change the already used xsd... I'm willing to apply xslt search using the id() function. Should I give up or is there a way ?

Thanks by advance for any help to my first question over a forum.

1

There are 1 best solutions below

0
On BEST ANSWER

(Good question, pity it didn't receive enough attention at the time.)

ID is a type of attribute and so it cannot be applied to attributes, elements or their content

This was a bug in the spec. It should have said "cannot be applied to elements or their content". In XML Schema 1.1 it is quite clear, though:

For compatibility, ID should be used only on attributes.

This means, you can apply the type to attributes, but not to elements, but it typically won't raise an error when you do.

However, using xs:ID does not always have the effect that people expect. For instance, it expects an NCName, which means it cannot start with a digit. Also, the uniqueness constraint is not enforced.

It is usually a lot better to use xml:id. It is well-defined by the spec, and it is well-supported by the fn:id() function you referred to. Also, using xml:id, people don't need to learn a new name and it doesn't require an extra namespace definition.

Even if your XSD validator accepts xs:ID on an element, the XPath specification does not accept it and will not return it when using fn:id(). However, both this function and the element-with-id() function return the element the ID is applied on anyway, so that does not need to be too much of a problem.