Question

When validating the the following XML against the schema, the reference attributes of KeyB are marked as missing/undeclared unless prefixed with a namespace. Similar attributes for KeyA, which are declared "inline", validate fine. Can someone explain this to me? (Note: Using .NET's XmlReader for validation).

Schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="so"
    targetNamespace="http://test/so.xsd"
    elementFormDefault="qualified"
    xmlns="http://test/so.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:attribute name="Id" type="xs:unsignedByte" />
  <xs:attribute name="Index" type="xs:unsignedByte" />

  <xs:element name="KeyA">
    <xs:complexType>
      <xs:attribute name="Id" type="xs:unsignedByte" use="required" />
      <xs:attribute name="Index" type="xs:unsignedByte" use="required" />
    </xs:complexType>
  </xs:element>

  <xs:element name="KeyB">
    <xs:complexType>
      <xs:attribute ref="Id" use="required" />
      <xs:attribute ref="Index" use="required" />
    </xs:complexType>
  </xs:element>


  <xs:element name="Keys">
    <xs:complexType>
      <xs:all>
        <xs:element ref="KeyA"/>
        <xs:element ref="KeyB"/>
      </xs:all>
    </xs:complexType>
  </xs:element>

</xs:schema>

XML instance example:

<?xml version="1.0" encoding="utf-8" ?>
<Keys xmlns="http://test/so.xsd">
  <KeyA Id="0" Index="3"/>
  <KeyB Id="0" Index="3"/>
</Keys>

I receive the following error messages for the KeyB element:

The required attribute 'http://test/so.xsd:Index' is missing.
The required attribute 'http://test/so.xsd:Id' is missing.

The 'Index' attribute is not declared.
The 'Id' attribute is not declared.
Was it helpful?

Solution

Well, that's easy.

The local attributes (that's what you call declared "inline") may be qualified and may be not. "Qualified" roughly means that the namespace prefix will be required.

This is controlled by the attributeFormDefault attribute of your <xs:schema>. If you had specified:

<xs:schema id="so"
    targetNamespace="http://test/so.xsd"
    elementFormDefault="qualified"
    attributeFormDefault="qualified"
    xmlns="http://test/so.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

....

then, all local attributes would have to be qualified.

But by default, the value of attributeFormDefault is "unqualified". So, when you miss it, you have all local attributes unqualified (no namespace prefix needed).

Concerning global attributes (and only them you can include by reference), they must always be qualified. That's actually the rule for anything declared globally (and elements as well).

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