我有 xml:
<?xml version="1.0" encoding="utf-8"?>
<offer>
<products>
<product id="1">
<description>
<name ><![CDATA[aa'bb]]></name>
<name2 >aa'bb</name2>
</description>
</product>
</products>
</offer>
和 xslt:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text"/>
<xsl:strip-space elements="node"/>
<xsl:strip-space elements="*" />
<xsl:template match="/offer/products/product" >
<xsl:variable name="title_free" select="description/name"/>
<xsl:variable name="title_free2" select="description/name2"/>
<xsl:value-of select="$title_free"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="$title_free2"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
和 xsltproc 的输出
$ xsltproc my.xsl my.xml
aa'bb
aa'bb
所需的输出应该是:
aa'bb
aa'bb
知道如何实现这一目标吗?
如何将CDATA字段中的实体替换为字符?
最佳答案
CDATA 是一种禁用文本中易于识别的部分中的特殊字符含义的方法。因此,如果您有很多特殊字符,例如 <
和&
,例如代码示例中的 XML 片段,您不必转义它们。
如果你写&
在 CDATA 中,它表示字符 &
(与外部 CDATA 不同,它引入实体或字符引用)。
所以您所要求的基本上是一个 XML 解析器,而 XSLT 1.0 中不存在该解析器。如果您问的是如何将一个字符串替换为另一个字符串(此处将“'
”替换为“'
”),那么您可以使用 replace()
在 XSLT 2.0 中或 XSLT 1.0 中的任何其他字符串操作。
但我怀疑您真正想要的是解决您的输入数据,该数据恰好包含一些双重转义(转义字符串将自身封装到 CDATA 中)。
关于xml - XSL 和 CDATA 中的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30996662/