2

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

输入数据是这样的:

<item>
  ...
  <g:category>Gifts under &amp;pound;10</g:category>
</item>

我的 XSL 是:

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

我希望它呈现如下内容:

Gifts under £10

但我得到的只是:

Gifts under &pound;10
4

2 回答 2

6

好吧,假设您有某种方法可以定位<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>

在这种情况下,所有<p>带有 test = 'test' 属性的标签都将应用 CDATA,所有其他标签将正常输出。

于 2009-10-30T15:22:58.937 回答
0

我的代码是:

<xsl:element name="a">
  <xsl:attribute name="href">
    product.aspx?prod=<xsl:copy-of select="title/text()" />
  </xsl:attribute>
  <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
  <xsl:copy-of select="g:price" /> - <xsl:copy-of select="title/text()" />
  <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
</xsl:element>
于 2009-10-30T16:53:30.533 回答