问题
If I have XML like:
<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
?
Edit: The tool i\'m using (Andariel ant tasks) doesn\'t support the XPath 2.0 solution.
回答1:
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.
If embedding these queries in XML files like XSLT or ant scripts, remember to encode <
and >
as <
respecting >
.
回答2:
Turns out the tool does not support 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:
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 >
.
回答3:
This should work ...
max(foo/bar/@score)
... and ...
min(foo/bar/@score)
... check out this function reference.
回答4:
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.
<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>
回答5:
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.
回答6:
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.
min(//bar[@score !='']/@score)
The !=''
was to avoid a nulls which brought up NaN values (there's probably a better way to do that)
Here is a working xpath/xquery:
//bar/@score[@score=min(//*[@score !='']/number(@score))]
来源:https://stackoverflow.com/questions/1128745/how-can-i-use-xpath-to-find-the-minimum-value-of-an-attribute-in-a-set-of-elemen