0

I have an XML document with a default namespace. I wasn't able to XPath query on it like /someroot/somechild until I discovered that I had to map the namespace to a prefix, say x, and use that prefix /x:someroot/x:somechild. This is all fine and well until I want to query for elements having some attribute, in which case /x:someroot/x:somechild[@x:someattribute] doesn't work, but /x:someroot/x:somechild[@someattribute] does. With no namespace prefixes defined in the XML document, I would expect every node, element, attribute, or otherwise, to inherit the default namespace. It seems, rather, that elements have inherited the default namespace, but attributes have not. Granted, my understanding of XML is quite limited, so what am I missing?

I am using a .NET XmlDocument object as my document to query on, an XmlNamespaceManager object to map the document's default namespace to a prefix, and the SelectSingleNode(String, XmlNamespaceManager) method to query on the document.

3
  • Possible duplicate of XML Default namespaces for unqualified attribute names? Sep 28, 2016 at 18:30
  • 1
    When you ask "why?", what kind of answer are you expecting? Do you want to know the rationale given by the members of the XML Namespaces working group at the time they made the design decision? Or do you want some kind of third-party rationale as to why this might have been a reasonable decision to make? Or do you simply want a pointer to the rules in the spec that say, yes, this is the way it works? Sep 28, 2016 at 19:41
  • Is a dup, but I got an answer I like. What would you recommend I do?
    – Void Star
    Sep 28, 2016 at 22:53

1 Answer 1

4

This is what the Namespaces spec has to say on the matter:

Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.

No rationale is given as to why the authors of the spec made this decision.

It has been pointed out that this statement is in fact very fuzzy. It doesn't say, for example, that unprefixed attributes are in no namespace, and it doesn't say that they are in the same namespace as the containing element, but both interpretations could reasonably be adopted. For a definitive statement you have to look at the XPath 1.0 specification which states unequivocally:

The namespace URI of the attribute's name will be null if the QName of the attribute does not have a prefix.

Again, no rationale is given (it's not usual for specifications to give a rationale unless the editors felt a strong need to justify themselves). But the editor of the XPath 1.0 spec was James Clark, and James Clark has written a tutorial here

http://www.jclark.com/xml/xmlns.htm

which gives clues as to his thinking about namespaces in general; but no rationale for this particular decision, which he simply restates:

Note that the xmlns attribute does not affect unprefixed attribute names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.