Search code examples
xmlxsdxsd-validationxml-validation

Multiple xs:id Attributes Under Single Element - XSD Definition


I'm getting an error in IntelliJ 2015 (IJ) when trying to define an XSD with an element that has two attributes of type "xs:ID". Unfortunately I've inherited this code from someone long gone so not sure exactly what they were trying to achieve.

Here's a stripped down version of the XSD:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    ...
    <xs:element name="visitor-test">
        <xs:complexType>
            ...
            <xs:attribute name="null-node" type="xs:ID" default="null-node"/>
            <xs:attribute name="null-id" type="xs:ID" default="null-id"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

And here's the start of the XML, with the error:

<?xml version="1.0" encoding="UTF-8"?>
<visitor-test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="visitor-test-schema.xsd" null-id="f">

IJ is underlining in red and complaining that:

Attribute 'null-id' is not allowed to appear in element 'visitor-test

What does work:

  1. If I change the XML attribute to null-node i.e. null-node="f">
  2. If I reverse the order of the attributes null-node and null-id in the XSD (but then the same problem occurs for null-node)
  3. If I change the type of either attribute in the XSD to xs:String e.g. <xs:attribute name="null-node" type="xs:string" default="null-node"/>

Since this isn't my code, and I'm not sure what these are being used for, I'm wondering, did my predecessor make a mistake defining two ID attributes on the same element? Is that illegal according to "http://www.w3.org/2001/XMLSchema"? And if so, what is the "safest" choice of mine? It seems #3...

Also, #1 and #2 together imply that only the first attribute define in the XSD seems to be valid, subsequent ones are not allowed. Strangely though, both attributes are suggested by the IDE.


Solution

  • The xs:ID type in XSD 1.0,

    3.3.8 ID

    [Definition:] ID represents the ID attribute type from [XML 1.0 (Second Edition)].

    derives from the ID attribute type in XML/DTD,

    Validity constraint: One ID per Element Type

    An element type must not have more than one ID attribute specified.

    where you can see that only one ID attribute is allowed per element type.

    Alternatives xs:unique and xs:key do not have such limitations.


    Note: In XSD 1.1, multiple ID attributes per element are allowed:

    G.1.7 ID, IDREF, and related types

    An element may now have multiple attributes of type xs:ID. Elements have always been able to have multiple children of type xs:ID, but XSD 1.0 forbad multiple attributes of this type for compatibility with XML DTDs. (Schemas intended to be translatable into DTD form should still avoid the practice.) This change should make it easier for XML vocabularies to support both existing ID attributes and xml:ID.

    (Thanks to Michael Kay for this helpful update.)