[xsl] Pretty-print or replace problem

Subject: [xsl] Pretty-print or replace problem
From: Maik Stührenberg <Maik.Stuehrenberg@xxxxxxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 17 Feb 2003 10:44:06 +0100

I have a huge 'replace' related problem. I had a look to the archive and saw some global solutions but none of them did fit. The problem is this:
I have an XML document with some XML sourcecode, marked as <code>...</code>, declared in the DTD as #PCDATA. This code is transformed into XHTML, no problem at all. The next step I tried is to do some kind of 'pretty-print', eg. to format XML keywords like ELEMENT, ATTLIST and so on with colors different from the ones for the name of an element/attribution or its content model (using simply the XHTML span element). Two problems have arisen:
1. How do I match the &lt;?
2. (the bigger one) How do I manage to select the separate text content of the node? I tried it with the XSLT shown below, but there has to be another way.

Any help would be greatly appreciated. Kind regards,

Maik Stührenberg

<xsl:template match="Code">
<span style="font-family:monospace">
<xsl:call-template name="pretty"/>

<xsl:template name="pretty">
<xsl:variable name="lt" select="substring-before(text()[contains(.,'E')] | text()[contains(.,'A')],'!')"/>
<xsl:variable name="elem" select="substring-before(substring-after(.,'!'),' ')"/>
<xsl:variable name="name" select="substring-before(substring-after(text()[$elem],' '),'(')"/>
<xsl:variable name="content_model" select="substring-before(substring-after(text()[$name],'('),')')"/>
<xsl:variable name="gt" select="substring-after(text()[$content_model],substring-before(text()[$content_model],'&gt;'))"/>
<xsl:for-each select="text()[contains(.,$elem)]">
<span style="color:green"><xsl:value-of select="$lt"/></span>
<span style="color:red"><xsl:value-of select="$elem"/></span>
<xsl:text> </xsl:text>
<span style="color:black"><xsl:value-of select="$name"/></span>
<span style="color:blue"><xsl:value-of select="$content_model"/></span>
<span style="color:green"><xsl:value-of select="$gt"/></span>

XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list

Current Thread