我一直在尝试编写一个 XSLT,它将选择具有“精选”类别的项目,因为类别内的数据包装在 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 包装器的组合让我陷入了困境。谢谢