0
votes

I'm fairly new to XML, and to coding in general.

I'm trying to validate an XML markup with a schema that I've put together. One of my elements utilizes the xml:id attribute. It looks like this: <item xml:id="MAT_10_23">. In my schema, I've followed the recommended structure with <xs:attribute name="id" type="xs:ID"/> in the element declaration. Here it is in full:

<xs:element name="item">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="label">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:ID"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

When I try to validate this my markup, however, I'm told that "Attribute xml:id is not allowed to appear in the element ". I've tried declaring the attribute separately in my schema separately. This is what it looks like:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:attribute name="id" type="xs:ID"/>

But this does not change anything. What do I need to change in my schema to successfully validate my XML markup?

1

1 Answers

0
votes

You've declared an attribute with local name id in no namespace, when you want the attribute to be in the XML namespace. You need to:

(a) replace the attribute declaration by

<xs:attribute ref="xml:id"/>

(b) add an xs:import for the schema for the XML namespace (which contains a declaration of this attribute):

<xs:import namespace="http://www.w3.org/XML/1998/namespace"/>

You may also need to add a schemaLocation attribute to provide a location for a copy of this schema document - some schema processors will recognize the namespace and know where to find its definition, but I don't think that this is true of all processors.