Re: [xsl] rendering xml fragment tree as syntax highlighted HTML

Subject: Re: [xsl] rendering xml fragment tree as syntax highlighted HTML
From: Antony Quinn <aquinn@xxxxxxxxx>
Date: Fri, 12 Jan 2007 16:08:24 +0000
Christiane,

Here's how I did it:

<xsl:apply-templates select="$source" mode="escape"/>

...

<xsl:template match="node()" mode="escape">
&lt;<xsl:value-of select="name()"/><xsl:apply-templates select="@*" mode="escape"/>&gt;
<xsl:apply-templates select="node()" mode="escape"/>
&lt;/<xsl:value-of select="name()"/>&gt;
</xsl:template>


<xsl:template match="@*" mode="escape">&#160;<xsl:value-of select="name()"/>="<xsl:value-of select="."/>"</xsl:template>

    <xsl:template match="text()" mode="escape">
        <xsl:value-of select="."/>
    </xsl:template>

    <xsl:template match="comment()|processing-instruction()" mode="escape">
        <xsl:apply-templates select="node()" mode="escape"/>
    </xsl:template>

It works, but I'm not very happy with the result [1] because there's lots of white space, and empty elements are not rendered well (for example <ns2:parameter factor="8" term="IdCounter"/> becomes <ns2:parameter factor="8" term="IdCounter"></ns2:parameter>).

See below for links to the XML [2] and XSLT [3].

Any advice of how to improve this would be much appreciated.

Thanks,

Antony

[1] Click "[show]" by "Advanced options" to see XML in "Result" box:
http://tinyurl.com/vo8un

[2] http://tinyurl.com/v9mg6

[3] I use style-free stylesheets [4] for some of the rendering:
http://tinyurl.com/yxa8yw

[4] http://www.xml.com/pub/a/2000/07/26/xslt/xsltstyle.html

Christiane Fritze wrote:
Hi all,

I want to copy a fragment tree from my source xml document using <xsl:copy-of/> to the result html document.
This xml fragment should be rendered als well known code2html tools does it for perl code etc.
It means at least all < and > have to be transformed into &lt; and &gt;


But how I have to handle my <xsl:copy-of> fragment to get the wanted result?


<!-- my XML -->


<?xml version="1.0" encoding="UTF-8"?>
<div0>
<entry key="Capriccio">
<form>
<orth>Capriccio</orth>
</form>
<sense>
<gramGrp>n.</gramGrp> virtuoses, einfallsreiches <seg function="Mu|sikst">Musikst</seg>
<lb/> ueck, Uebernahme (18. Jh.) von gleichbed.<lb/> ital. <hi rend="i">capriccio</hi>,
eigentl. <def>Laune, Grille, <seg function="Ein|fall">Einfall</seg>
<lb/> </def>; s. <hi rend="i">Kaprice</hi>.<lb/>
</sense>
</entry>
</div0>



<!-- my XSL -->


?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">
<xsl:output method="html" media-type="text/html"/>
<xsl:template match="div0">
<html>
<title>show XML tree</title>
<body>
<xsl:apply-templates mode="rendered"/>
<hr />
<xsl:apply-templates mode="structured"/>
</body>
</html>
</xsl:template>


<!-- rendering as a dictionary like layout -->
<xsl:template match="entry" mode="rendered">
<!-- some code, works fine -->
</xsl:template>

<!-- show the user the original xml code -->
<xsl:template match="entry" mode="structured">
<xsl:copy-of select="."/>
<tt>
<!-- ?? HOW to handle this result tree so that it can be shown in html? -->
</tt>
</xsl:template>
</xsl:stylesheet>


<!-- WANTED OUTPUT -->


<html>
<title>show XML tree</title>
<body>
<!-- some dictionary like rendered output -->
<tt>
&lt;entry key="Capriccio"&gt;<br />
&lt;form&gt;<br />
&lt;orth&gt;Capriccio&lt;/orth&gt;<br />
&lt;/form&gt;<br />
&lt;sense&gt;<br />
&lt;gramGrp&gt;n.&lt;/gramGrp&gt; virtuoses, einfallsreiches &lt;seg function="Mu|sikst"&gt;Musikst&lt;/seg&gt;<br />
&lt;lb/&gt; ueck, Uebernahme (18. Jh.) von gleichbed.&lt;lb/&gt; ital. &lt;hi rend="i"&gt;capriccio&lt;/hi&gt;,<br />
eigentl. &lt;def&gt;Laune, Grille, &lt;seg function="Ein|fall"&gt;Einfall&lt;/seg&gt;<br />
&lt;lb/&gt; &lt;/def&gt;; s. &lt;hi rend="i"&gt;Kaprice&lt;/hi&gt;.&lt;lb/&gt;<br />
&lt;/sense&gt;<br />
&lt;/entry&gt;<br />
</tt>
</body>
</html>


If the shown xml code is somehow colored it would be perfect.

Many thanks for any hint,

Regards,
Christiane

Current Thread