[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [xml-dev] Namespaces Best Practice - some working code!
- From: Evan Lenz <firstname.lastname@example.org>
- To: Jonathan Borden <email@example.com>, Tony.Coates@reuters.com,firstname.lastname@example.org
- Date: Fri, 21 Sep 2001 12:44:46 -0700
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.
(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
Below is an example of what this transformation does. Note that prefixes can
be removed ("abc") and created ("d0e7").
<abc:bar xmlns="default2andfoo" xmlns:abc="default1">
<bat xmlns:foo="default2andfoo" foo:bar="value">
<abc:hi xmlns:xyz="unused-uri" />
AFTER (serialized by SAXON):
<foo xmlns="default1" xmlns:bar="default2andfoo" xmlns:d0e7="default3"
Questions and comments welcome.