Menu

Processing xslthl results

Michiel Hendriks

Processing xslthl results

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.

Node names

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:

  • keyword; for keywords in the language
  • string; for string literals
  • number; for numerical literals
  • comment; for any type of comment
  • doccomment; comments used as documentation (i.e. javadoc, or xmldoc)
  • directive; for (pre)processor directives
  • annotation; for annotations (or "attributes" as they are called in .NET)

The XML highlighter contains some hardcoded style/node names (which can also be used by other highlighters):

  • tag; for the xml tags/elements
  • attribute; the the attributes within the tags
  • value; use for the values of the attributes
  • comment
  • directive; used for the processing instructions
  • doctype; used for the DOCTYPE element, and all its content.

Non-xslthl elements

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.

Nested highlighters

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">
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Example of mixing different languages&lt;title&gt;
&lt;head&gt;
&lt;body&gt;
    &lt;H1 style="page-header"&gt;Hello World&lt;/h1&gt;
    <code language="php">&lt;?php
    /*
     * HTML document using the default xml highligter
     * with a nested code block using the php syntax highlighter
     */
    $string = "Hello universe";
    echo $string;
    ?&gt;</code>
&lt;/body&gt;
&lt;/html&gt;
</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) &gt; 0">
                <!-- prevent starting a new "pre" part when it's already highlighted -->
                <xsl:copy-of select="$result" />
            </xsl:when>
            <xsl:otherwise>
                &lt;pre&gt;
                    <xsl:copy-of select="$result" />
                &lt;/pre&gt;
            </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.


Related

Wiki: Home
Wiki: Syntax Highlighters
Wiki: Usage