This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
XSLT stylesheet highlighter / whitespace with output method text
- To: xsl-list at mulberrytech dot com
- Subject: XSLT stylesheet highlighter / whitespace with output method text
- From: rolf at pointsman dot de
- Date: Sun, 19 Mar 2000 04:01:35 +0100 (MET)
- Reply-To: xsl-list at mulberrytech dot com
Just the other day I got curios. XSLT is capable to convert XML data
into HTML. XSLT stylesheets are XML data. Isn't it possible, to write
a XSLT stylesheet, that convert XSLT stylesheet into some kind of HTML
source code listing with some basic highlighting?
I didn't found anything about this topic within the web. Only Mike J
Browns Fancy XML Tree Viewer is some kind of related, but this clever
work has his focus on introspection.
I've appended my attempt. I don't claim that this stylesheet is
clever or even works as pronounced. It hasn't any real practical
importance. There are a obviously some limitations. But it's nice.
This stylesheet tries to reproduce a XSL stylesheet as much as possible
as a HTML document and adds some simple highlighting. It should be
not to hard to write a similar simple XHTML highlighting XSL
stylesheet. For example (and if you use James Clarks xt) you could say
xt xsl_highlight.xsl xsl_highlight.xsl xsl_highlight.html
and you get a HTML representation of the xsl_highlight.xsl stylesheet,
that (nearly) looks like the stylesheet viewed with a text editor.
For my attempt the whitespace stripping rules of the used XSLT
processor obviously are crucial . In theory, every conformant XSLT
processor should produce (maybe nearly) the same HTML output file -
maybe not the one I expect, nevertheless. In reality this isn't
true. I tried xt110599, saxon 5.2, xalan 0.20 and
oracle-xmlparser_v2_0_2_7. Well, I got 4 different results. The xt
output is the only one that looks satisfactory.
You may say whitespace isn't mostly significant within HTML and your
basicly true. But there is this "mostly" - and this makes a big
difference in some cases, not only within <pre></pre> elements.
There is an amusing detail. Change the xsl:output method of this
stylesheet to "text" and xt and saxon produces very similar - but, of
course, very senseless - output files.(I haven't checked with xalan
and oraxsl, sorry for that, this beasts are all incompatible, it's a
mess.) Complete control over the whitespace within the result document
seems possible, according to the "text" results. Come one, have a
heart for the purists and do this for the HTML output also.
Greetings
rolf
<?xml version='1.0' encoding='ISO-8859-1'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<!-- The main template. Builds a basic html Skeleton and calls the rest -->
<xsl:template match="/">
<html>
<head>
<title>Highlighted xsl Code</title>
</head>
<body bgcolor="#ffffff">
<pre>
<xsl:apply-templates select="*|text()|comment()|processing-instruction()"/>
</pre>
</body>
</html>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="comment()">
<xsl:text><!--</xsl:text>
<xsl:element name="i">
<xsl:value-of select="."/>
</xsl:element>
<xsl:text>--></xsl:text>
</xsl:template>
<xsl:template match="processing-instruction()">
<font color="red"><i><xsl:value-of select="."/></i></font>
</xsl:template>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="*|text()|comment()|processing-instruction()"><xsl:call-template name="notempty"/></xsl:when>
<xsl:otherwise><xsl:call-template name="empty"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="notempty">
<xsl:choose>
<xsl:when test="substring(name(.),1,3)='xsl'">
<xsl:element name="b">
<xsl:text><</xsl:text>
<xsl:element name="font">
<xsl:attribute name="color">blue</xsl:attribute>
<xsl:value-of select="name(.)"/>
</xsl:element>
</xsl:element>
<xsl:call-template name="stylesheedattributes"/>
<xsl:element name="b">
<xsl:text>></xsl:text>
</xsl:element>
<xsl:apply-templates/>
<xsl:element name="b">
<xsl:text></</xsl:text>
<xsl:element name="font">
<xsl:attribute name="color">blue</xsl:attribute>
<xsl:value-of select="name(.)"/>
</xsl:element>
<xsl:text>></xsl:text>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:text><</xsl:text>
<xsl:value-of select="name(.)"/>
<xsl:call-template name="attributes"/>
<xsl:text>></xsl:text>
<xsl:apply-templates/>
<xsl:text><</xsl:text>
<xsl:value-of select="name(.)"/>
<xsl:text>></xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="empty">
<xsl:choose>
<xsl:when test="substring(name(.),1,3)='xsl'">
<xsl:element name="b">
<xsl:text><</xsl:text>
<xsl:element name="font">
<xsl:attribute name="color">blue</xsl:attribute>
<xsl:value-of select="name(.)"/>
</xsl:element>
</xsl:element>
<xsl:call-template name="stylesheedattributes"/>
<xsl:element name="b">
<xsl:text>/></xsl:text>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:text><</xsl:text>
<xsl:value-of select="name(.)"/>
<xsl:call-template name="attributes"/>
<xsl:text>/></xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="stylesheedattributes">
<xsl:for-each select="attribute::*">
<xsl:text> </xsl:text>
<xsl:element name="b">
<xsl:value-of select="name(.)"/>
</xsl:element>
<xsl:text>="</xsl:text>
<xsl:element name="font">
<xsl:attribute name="color">blue</xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
<xsl:text>"</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template name="attributes">
<xsl:for-each select="attribute::*">
<xsl:text> </xsl:text>
<xsl:value-of select="name(.)"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>