用于HTML的xslt中的cdata [英] cdata in xslt for html
问题描述
我有一个生成纯HTML的XSLT文件。我需要将一些元素包装在CDATA块中,因此打算使用cdata-section-elements。但是,如果我要包含的CDATA元素仅是页面上的一个< p>
,我如何才能不将CDATA放在所有其他<$中c $ c>< p> 元素?
I have an XSLT file generating plain HTML. I need to wrap some elements in CDATA blocks, so intend to use cdata-section-elements. But, if the element I want to have contain CDATA is only one <p>
on the page, how do I get it to not put CDATA in all the other <p>
elements?
输入数据是这样的:
<item>
...
<g:category>Gifts under &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>
我希望它呈现如下内容:
I want this to render something like:
Gifts under £10
但我得到的是:
Gifts under £10
推荐答案
假设您有某种定位所需的< p>
标记的方法放在CDATA部分中,您可以执行以下操作:
Well assuming you have some way of targeting the <p>
tag that you want to enclose in CDATA section, you could do something like:
<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"><![CDATA[</xsl:text>
<xsl:apply-templates/>
<xsl:text disable-output-escaping="yes">]]></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,其他所有标签将照常输出。
In this case all <p>
tags with an attribute test = 'test' will have CDATA applied to them, all other tags will just be output as normal.
这篇关于用于HTML的xslt中的cdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!