Kirk Evans Blog

.NET From a Markup Perspective

Displaying XML data: defaultss.xslt

As noted in yesterday’s blog entry, I was looking for a means to “colorize” XML documents so that I could include XML within my blog.  I knew about Andrew Welch’s XSLT version of defaultss.xslt, but that stylesheet did a little more than I wanted.  I simply wanted the generic color-coding capabilites for any XML document, but I didn’t want JavaScript capabilities in the output.  Further, the styles had to be inlined (including the STYLE element in this RSS feed caused problems on several readers).  It removes the commented template rules of Andrew’s stylesheet, inlines the styles, and removes the JavaScript capabilites. 

What I came up with still has some shortcomings.  XSLT cannot match the xmlns attribute of an element, it is up to the XSLT parser to include it in the output.  Without this capability, I cannot see a way to render the namespace declarations accurately.  Second, character entities are escaped into their HTML equivalent when the stylesheet is displayed.  When your HTML rendering requires “&lt;” to be visually rendered, it will be displayed with its character equivalent of “<“.  A workaround is to escape the escape, using &amp;lt;. 

To download the stylesheet, see http://xmlandasp.net/examples/xslt/defaultss.xslt.