james.garriss
james.garriss

Reputation: 13406

Why does XML Schema allow a QName to be the value of an element?

In How do I find all text nodes in an XML document with a namespace using XPath?, SO users asserted that text nodes in an XML document cannot be namespace qualified. The XPath 1.0 spec and this doc on QNames agree with their assertions.

OTOH, consider this XML

<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:bob="http://www.bob.com">
    <Hello>bob:World</Hello>
</Root>

And this XML Schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="Root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Hello" type="xs:QName" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

It declares Hello to be of type QName. If the http://www.bob.com namespace is removed from the XML document, it won't validate.

Want to see a real world example that does this? The faultcodes in the SOAP 1.2 spec are one that I've run across.

So here's my questions:

  1. Why does XML Schema allow a QName to be the value of an element? It seems that this contradicts other specs.
  2. Is it fair to say that even though XML parsers do not understand a text node to be namespace qualified, other processors, such as schema validators and SOAP processors, do?

Upvotes: 3

Views: 3410

Answers (1)

Michael Kay
Michael Kay

Reputation: 163587

You are confusing concepts.

Element and attributes have names, and their names can be namespace-qualified. Text nodes do not have names, so their names obviously cannot be namespace-qualified.

Attributes and text nodes can both have QNames as their content. The namespace prefix in both cases is resolved against the in-scope namespaces of the containing element: that is, all the namespaces declared on the element and its ancestors. This is quite unrelated to the name of the element or attribute in question.

You are correct, however, in pointing out that QNames in element or attribute content cannot be detected by the XML parser itself, only by a schema validator. You are also correct to identify this as a problem. It is not, however, a contradiction between the specs. It's entirely consistent with the fact that an XML parser can't recognize that a node contains a number or a date, but a schema validator can.

Upvotes: 4

Related Questions