[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Namespaces Best Practice - some working code!

  • From: Evan Lenz <elenz@x...>
  • To: Jonathan Borden <jborden@m...>, Tony.Coates@r...,xml-dev@l...
  • Date: Fri, 21 Sep 2001 12:44:46 -0700

node set namespaces
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.


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.