21

I need to unescape XML characters from inside of XML nodes with the help of only XSLT transformations. I have <text>&lt;&gt;and other possible characters</text> and need to get it as valid formatted HTML when I place it inside of the body tag.

2
  • I'm not sure I understand... &lt; and &gt; are valid HTML. Are you saying you want to turn them into parts of HTML tags, e.g. transform "&lt;i&gt;foo&lt;/i&gt;" into "<i>foo</i>"?
    – wdebeaum
    Mar 17, 2010 at 14:53
  • I just tried to do this in XSL 1.0, and it turned out to be really nasty (2.0 might be a little better, but it's not widely supported). Unless you can do the disable-output-escaping trick, you basically have to parse the HTML in each text node and recursively build elements using <xsl:element> and <xsl:attribute>. Assuming this is in a web browser, maybe you could do it in JavaScript instead (or even on the server side)?
    – wdebeaum
    Mar 17, 2010 at 18:30

4 Answers 4

48
<xsl:template match="text">
  <body>
    <xsl:value-of select="." disable-output-escaping="yes" />
  </body>
</xsl:template>

Note that the output is not guaranteed to be well-formed XML anymore.

3
  • 4
    I would also note that disable-output-escaping is an optional serialization feature. It is not supported for instance by the XSLT processor used in Mozilla browsers like Firefox as those directly render the result tree and don't serialize it. Mar 17, 2010 at 17:28
  • 2
    hello when <br/> tag occurred in string then disable-output-escaping="yes" its not working. Aug 6, 2014 at 5:28
  • 1
    Thanks! I used this answer to convert tens of MB's of escaped XML, put within an CDATA wrapper, back to be processable XML (and also make it human readable). Much to my surprise this is also 'blazingly fast' comparing to 'search and replace' macro's in other programs.
    – Piemol
    Jan 6, 2022 at 16:24
2

With xslt 2.0 I have come up with this one. Note that the output is not guaranteed to be correct xml, a simple unequality can mess up your output.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:character-map name="a">
    <xsl:output-character character="&lt;" string="&lt;"/>
    <xsl:output-character character="&gt;" string="&gt;"/>
</xsl:character-map>

<xsl:output use-character-maps="a"/>

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

</xsl:stylesheet>
1

I haven't found an answer to this question. So I came to the conclusion that this is no way to do this. I found workaround for this problem, unescaping file on server side.

4
  • Erm... What's wrong with my answer? This is the way to do what you ask for.
    – Tomalak
    Mar 23, 2010 at 8:10
  • Your suggestion disables output escaping and I needed to unescape symbols like &nbsp to " ". Mar 23, 2010 at 8:14
  • 2
    Which is exactly what "disabling the output escaping" means. If your input would be correctly escaped, which it isn't, you would have &lt; and &gt;, and these would turn to < and >. And then you would have &amp;nbsp;, which would turn to &nbsp;. And that's a perfectly legal HTML escape sequence that does not need any further translation to " ". The fact that you have &nbsp; means your input is broken to begin with.
    – Tomalak
    Mar 23, 2010 at 8:35
  • 2
    And besides, you were not asking how to evaluate HTML named entities into actual characters, your question title is "How to unescape XML characters with help of XSLT?" and the only XML "special characters" are &lt;,&gt;&amp;&quot;&apos; or <,>,&,",', respectively. This means that a) your question is imprecise (as is this answer, in context) and b) your input is broken, as I said. Fix your input escaping and disable-output-escaping will start to work.
    – Tomalak
    Mar 23, 2010 at 8:52
1

Another solution. This one does not use the xml postprocessor, so readily useable as input of further xslt processing. Also guaranteed to create valid xml. This is a xslt 2.0 solution, escaping text within "documentation" tags, tested with saxon. You should modify the "allowedtags" variable to define your own data model. The immediate children are the tags, the ones below them are the attributes possible. Reading the allowed tags from an xsd is left as an excercise for the reader (please share it with me).

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:my="http://magwas.rulez.org/my"
>
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:variable name="allowedtags">
<li/>
<ul/>
<br/>
<a><href/></a>
</xsl:variable>

<xsl:template match="@*|*|processing-instruction()|comment()" mode="unescape">
    <xsl:copy>
        <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" mode="unescape"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="documentation" mode="unescape">
    <documentation>
    <xsl:call-template name="doc">
        <xsl:with-param name="str">
            <xsl:value-of select="."/>
        </xsl:with-param>
    </xsl:call-template>
    </documentation>
</xsl:template>

<xsl:template name="doc">
    <xsl:param name="str"/>
    <xsl:variable name="start" select="fn:substring-before($str,'&lt;')"/>
    <xsl:variable name="rest" select="fn:substring-after($str,'&lt;')"/>
    <xsl:variable name="fulltag" select="fn:substring-before($rest,'&gt;')"/>
    <xsl:variable name="tagparts" select="fn:tokenize($fulltag,'[  &#xA;]')"/>
    <xsl:variable name="tag" select="$tagparts[1]"/>
    <xsl:variable name="aftertag" select="fn:substring-after($rest,'&gt;')"/>
    <xsl:variable name="intag" select="fn:substring-before($aftertag,fn:concat(fn:concat('&lt;/',$tag),'&gt;'))"/>
    <xsl:variable name="afterall" select="fn:substring-after($aftertag,fn:concat(fn:concat('&lt;/',$tag),'&gt;'))"/>
    <xsl:value-of select="$start"/>
    <xsl:choose>
    <xsl:when test="$tag">
        <xsl:variable name="currtag" select="$allowedtags/*[$tag = local-name()]"/>
        <xsl:if test="$currtag">
            <xsl:element name="{$currtag/local-name()}">
                <xsl:for-each select="$tagparts[position()>1]">
                    <xsl:variable name="anstring" select="fn:replace(.,'^([^ &#xA;=]*)=.*$','$1')"/>
                    <xsl:variable name="antag" select="$currtag/*[$anstring = local-name()]"/>
                    <xsl:if test="$antag">
                        <xsl:attribute name="{$antag/local-name()}">
                            <xsl:value-of select="fn:replace(.,'^.*[^ &#34;]*&#34;([^&#34;]*)&#34;.*','$1')"/>
                        </xsl:attribute>
                    </xsl:if>
                </xsl:for-each>
                <xsl:if test="$intag">
                    <xsl:call-template name="doc">
                        <xsl:with-param name="str">
                            <xsl:value-of select="$intag"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:if>
            </xsl:element>
        </xsl:if>
        <xsl:if test="$afterall">
            <xsl:call-template name="doc">
                <xsl:with-param name="str">
                    <xsl:value-of select="$afterall"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:when>
    <xsl:otherwise>
                    <xsl:value-of select="$str"/>
    </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>
1
  • @*|*|processing-instruction()|comment() can be shortened to @* | node()
    – Vampire
    Sep 5, 2017 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.