I created this XSL document for my old website to display various XML and XSL files that I had created. It effectively adds markup to the document so that the various parts appear in different styles, governed by an external stylesheet. I have stuck to just changing the colours. The layout of the document is roughly preserved. The <?xml ?> line at the beginning is lost, as is the DOCTYPE declaration. Also lost are any text nodes outside the root element. These are added in by the stylesheet. The XSL1 specification does not allow for the creation of ‘identity’ templates, as all entites are resolved by the XML processor before they reach the XSL template.
How It Works
The initial template matches the document node. It first looks for comments, using the prolog mode comment template, which adds a newline after every comment. Thus any comments at the bottom of the source will appear at the top. It then selects either the root element, or any processing instructions.
The attributes are dealt with by the match="@*" template and comments are dealt with by the match="comment()". The match="text()" sorts out all the text, and displays it all, including whitespace to ensure that the document is laid out as it is in the source.
The XSL document seems to manage OK with my documents, which are all laid out in a similar style, however I don’t think it could cope with all documents. The version used to create the old website differed only in that it can be called from other XSL sheets so that I can put a line into my HTML source such as <xml2xhtml root="yes" href="cv2pdf.xsl" /> and the code will be inserted.
The XSL File
This is the file, having been processed by the Syntax Ruby Gem. I don’t use XSL on this website at all anymore. You can Download it.
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt" xmlns="http://www.w3.org/1999/xhtml" version="1.0"> <xsl:output omit-xml-declaration="no" method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" xalan:indent-amount="1" indent="no" encoding="iso-8859-1" /> <xsl:template match="/"> <html> <head> <link rel="stylesheet" href="convert.css" /> </head> <body> <pre> <xsl:apply-templates select="comment()" mode="prolog" /> <xsl:apply-templates select="* | processing-instruction()" /> </pre> </body> </html> </xsl:template> <xsl:template match="*"> <xsl:text><</xsl:text> <xsl:call-template name="showName"> <xsl:with-param name="name" select="name()" /> </xsl:call-template> <xsl:apply-templates select="@*" /> <xsl:choose> <xsl:when test="node()"> <xsl:text>></xsl:text> <xsl:apply-templates /> <xsl:text><</xsl:text> <xsl:text>/</xsl:text> <xsl:call-template name="showName"> <xsl:with-param name="name" select="name()" /> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:text>/</xsl:text> </xsl:otherwise> </xsl:choose> <xsl:text>></xsl:text> </xsl:template> <xsl:template name="showName"> <xsl:param name="name" /> <xsl:choose> <xsl:when test="substring-before($name, ':')"> <span class="namespace"> <xsl:value-of select="substring-before($name, ':')" /> </span> <xsl:text>:</xsl:text> <span class="element"> <xsl:value-of select="substring-after($name, ':')" /> </span> </xsl:when> <xsl:otherwise> <span class="element"> <xsl:value-of select="$name" /> </span> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="processing-instruction()"> <span class="process"> <xsl:text><?</xsl:text> <xsl:value-of select="name()" /> </span> <xsl:text> </xsl:text> <span class="text"> <xsl:value-of select="." /> </span> <xsl:text>?></xsl:text> </xsl:template> <xsl:template match="@*"> <xsl:text> </xsl:text> <span class="attribute"> <xsl:value-of select="name()" /> <xsl:text>=</xsl:text> </span> <span class="text"> <xsl:text>"</xsl:text> <xsl:value-of select="." /> <xsl:text>"</xsl:text> </span> </xsl:template> <xsl:template match="text()"> <span class="text"> <xsl:value-of select="." /> </span> </xsl:template> <xsl:template match="comment()"> <span class="comment"> <xsl:text><!--</xsl:text> <xsl:value-of select="." /> <xsl:text>--></xsl:text> </span> </xsl:template> <xsl:template match="comment()" mode="prolog"> <span class="comment"> <xsl:text><!--</xsl:text> <xsl:value-of select="." /> <xsl:text>--></xsl:text> </span><xsl:text> </xsl:text> </xsl:template> </xsl:stylesheet>