2011-02-22 26 views
2

对不起,但我真的在XPath noob。所以,这是我的问题。让我们假设我们有结构像这样的......如何按参数过滤节点,然后将位置()!= 1作为结果?

<structure> 
    <item filter=0>1 do not display</item> 
    <item filter=1>2 display</item> 
    <item filter=1>3 display</item> 
    <item filter=0>4 do not display</item> 
</structure> 

如何应用过滤器structure[filter=1],并从得到的数据只选择第一个元素? 我想这将是类似structure[filter=1][position() = 1]

PS:请推荐在线xlst测试工具。

谢谢。

回答

3

你几乎自己做了。

/*/item[@filter = 1][1] 

看看这个例子(明确value-of只是为了清楚):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 
    <xsl:template match="/"> 
     <xsl:value-of select="*/item[@filter = 1][1]"/> 
    </xsl:template> 
</xsl:stylesheet> 

结果对这种结构良好的输入:

<structure> 
    <item filter="0">1 do not display</item> 
    <item filter="1">2 display</item> 
    <item filter="1">3 display</item> 
    <item filter="0">4 do not display</item> 
</structure> 

将是2 display

要选择所有项目,那场比赛@filter=1条件,除了他们的第一个使用该XPath表达式:

/*/item[@filter = 1][position() > 1] 
+0

+1正确答案。 – 2011-02-22 23:42:59