【发布时间】:2015-06-11 18:45:07
【问题描述】:
我正在进行 XSL 1.0 转换,以便在 Firefox 中显示 XML 时获得 HTML 可视化。 在我的原始 XML 中,我有像
这样的字符é è ‘...
我需要将它们转换成
é, è, ‘...
我用过这个模板:
<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>
调用每个特殊字符(例如 è):
<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>
有没有一种解决方案,我可以直接将&amp; 替换为 &,例如,无需为我希望存在的每个特殊字符调用替换模板?
【问题讨论】:
-
在不切换到 XSLT 2.0 的情况下,我能想到的唯一改进是在您的样式表中创建一个特殊字符的“表”并递归遍历它,调用
string-replace-allfor表中的每个“行”都输出上一个调用。但是,可能有更好的选择涉及扩展函数,例如 exslt.org/dyn/functions/evaluate/index.html,具体取决于您使用的 XSLT 处理器。 -
我需要使用 Firefox 直接显示 XML 及其转换,无需特殊预处理器。
-
好的,既然 Firefox 是你的环境,为什么不让你的样式表输出一些 Javascript。 JS代码可以在页面加载完成后,通过
&amp;替换&。我不确定它是否会起作用,但值得一试。 -
我试过了,但它不适用于 XSL Transfo 生成的 XML。我最终不得不创建一个 XSL 样式表,其中所有的 HTML 字符都经过双重编码,并且它们的结果......痛苦而丑陋,但它确实有效。非常感谢您的支持。
标签: javascript xml firefox xslt xslt-1.0