【发布时间】:2015-03-10 13:06:27
【问题描述】:
任何关于如何解决以下问题的想法都将受到高度赞赏。
输入:
<p>
<div>
Original<br/>This is the original <b>acid</b>, a hydroxy monocarboxylic <span class="hl1">acid</span>.
</div>
</p>
期望的输出:
<p>
<div>
Original<br/>This is the original <b>acid</b>, a hydroxy monocarboxylic <span class="hl1">acid</span>.
</div>
</p>
尝试 1:
`<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output omit-xml-declaration="yes" indent="no" encoding="UTF-8"/>
<!--The identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div">
<xsl:copy>
<xsl:value-of select="/" disable-output-escaping="no"/>
</xsl:copy>
</xsl:template>
`
尝试2: 作为替代方案,我考虑将子元素的内容放入 CDATA 包装器中。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output omit-xml-declaration="yes" indent="no" encoding="UTF-8"/>
<!--The identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div">
<xsl:copy>
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:value-of select="/" />
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</xsl:copy>
</xsl:template>
但这并没有给我我想要的。 有人有更好的主意吗?我正在使用 XSLT 2.0
【问题讨论】:
-
您使用哪种 XSLT 2.0 处理器?如果那是 Saxon 9,例如 Saxon 9.6,那么我只需设置
version="3.0"并使用 w3.org/TR/xpath-functions-30/#func-serialize 函数。旧版本的 Saxon 有一个扩展来做同样的事情。 -
您好,谢谢!我正在使用撒克逊 9.1.0。我不认为切换到较新版本是一种选择,因为它在许多地方都使用过,而且升级的努力可能会很高!
标签: xslt escaping xslt-2.0 cdata