2

I'm working with XML files that uses xml:lang, now someone send me a file with this instead:

<element xmlns:ns1="xml" ns1:lang="fr" /> 

Should I treat this as equal to this?

<element xml:lang="fr" /> 

Or should I treat the file as invalid when the xml:lang is required? If the first, can I capture this 'proxying' of the xml namespace in a XSD?

G_H
  • 11,739
  • 3
  • 38
  • 82
Vincent
  • 324
  • 1
  • 8

2 Answers2

2

The two examples you give are not equivalent. A namespace is a URI, and such URIs can be bound to prefixes for defining fully-qualified names for elements and attributes. The URI does not need to point to an existing resource and XML processors are not expected to do any sort of resolution of the URI or otherwise create some canonical representation of it (apart from some attribute value normalization). Indeed, a namespace is actually little more than a case-sensitive string, but URIs just happen to have been chosen as their representation.

In other words, this

<element xmlns:ns1="xml" ns1:lang="fr" /> 

binds prefix ns1 to namespace xml (which is not a legal URI reference). The lang attribute is then in namespace xml since it's qualified with the ns1 prefix.

So why is this not the same as your second example? Because in

<element xml:lang="fr" /> 

the xml is the prefix, not the namespace. This prefix is implicitly bound to namespace http://www.w3.org/XML/1998/namespace as per the specification regarding reserved prefixes and namespace names.

Look further into that spec and you'll also find that not only are you not allowed to bind the xml prefix to any other namespace (makes sense) but you're also not allowed to bind the http://www.w3.org/XML/1998/namespace namespace to any other prefix (makes less sense, but allows for some XML processing optimization and assumptions).

In conclusion, you should treat the first input as invalid if the xml:lang attribute is required, because the attribute is not present in it. Whoever constructed that file has failed to understand XML namespaces.

I go more in-depth into the concepts in this answer.

G_H
  • 11,739
  • 3
  • 38
  • 82
1

No.

Background: The xml:lang attribute is defined in the W3C XML Recommendation without dependence on XML namespaces, despite its use of a colon (:). In the W3C XML Namespaces Recommendation, the xml prefix (and all namespace prefixes beginning with xml) are declared to be reserved. The xml prefix itself is automatically defined to be bound to http://www.w3.org/XML/1998/namespace.

Therefore, xml:lang="fr" is entirely proper and can be used without declaration of the xml namespace prefix. If you did want to declare the xml namespace prefix, it would be xmlns:xml=http://www.w3.org/XML/1998/namespace.

Furthermore, xmlns:ns1="xml" ns1:lang="fr" suggests a misunderstanding of namespace prefix declarations by implying that namespace prefix declarations can be transitively assigned; they cannot.

So although

<element xmlns:ns1="xml" ns1:lang="fr" />

is well-formed (see well-formed vs valid), it is not equivalent to

<element xml:lang="fr" /> 
kjhughes
  • 106,133
  • 27
  • 181
  • 240