xml 如何使用 XPath 查找一组元素中某个属性的最小值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1128745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use XPath to find the minimum value of an attribute in a set of elements?
提问by brasskazoo
If I have XML like:
如果我有像这样的 XML:
<foo>
<bar id="1" score="192" />
<bar id="2" score="227" />
<bar id="3" score="105" />
...
</foo>
Can I use XPath to find the minimum and maximum values of score?
我可以使用 XPath 来查找 的最小值和最大值score吗?
Edit: The tool i'm using (Andarielant tasks) doesn't support the XPath 2.0 solution.
编辑:我使用的工具(Andarielant 任务)不支持 XPath 2.0 解决方案。
采纳答案by brasskazoo
Turns out the tool does not support XPath 2.0.
原来该工具不支持 XPath 2.0。
XPath 1.0 doesn't have the fancy min()and max()functions, so to find these values we need to be a little tricky with the XPath logic, and compare the values on the siblings of the node:
XPath 1.0 没有花哨min()和max()功能,所以要找到这些值,我们需要对 XPath 逻辑有点棘手,并比较节点的兄弟节点上的值:
Maximum:
最大值:
/foo/bar[not(preceding-sibling::bar/@score >= @score)
and not(following-sibling::bar/@score > @score)]/@score
Minimum:
最低限度:
/foo/bar[not(preceding-sibling::bar/@score <= @score)
and not(following-sibling::bar/@score < @score)]/@score
If embedding these queries in XML files like XSLT or ant scripts, remember to encode <and >as <respecting >.
如果嵌入在像XSLT或Ant脚本的XML文件这些查询,记得编码<和>作为<尊重>。
回答by Pavel Minaev
Here's a slightly shorter solution.
这是一个稍短的解决方案。
Maximum:
最大值:
/foo/bar/@score[not(. < ../../bar/@score)][1]
Minimum:
最低限度:
/foo/bar/@score[not(. > ../../bar/@score)][1]
I've edited the predicate so that it's applicable to any sequence of bar, even if you decide to change the path. Note that parent of attribute is the element to which it belongs.
我已经编辑了谓词,使其适用于 的任何序列bar,即使您决定更改路径。请注意,属性的父级是它所属的元素。
If embedding these queries in XML files like XSLT or ant scripts, remember to encode <and >as <respecting >.
如果嵌入在像XSLT或Ant脚本的XML文件这些查询,记得编码<和>作为<尊重>。
回答by JP Alioto
This should work ...
这应该工作...
max(foo/bar/@score)
... and ...
... 和 ...
min(foo/bar/@score)
... check out this function reference.
...查看此函数参考。
回答by Graham Lower
I stumbled upon the thread and didn't find an answer that worked for me, so where is what I eventually ended up using...
我偶然发现了该线程并没有找到对我有用的答案,所以我最终最终使用的方法在哪里......
Outputs the lowest value, of course you could choose to output the @id from the node with the lowest value instead of you choose.
输出最低值,当然你可以选择从最低值的节点输出@id,而不是你自己选择。
<xsl:for-each select="/foo">
<xsl:sort select="@score"/>
<xsl:if test="position()=1">
<xsl:value-of select="@score"/>
</xsl:if>
</xsl:for-each>
The same for the maximum value:
最大值相同:
<xsl:for-each select="/foo">
<xsl:sort select="@score" order="descending"/>
<xsl:if test="position()=1">
<xsl:value-of select="@score"/>
</xsl:if>
</xsl:for-each>
回答by Paulb
I know this is five years old. Just adding some more options for whoever may search and come across this.
我知道这是五岁。只需为可能搜索并遇到此问题的人添加更多选项即可。
Something similar to this worked for me in XSLT 2.0.
类似的东西在 XSLT 2.0 中对我有用。
min(//bar[@score !='']/@score)
The !=''was to avoid a nulls which brought up NaN values (there's probably a better way to do that)
这!=''是为了避免带来 NaN 值的空值(可能有更好的方法来做到这一点)
Here is a working xpath/xquery:
这是一个有效的 xpath/xquery:
//bar/@score[@score=min(//*[@score !='']/number(@score))]
回答by kuy
Try this:
尝试这个:
//foo/bar[not(preceding-sibling::bar/@score <= @score) and not(following-sibling::bar/@score <= @score)]
Maybe this will work on XPath 1.0.
也许这将适用于 XPath 1.0。

