OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [xml-dev] Namespaces Best Practice - some working code!



Jonathan Borden wrote:
> During the parse phase phase, build a map of prefix/namespaceURI bindings.
> Remove all duplicate prefix/namespaceURI bindings by only using the first
> prefix declared on a namespaceURI and renaming any prefix declared on a
> namespaceURI already in the map.
>
> During the serialization phase, emit the prefix/ns bindings at xmlns
> attributes of the root element. As each element/attribute is
> emitted, lookup
> the prefix from the node's namespaceURI in the map.

http://www.xmlportfolio.com/namespaces/normalize-namespaces.xsl
http://www.xmlportfolio.com/namespaces/normalize-namespaces.html
(pretty-printed for easier reading)

I implemented the algorithm you describe, using XSLT and the exsl:node-set()
extension function (which we shouldn't need in a future version of XSLT).
This works fine with SAXON and should work with any other conformant XSLT
processor that also implements the EXSLT "common" node-set() function. To
work with Xalan or other processors that support their own node-set()
function, simply change the namespace URI and possibly name (Xalan's is
"nodeset()") of the function.

The stylesheet itself is commented throughout, but I'd like to make a few
general observations here.

With respect to the XPath data model, this transformation does not simply
produce an alternative serialization, i.e. it is indeed a transformation. In
the XPath data model, prefixes are significant and exactly where namespace
bindings are in scope, is significant. Those and only those two things are
precisely what this transformation (possibly) changes. In other words, if
your data model is not quite as low-level as XPath's, this transformation
indeed provides you an alternate serialization that perhaps is more
human-readable, and, in any case, follows Jonathan's "best practice" #1.
Accordingly, I don't think it will create nearly the controversy that
Simon's SAX filters did :)

Conversely, it must be used with care on XSLT stylesheets, XSDL schemas, and
any other grammar that uses in-scope namespace bindings to expand QNames in
attribute values or element content. It will not necessarily break them, but
it could.

Below is an example of what this transformation does. Note that prefixes can
be removed ("abc") and created ("d0e7").

BEFORE:
<foo xmlns="default1">
  <abc:bar xmlns="default2andfoo" xmlns:abc="default1">
    <bat xmlns:foo="default2andfoo" foo:bar="value">
      <bang xmlns="default3">
        <abc:hi xmlns:xyz="unused-uri" />
      </bang>
    </bat>
  </abc:bar>
</foo>

AFTER (serialized by SAXON):
<foo xmlns="default1" xmlns:bar="default2andfoo" xmlns:d0e7="default3"
xmlns:xyz="unused-uri">
  <bar>
    <bat bar:bar="value">
      <d0e7:bang>
        <hi />
      </d0e7:bang>
    </bat>
  </bar>
</foo>

Questions and comments welcome.

Evan Lenz
XYZFind Corp.