xml - XSL 和 CDATA 中的实体

标签 xml xslt

我有 xml:

<?xml version="1.0" encoding="utf-8"?>
<offer>
  <products>
    <product id="1">
                <description>
            <name ><![CDATA[aa&apos;bb]]></name>
            <name2 >aa&apos;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>&#xa;</xsl:text>
<xsl:value-of select="$title_free2"/>
<xsl:text>&#xa;</xsl:text>
</xsl:template>
</xsl:stylesheet>

和 xsltproc 的输出

$ xsltproc my.xsl my.xml
aa&apos;bb
aa'bb

所需的输出应该是:

aa'bb
aa'bb

知道如何实现这一目标吗?

如何将CDATA字段中的实体替换为字符?

最佳答案

CDATA 是一种禁用文本中易于识别的部分中的特殊字符含义的方法。因此,如果您有很多特殊字符,例如 <& ,例如代码示例中的 XML 片段,您不必转义它们。

如果你写&在 CDATA 中,它表示字符 & (与外部 CDATA 不同,它引入实体或字符引用)。

所以您所要求的基本上是一个 XML 解析器,而 XSLT 1.0 中不存在该解析器。如果您问的是如何将一个字符串替换为另一个字符串(此处将“&apos; ”替换为“' ”),那么您可以使用 replace()在 XSLT 2.0 中或 XSLT 1.0 中的任何其他字符串操作。

但我怀疑您真正想要的是解决您的输入数据,该数据恰好包含一些双重转义(转义字符串将自身封装到 CDATA 中)。

关于xml - XSL 和 CDATA 中的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30996662/

相关文章:

Android:将按钮文本移至底部

xml - 用于多个字符串的 XPath/XSLT contains()

java - 排除结果前缀 ="xmlns"

xslt - 需要 xsl 转换帮助 - 不需要的文本

xml - XSLT:在 html 属性中插入参数值

java - XML 验证似乎忽略了一些 XSD 元素

xml - 防止将引用的程序集 PDB 和 XML 文件复制到输出

php - 如何在 PHP 中解析大型 XML 文件?

java - 有关在 Java/Spring 中使用注释的资源

xslt - 找不到 FOP 图像错误