RE: [xsl] Re: pretty-printing XML into HTML

Subject: RE: [xsl] Re: pretty-printing XML into HTML
From: "Lars Huttar" <lars_huttar@xxxxxxx>
Date: Thu, 18 Dec 2003 12:45:02 -0600
> Dimitre Novatchev

> > So... if this is a "normal" thing to do, hopefully somebody has
> > already written a pretty good XML-to-HTML pretty-printer in XSL?
> > It seemed to me there was an extension function in Saxon or in
> > XSLT 2.0 that would serialize a source tree fragment, hopefully
> > with indentation, but I can't find a reference to it right now.
> > Any ideas?
> This has been done years ago. For example look at the code of the
> stylesheets used by the XPath Visualizer (both for IE and
> Mozilla). These
> are adaptations of the original vendors' stylesheets with
> correction of some
> errors -- e.g. namespace definitions and the escaping of &.

Thanks for the pointer. I thought this seemed like something that would
have been done years ago!

The Mozilla version of XPath Visualizer is especially useful as it doesn't
use processor-specific extensions.
(Albeit the output DHTML doesn't work in IE, but I don't need
collapsing tree functionality anyway, so I can just take it out.)

One note on correctness of displaying namespaces:
XPath Visualizer doesn't give correct results for an attribute that's
in a namespace that no ancestor element has (or has for the same prefix).

E.g. the XML document
  <test xmlns:foo="bigrabbit" foo:squid="fish" />
is displayed as
  <test foo:squid="fish"/>
i.e. the foo: prefix is used but never declared.

Another example input for which incorrect output is generated is
  <foo:test xmlns:foo="bigrabbit">
    <bar xmlns:foo="littlerabbit" foo:squid="fish" />
The generated output is:
  <foo:test xmlns:foo="bigrabbit">
    <bar xmlns="" foo:squid="fish"/>
I.e. foo:squid is in the wrong namespace.

Also, the generated namespace declarations are not very elegant when
you have a namespace that's used in many places that are siblings
but is not used on any of their ancestors... it declares the
namespace many times whereas the original XML document would
probably declare it once, on the document element or some other
common ancestor.

As a fix for these two namespace issues, let me suggest the
approach I used in my previous post... go by the existence of
namespace nodes attached to the source tree elements. I believe
this will always give correct results and will usually give much more
readable results than only going by the prefix/ns-uri of each
element and attribute node. (The readability boost is dependent on
the assumption that the data model includes a namespace node on
every element for which a given namespace declaration is in scope
in the original XML markup, not just those for which the namespace
declaration is needed. The XPath 1.0 spec, section 5.4, says
this is true.
But even if this assumption is false, correctness is not affected,
because we know the data model must include namespace nodes
at least where they're needed.)
The only possible disadvantage is that if the input XML document contains
superfluous namespace declarations, so will the output. But the
output will be no worse than the input. And that's pretty good
in the circumstances where I've tried it.

I may go ahead and make changes like this to myDefaultss80.xsl and
submit it to you.

By the way, a few (actual or possible) textual errata I ran across
in the process of looking up XPath Visualizer:

There's a broken link on the XPath Visualizer page
should be

Also, the Readme.txt file for the Mozilla version of XPath Visualizer
1.4 (which I found at

  This is a customisation of the original XMLPrettyPrint.xsl
  written by Jonas Sicking <sicking@xxxxxxxxxxx>.

I can't find references to any XSL file by that name, but there
is a file called XMLPrettyPrint.xml (note extension)
by Jonas Sicking, at
Maybe the Readme.txt file needs to be corrected?

Lastly, the file XPathMainNS.htm (in the Mozilla version)
has a title of "XPath Visualiser Ver. 1.0" (note both spelling and
version number) -- whereas the Readme.txt file says the latest
version is 1.4. Presumably XPathMainNS.htm needs to have its title


 XSL-List info and archive:

Current Thread