简体   繁体   中英

Undeclaring namespaces in XML 1.1 and the “Prefix Declared” namespace constraint

Example

Apparently, this is a valid XML document (copied from http://www.lenzconsulting.com/namespaces/ ).

<?xml version="1.1"?>
<my:doc xmlns:my="http://xmlportfolio.com/xmlguild-examples">
<simple xmlns:my="">
  <remark>We don't use namespaces.</remark>
</simple>
</my:doc>

And yet it appears to violate the "Prefix Declared" namespace constraint in the Namespace 1.1 spec .

Namespace constraint: Prefix Declared

The namespace prefix, unless it is xml or xmlns, must have been declared in a namespace declaration attribute in either the start-tag of the element where the prefix is used or in an ancestor element (ie an element in whose content the prefixed markup occurs). Furthermore, the attribute value in the innermost such declaration must not be an empty string.

Notice the last sentence ' the attribute value in the innermost such declaration must not be an empty string ' and how it applies to the my: undeclaration in the simple element of the document above. Surely this is the innermost such declaration?

My reading of this, is that you cannot undeclare a prefix, unless you have a child element which re-declares it.

Question

  1. Is the above XML document valid with respect to Namespaces 1.1?
  2. If so, how have I misinterpreted the spec? Can the answer give an example and counter-example of meeting and violating this constraint?
  3. If not, what is the value of undeclaration?
  1. Yes, it is valid (technically, "namespace-well-formed") according to Namespaces 1.1

  2. The rule you cite is referring to the validity of element and attribute names such as my:element or my:attribute, and it's a rule concerning the validity of the prefix "my" in such a name. You don't have any element or attribute with the prefix "my" for which the namespace declaration xmlns:my is the innermost declaration, so you haven't violated the rule.

  3. There's very little value in undeclaration. It was a big mistake: a painful addition to the spec to solve a tiny problem. The tiny problem was "namespace pollution": if you wrap some XML payload inside a SOAP envelope, for example, then the SOAP namespaces permeate your payload, and when you extract the payload, it's hard to get rid of the SOAP namespaces. Namespace declaration was intended to be a solution to that problem; but it's hardly ever used, because in practice, everyone ignores unnecessary unused namespaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM