简体   繁体   English

XSL中的特殊字符

[英]Special characters in XSL

I'm working on a XSL 1.0 transformation to get HTML visualisation when displaying the XML in Firefox. 我正在进行XSL 1.0转换,以便在Firefox中显示XML时获得HTML可视化。 In my original XML, I have characters like 在我的原始XML中,我有类似

é è ‘...

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;) : 要求每个特殊字符(例如&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; 有没有可以直接替换&amp;的解决方案&amp; into & for example without the need to call the replacement template for each special character I expect to exist? 进入&例如不需要为我希望存在的每个特殊字符调用替换模板?

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: 浏览器应将其呈现为:

在此处输入图片说明

This seems to work for me in (an old version of) Firefox: 在Firefox(旧版本)中,这似乎对我有用:

XML 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 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): 结果(屏幕截图):

在此处输入图片说明

Caveat : I am not a Javascript expert; 警告 :我不是Java语言专家;我不是Java语言专家。 this is just something I cobbled together on impulse. 这只是我一时冲动而拼凑的东西。

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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM