cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tobia <>
Subject Re: REPOST: HTML Tags from a database field
Date Thu, 10 Jan 2008 12:24:54 GMT
Derek Hohls wrote:
> Tobia wrote:
>> I use the HTMLTransformer for that purpose, with a couple of XSLT  
>> stylesheets before and after it.  The first one writes <unescape>  
>> tags around each piece of escaped  HTML.  Then the HTMLTransformer  
>> is instructed to only transform text nodes inside those tags.   
>> Finally a second piece of XSLT gets rid of the <unescape> tags  
>> along with the fake <body> and <html> inserted by the transformer,  
>> and does a bit of magic with &amp; and &lt; characters
> Whew!  Sounds like a lot of work for what I thought would be a  
> simple config issue.   Any chance you could make the stylesheets  
> available - maybe upload them to the wiki?

It's not much work really.

First identify the text nodes that need unescaping and wrap them in  
<unescape> tags.
For me that was all text nodes under a field tag, containing either an  
"&" or a "<":

<xsl:template match="sql:row/*[not(self::sql:rowset)]">
     <xsl:when test="contains(., '&amp') or contains(., '&lt;')">
         <xsl:copy-of select="node()"/>
       <xsl:copy-of select="node()"/>

<xsl:template match="@*|node()" priority="-1">
   <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>

Then put it in the pipeline followed by the HTML transformer:

<map:transform src="unescape-pre.xsl">
<map:transform type="html">
   <map:parameter name="tags" value="unescape"/>
<map:transform src="unescape-post.xsl">

The latter XSLT cleans up unnecessary elements:

<xsl:template match="unescape">
   <xsl:apply-templates select="html/body/node()"/>

<xsl:template match="@*|node()" priority="-1">
   <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>

In the last step I also needed to replace "&" with "&amp;" and "<"  
with "&lt;" (Java string notation) in all text nodes under <unescape>,  
by calling a utility Java function from within XSLT. But that's not an  
elegant solution and is probably related to my particular pipeline  
setup, so I won't bother you with the details unless you have the same  


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message