用于html的xslt中的cdata

Mat*_*t W 2 xslt cdata

我有一个生成纯HTML的XSLT文件.我需要在CDATA块中包装一些元素,因此打算使用cdata-section-elements.但是,如果我想要包含CDATA的元素<p>在页面上只有一个,那么如何让它不将CDATA放在所有其他<p>元素中呢?

输入数据是这样的:

<item>
  ...
  <g:category>Gifts under &amp;pound;10</g:category>
</item>
Run Code Online (Sandbox Code Playgroud)

我的XSL是:

<xsl:element name="a">
  <xsl:attribute name="href">productlist.aspx</xsl:attribute>
  <xsl:copy-of select="text()" />
</xsl:element>
Run Code Online (Sandbox Code Playgroud)

我想要这样做:

Gifts under £10
Run Code Online (Sandbox Code Playgroud)

但我得到的只是:

Gifts under &pound;10
Run Code Online (Sandbox Code Playgroud)

Jam*_*win 5

假设您有一些方法可以定位<p>要包含在CDATA部分中的标记,您可以执行以下操作:

<xsl:output method="xml" version="1.0" encoding="UTF-8" 
    indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="p[@test = 'test']">
    <xsl:copy>
        <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
        <xsl:apply-templates/>
        <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
    </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,所有<p>具有属性test ='test'的标签都将CDATA应用于它们,所有其他标签将只是正常输出.