Re: xslt idea: namespace-coloured XML prettyprinter?

Dan Brickley <danbri@w3.org> wrote:

> Would it be possible to implement the following using XSLT?

You know I can't resist a challenge. ;) 
Jump to bottom for a solution that almost does it. Read on to know
why almost.

Writing an XML serialiser in pure XSLT is almost possible.  You can
get most of what you want, with a few exceptions like namespace
declarations, since the XPath data model doesn't store them. Instead
it stores with every element a list of all the namespace URIs that are
in scope on this element. You could serialise those, but it means you
have namespace declarations on every single element. So the example
stylesheet below doesn't produce them at all (and you may have noticed
that mozilla 1.3 doesn't either, when it displays XML. This may be
because they use XSLT to do it, just like IE, except that IE cheats
and uses XSLT extensions to find the original namespace decls).

There's a way around that, but you have to implement the "namespace
fixup" algorithm to factor out the redundant declarations. Not
impossible, but even that doesn't ensure you get the original
namespace declarations, only the less redundant way to declare them.

Another thing that you are not certain to get in the output (although
most processors do it right) are the namespace prefixes, since they are
not stored in the xpath data model either, and a processor could just
make them up as it outputs the node names.

Bar those, I think you can pretty much get what you want. Look at 

http://www.w3.org/2003/02/colour-xml-serializer.xsl
http://www.w3.org/2003/02/rssexample.html

Cheers,

Max.

Received on Tuesday, 25 February 2003 05:18:30 UTC