XSL中的特殊字符 [英] Special characters in XSL

查看:156
本文介绍了XSL中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行XSL 1.0转换,以在Firefox中显示XML时获得HTML可视化。
在我的原始XML中,我有像

 & amp; eacute; &放大器;放大器; egrave; & amp; lsquo; ... 

我需要将它们转换为

 é,è,'... 

我已经使用了这个模板:

 < xsl:template name =string-replace-all> ; 
< xsl:param name =text/>
< xsl:param name =replace/>
< xsl:param name =by/>
< 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 =textselect =substring-after($ text,$ replace)/>
< / xsl:call-template>
< / xsl:when>
< xsl:otherwise>
< xsl:value-of select =$ text/>
< / xsl:否则>

调用每个特殊字符(例如& egrave;):

 < xsl:variable name =newtext> 
< xsl:call-template name =string-replace-all>
< xsl:with-param name =textselect =$ originaltext/>
< / xsl:call-template>
< / xsl:variable>

有没有解决方法可以直接替换& amp; code>放入&例如,不需要为每个特殊字符调用替换模板,我希望存在?

解决方案

这似乎适用于我(旧版本)Firefox:

XML

 <?xml version =1.0encoding =utf-8?> 
<?xml-stylesheet type =text / xslhref =mystyle.xsl?>
< root>
< description>包含逃逸物件的文章< / description>
Lor& eacute; m ipsum& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; $ 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; / g,'& amp;');
< / script>

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

< / xsl:stylesheet>

结果(屏幕截图): b $ b


警告:我不是Javascript专家;这只是我冲动拼凑在一起的东西。


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

&amp;eacute; &amp;egrave; &amp;lsquo;...

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?

解决方案

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):

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

这篇关于XSL中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆