How can I use XPath to find the minimum value of an attribute in a set of elements?

和自甴很熟 提交于 2019-11-26 05:30:38

问题


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 &lt; respecting &gt;.




回答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 &lt; respecting &gt;.




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!