An XSLT Stylesheet for Listing Namespace Values

Bob DuCharme
August 11, 2003

This was inspired by a comment that Jon Udell made in a 2003-08-11 Jon's Radio posting:

I guess this all further illustrates the point of my column, which is that learning to ride the XML namespace bicycle requires a bit of help. An example of the kind of thing I have in mind is this tutorial by Mark Priest, which enumerates the namespace assignments in a sample document under various conditions. It strikes me that an online service that reports such assignments for any uploaded document would be extremely helpful.

It was also inspired by a very nice comment that Margarita Isayeva made on the Amazon page for my book "XSLT Quickly" about the book's coverage of namespace wrangling. For excerpts from this coverage, see my April 2001 and May 2001 columns for XML.com.

The nscomments.xsl ("namespace comments") stylesheet copies an XML document, adding a comment before each element's start-tag that lists the namespace URLs of the element and any attributes it has. I tried to fix the whitespace of both the stylesheet and its output to be as readable as possible. For the stylesheet, this meant declaring a carriage return entity, which makes it difficult to display the stylesheet in IE. (Mozilla has no problem with it.) For the output, the spacing still doesn't look that great, and I'm open to suggestions.

It probably would be simplest to download the nscomments.xsl stylesheet and run it locally, but if you want to run it as an online service as Jon describes, you can use the W3C XSLT Service. For example, I stored a copy of the example document from the Tim Bray ongoing posting that inspired Jon's posting. To see my stylesheet applied to Tim's document with the W3C service, click here.