我需要遍历XML文档(那里没有问题)并检查我找到的值是否已经存在于我正在生成的XSL文档中的div中的(a)标记中,仅当值为不在那个(a)标签中我应该为它创建一个新的(a)标签并放入我正在检查的div中... 任何人都知道如何在XSLT中动态地做到这一点?
<div id="tags"><span class="l_cap"> </span>
<a href="#" class="current">all</a>
<xsl:for-each select="root/nodes/node/data/genres">
<xsl:for-each select="value">
**<xsl:if test="not(contains())">**
<a href="#"><xsl:value-of select="current()"/></a>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
对不起之前,我要做的是:在if语句中,检查div中是否已存在当前值,如果没有则添加,如果是,则不做任何事情......
再次10倍答案 0 :(得分:3)
听起来您正在尝试创建列表中所有“流派”的明确列表。
假设数据结构看起来有点像这样:
<root>
<nodes>
<node>
<data>
<genres>
<value>One</value>
<value>Two</value>
<value>Two</value>
<value>Three</value>
<value>Two</value>
</genres>
</data>
</node>
</nodes>
</root>
样式表看起来有点像这样:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="genres" match="value" use="."/>
<xsl:template match="/">
<div>
<xsl:for-each select="/root/nodes/node/data/genres/value">
<xsl:if test="generate-id(.) = generate-id(key('genres', .)[1])">
<a href="#"><xsl:value-of select="."/></a>
</xsl:if>
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>
然后你会得到这样的东西:
<div>
<a href="#">One</a>
<a href="#">Two</a>
<a href="#">Three</a>
</div>
这是一种相当标准的XSLT 1.0技术。它使用密钥(在此描述:http://www.xml.com/pub/a/2002/02/06/key-lookups.html)来创建所有/ root / nodes / node / data / genres / value条目的索引。然后它遍历所有条目,但只打印每种类型的第一个。最终结果是每个值只输出一次。