XSL 中的特殊字符
Special characters in XSL
我正在进行 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>
是否有一种解决方案可以直接将 &
替换为 & 而无需为我希望存在的每个特殊字符调用替换模板?
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>";
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?
为什么不在输出文本时简单地禁用转义?例如,给定输入:
<content>Lor&eacute;m ipsum &lsquo;dolor&lsquo; sit am&egrave;t, consectetuer adipiscing elit.</content>
您可以让样式表将其处理为:
<p>
<xsl:value-of select="content" disable-output-escaping="yes"/>
</p>
和return:
<p>Lorém ipsum ‘dolor‘ sit amèt, consectetuer adipiscing elit.</p>
浏览器应呈现为:
这似乎适用于(旧版本的)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>
结果(截图):
警告:我不是Javascript专家;这只是我一时冲动拼凑的东西。
我正在进行 XSL 1.0 转换,以便在 Firefox 中显示 XML 时获得 HTML 可视化效果。 在我原来的 XML 中,我有像
这样的字符&eacute; &egrave; &lsquo;...
我需要将它们转换成
é, è, ‘...
我用过这个模板:
<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>
是否有一种解决方案可以直接将 &
替换为 & 而无需为我希望存在的每个特殊字符调用替换模板?
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>";
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?
为什么不在输出文本时简单地禁用转义?例如,给定输入:
<content>Lor&eacute;m ipsum &lsquo;dolor&lsquo; sit am&egrave;t, consectetuer adipiscing elit.</content>
您可以让样式表将其处理为:
<p>
<xsl:value-of select="content" disable-output-escaping="yes"/>
</p>
和return:
<p>Lorém ipsum ‘dolor‘ sit amèt, consectetuer adipiscing elit.</p>
浏览器应呈现为:
这似乎适用于(旧版本的)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>
结果(截图):
警告:我不是Javascript专家;这只是我一时冲动拼凑的东西。