Reputation: 1409
I am working with an XML file that has raw HTML stored inside a node's attribute (<node data="HTML...">).
I just realized that the HTML is double-encoded, so that, instead of being:
<div>
It is actually written as:
&lt;div&gt;
This means that if I do something like:
<xsl:value-of select="node/@data" disable-output-escaping="yes" />
I will still get a (single) escaped value:
<div>
What's the easiest way of unescaping this once again?
Upvotes: 3
Views: 609
Reputation: 338316
It's definitely not pretty, but basically you are looking at a limited number of string replace operations
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" />
<xsl:variable name="ampDbl" select="'&amp;'" />
<xsl:variable name="amp" select="'&'" />
<xsl:variable name="ltDbl" select="'&lt;'" />
<xsl:variable name="lt" select="'<'" />
<xsl:variable name="gtDbl" select="'&gt;'" />
<xsl:variable name="gt" select="'>'" />
<xsl:template match="/">
<xsl:apply-templates select="//@data" mode="unescape" />
</xsl:template>
<xsl:template match="@data" mode="unescape">
<xsl:variable name="step1">
<xsl:call-template name="StringReplace">
<xsl:with-param name="s" select="string()" />
<xsl:with-param name="search" select="$ltDbl" />
<xsl:with-param name="replace" select="$lt" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="step2">
<xsl:call-template name="StringReplace">
<xsl:with-param name="s" select="$step1" />
<xsl:with-param name="search" select="$gtDbl" />
<xsl:with-param name="replace" select="$gt" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="step3">
<xsl:call-template name="StringReplace">
<xsl:with-param name="s" select="$step2" />
<xsl:with-param name="search" select="$ampDbl" />
<xsl:with-param name="replace" select="$amp" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$step3" disable-output-escaping="yes" />
</xsl:template>
<!-- generic string replace template -->
<xsl:template name="StringReplace">
<xsl:param name="s" select="''" />
<xsl:param name="search" select="''" />
<xsl:param name="replace" select="''" />
<xsl:choose>
<xsl:when test="contains($s, $search)">
<xsl:value-of select="substring-before($s, $search)" />
<xsl:value-of select="$replace" />
<xsl:variable name="rest" select="substring-after($s, $search)" />
<xsl:if test="$rest">
<xsl:call-template name="StringReplace">
<xsl:with-param name="s" select="$rest" />
<xsl:with-param name="search" select="$search" />
<xsl:with-param name="replace" select="$replace" />
</xsl:call-template>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$s" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
When applied to
<root>
<node data="&lt;div&gt;bla &amp;amp; bla&lt;/div&gt;" />
</root>
gives (in source code)
<div>bla & bla</div>
which of course becomes this on screen:
bla & bla
You might want to add a step4
for '&quot;'
to '"'
.
Upvotes: 2