0

The XML specification says that unqualified namespaces are in no namespace. The semantics in the spec are debatable but all XML toolsets have been authored to work this way. See:

XML Namespaces and Unprefixed Attributes

It's a concept that my subconscious hates for reasons that my conscious brain struggles to articulate.

If unqualified attributes are not in any namespace, then how come validation still works? It's a contradiction.

And what happens when there's two attributes of the same name in scope. One from a default namespace and one belonging to the current element.

<document xmlns="default.ns" xmlns:hr="humanresources.ns">
  <hr:user id="abc" />
</document>

If id is defined in both default.ns and humanresources.ns but with different data types, say xs:token and xs:integer which namespace will id be resolved to for validation, if either?

Assuming a validator would error on the ambuiguity and force other attribute(s) to be qualified, then would I have to write a helper GetLocalAttribute method to deal with all this?

Like:

  • ids = select all attributes on element where localname equals id
  • b = select single from ids where namespace equals element namespace
  • if b != null { return b }
  • else return single from ids where namespace equals null

Luke

Community
  • 1
  • 1
Luke Puplett
  • 42,091
  • 47
  • 181
  • 266
  • Pedantry alert: You're not alone in disliking the way XML namespaces are specified, but your characterization is awry. The XML spec says nothing at all about namespaces. The XML namespaces spec says that unqualified names are not recognized as belonging to any namespace by namespace-compliant software -- it does *not* say that they "are in no namespace". And most tools in the XML stack treat unqualified names as being in a namespace distinct from all others. – C. M. Sperberg-McQueen Aug 28 '14 at 18:26

1 Answers1

2

If id is defined in both default.ns and humanresources.ns but with different data types, say xs:token and xs:integer which namespace will id be resolved to for validation, if either?

It won't be "resolved to" anything. Your example XML

<document xmlns="default.ns" xmlns:hr="humanresources.ns">
  <hr:user id="abc" />
</document>

has

  • a root-level element with local name document in namespace default.ns, with
  • a single child element with local name user in namespace humanresources.ns, with
  • an attribute with local name id in no namespace

Whether or not this is valid depends on the definition of the type of the user element in the humanresources schema. If that specifies that the user element is allowed an id attribute that is not in a namespace then it's valid regardless of whether or not either schema declares a global attribute named id that is in the corresponding namespace.

By default the same rules apply to attributes as to elements in an XML schema - top-level "global" attribute (and element) declarations belong to the schema's targetNamespace, but "local" attribute (and element) declarations that are nested inside a complexType do not belong to any namespace. This is controllable using the form attribute on a single declaration, and/or the elementFormDefault and attributeFormDefault on the whole schema. So a schema of

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
           targetNamespace="humanresources.ns" xmlns:tns="humanresources.ns">
  <xs:element name="user">
    <xs:complexType>
      <xs:attribute name="id" type="xs:string" />
    </xs:complexType>
  </xs:element>
</xs:schema>

declares a user element in the schema's targetNamespace with a required attribute named id in no namespace, whereas

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
           targetNamespace="humanresources.ns" xmlns:tns="humanresources.ns">
  <xs:attribute name="id" type="xs:string" />
  <xs:element name="user">
    <xs:complexType>
      <xs:attribute ref="tns:id" />
    </xs:complexType>
  </xs:element>
</xs:schema>

(with the attribute declared at the top level) would mean the user element requires an id attribute that is in the humanresources.ns namespace, i.e.

  <hr:user hr:id="abc" />
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • This is greatly helpful, thanks. Regarding your first schema example above with local attribute in no namespace, then would this validate?: `... ` Here, `id` is qualified but if I understand correctly, there is no `id` attribute in the schema/namespace (only the local one). – Luke Puplett Aug 28 '14 at 16:58
  • @LukePuplett no, it would not be valid. The namespace is part of the name of an element or attribute, a syntax you sometimes see to describe this is `{namespace}localname` which may be a clearer way to think about it (though it's not a syntax you can use directly in XML). My first version of the schema expects an attribute named `{}id` but this xml has `{humanresources.ns}id` instead. – Ian Roberts Aug 28 '14 at 17:21