I'm working on a XSL 1.0 transformation to get HTML visualisation when displaying the XML in Firefox.
In my original XML, I have characters like
é è ‘...
I need to convert them into
é, è, ‘...
I have used this template :
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
Calling for each special character (here for example è) :
<xsl:variable name="newtext">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="$originaltext" />
<xsl:with-param name="replace" select="'&egrave;'" />
<xsl:with-param name="by" select="'è'" />
</xsl:call-template>
</xsl:variable>
Is there a solution where I can directly replace &
into & for example without the need to call the replacement template for each special character I expect to exist?
This seems to work for me in (an old version of) Firefox:
XML
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="mystyle.xsl"?>
<root>
<description>Article Containing Escaped Entitites</description>
<content>Lor&eacute;m ipsum &lsquo;dolor&lsquo; sit am&egrave;t, consectetuer adipiscing elit.</content>
</root>
mystyle.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<html>
<body>
<h2><xsl:value-of select="description"/></h2>
<p id="content">
<xsl:value-of select="content"/>
</p>
<script>
var element = document.getElementById("content");
element.innerHTML = element.innerHTML.replace(/&amp;/g,'&');
</script>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Result (screenshot):
Caveat: I am not a Javascript expert; this is just something I cobbled together on impulse.
Is there a solution where I can directly replace & into & for
example without the need to call the replacement template for each
special character I expect to exist?
Why don't you simply disable the escaping when outputting the text? For example, given an input of:
<content>Lor&eacute;m ipsum &lsquo;dolor&lsquo; sit am&egrave;t, consectetuer adipiscing elit.</content>
you can have your stylesheet process this as:
<p>
<xsl:value-of select="content" disable-output-escaping="yes"/>
</p>
and return:
<p>Lorém ipsum ‘dolor‘ sit amèt, consectetuer adipiscing elit.</p>
which a browser should render as:
String htmlstring = "Put Your HTML string here"
+ htmlstringbuf
.toString()
.replaceAll(" ", " ")
.replaceAll("&", "&")
.replaceAll("null", " ")
.replaceAll("<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>"," ")
.replaceAll("Â", "<br></br>")
.replaceAll("<\\?xml version = '1.0' encoding ='UTF-8'\\?>",
" ") + "</body>";