Special characters in XSL

2019-09-05 08:29发布

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 &egrave;) :

            <xsl:variable name="newtext">
              <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text" select="$originaltext" />
                <xsl:with-param name="replace" select="'&amp;egrave;'" />
                <xsl:with-param name="by" select="'è'" />
              </xsl:call-template>
            </xsl:variable>

Is there a solution where I can directly replace &amp; into & for example without the need to call the replacement template for each special character I expect to exist?

3条回答
狗以群分
2楼-- · 2019-09-05 09:13
String htmlstring = "Put Your HTML string here"
            + htmlstringbuf
                    .toString()
                    .replaceAll("&nbsp;", " ")
                    .replaceAll("&", "&amp;")
                    .replaceAll("null", " ")
                    .replaceAll("<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>"," ")
                    .replaceAll("Â", "<br></br>")
                    .replaceAll("<\\?xml version = '1.0' encoding ='UTF-8'\\?>",
                            " ") + "</body>";
查看更多
欢心
3楼-- · 2019-09-05 09:14

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&amp;eacute;m ipsum &amp;lsquo;dolor&amp;lsquo; sit am&amp;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&eacute;m ipsum &lsquo;dolor&lsquo; sit am&egrave;t, consectetuer adipiscing elit.</p>

which a browser should render as:

enter image description here

查看更多
霸刀☆藐视天下
4楼-- · 2019-09-05 09:25

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&amp;eacute;m ipsum &amp;lsquo;dolor&amp;lsquo; sit am&amp;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;amp;/g,'&amp;');
            </script>

        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

Result (screenshot):

enter image description here

Caveat: I am not a Javascript expert; this is just something I cobbled together on impulse.

查看更多
登录 后发表回答