我一直试图编写一个XSLT,它将选择类别为“ Featured”的项目,因为类别内的数据包装在CDATA中。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Matched Items</h2>
<xsl:apply-templates select="//item[category/.='Featured']"/>
</body>
</html>
</xsl:template>
<xsl:template match="item">
<div class="news-item" width="100%">
<div class="news-item-description">
<xsl:value-of select="description"/>
</div>
</div>
<div class="clear" />
</xsl:template>
</xsl:stylesheet>
这是我的数据样本。这是来自wordpress博客的内容,因此我无法更改输出。
<items>
<item>
<category>
<![CDATA[ Category A ]]>
</category>
<category>
<![CDATA[ Featured ]]>
</category>
<description>This item should be in the output HTML</description>
</item>
<item>
<category>
<![CDATA[ Uncategorized ]]>
</category>
<category>
<![CDATA[ Some other category ]]>
</category>
<description>This item should NOT be in the output HTML</description>
</item>
<item>
<category>
<![CDATA[ Uncategorized ]]>
</category>
<category>
<![CDATA[ Featured ]]>
</category>
<description>This item should be in the output HTML</description>
</item>
</items>
我想要的输出看起来像这样:
<html>
<body>
<h2>Matched Items</h2>
<div class="news-item" width="100%">
<div class="news-item-description">This item should be in the output HTML</div>
</div>
<div class="clear"></div>
<div class="news-item" width="100%">
<div class="news-item-description">This item should be in the output HTML</div>
</div>
<div class="clear"></div>
</body>
</html>
这似乎应该很容易。多个类别元素加上CDATA包装器的结合让我陷于困境。谢谢
//item[category/.='Featured']
在语法上甚至都不是有效的XPath,我认为这是一个错字。
无论如何,这与CDATA无关。CDATA是没有意义的,它不存在。仅在解析文档时才重要。当您在输入上使用XPath时,它已经不存在了。
在进行字符串比较之前,需要修剪空格。尝试:
<xsl:apply-templates select="//item[category[normalize-space() = 'Featured']]" />
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句