The highlight function of xslthl returns a set of xml elements. Highlighted source fragments will be wrapped in an XML element with the xslthl prefix. The tag name is determined by the style that was configured for the highlighted code fragment.
Below is an example of an XML node set that the highlight function might return.
myvar = <xslthl:string>"this is a string"<xslthl:string>; <xslthl:keyword>return</xslthl:keyword> myvar;
The highlighted nodes are always simple nodes,they contain a single text node and no attributes.
Within your XSLT stylesheet you simply need to define the templates to process these nodes as desired.
There is no exhaustive list of used node names. Because the node names defines on the style as configured in the syntax highlighter for the given language it could be anything. The styles/node names are commonly used:
The XML highlighter contains some hardcoded style/node names (which can also be used by other highlighters):
The result of the highlight function could also contain non-xslthl elements. Only the text nodes are subject to transformation, all other nodes (like normal XML elements) are passed on without changes. This means that these elements must also be processed in your stylesheet, otherwise they might be copied to the output resulting in unwanted behavior.
Using some tricks you can combine highlighters of different languages to produce a unified result. An example of this can be found in the PHP example where the XML highlighter and PHP highlighter are nested. Consider the following source code:
<code language="xml"> <html> <head> <title>Example of mixing different languages<title> <head> <body> <H1 style="page-header">Hello World</h1> <code language="php"><?php /* * HTML document using the default xml highligter * with a nested code block using the php syntax highlighter */ $string = "Hello universe"; echo $string; ?></code> </body> </html> </code>
It contains two \<code> blocks. One with the language set to xml and one with the language set to php.
Using the following XSLT template for the code block it will nest these elements properly in an HTML document:
<xsl:template match="code"> <xsl:variable name="result"> <xsl:call-template name="syntax-highlight"> <xsl:with-param name="language"> <xsl:value-of select="@language" /> </xsl:with-param> <xsl:with-param name="source" select="." /> </xsl:call-template> </xsl:variable> <xsl:choose> <xsl:when test="count(ancestor::code) > 0"> <!-- prevent starting a new "pre" part when it's already highlighted --> <xsl:copy-of select="$result" /> </xsl:when> <xsl:otherwise> <pre> <xsl:copy-of select="$result" /> </pre> </xsl:otherwise> </xsl:choose> </xsl:template>
The result can be see at the end of this example page. The test expression checks if the current element has \<code> ancestors, if not it will surround the result with \<pre> tags. There are of course other ways to produce a similar unified look.