John Smith
John Smith

Reputation: 1876

find element with highest value with XPath?

<div class="box box-0 box-hover-null">10828</div>

I have a series of these in an html document, with variable number as text().

I need to find the first div with the highest and lowest text value.

my current selector:

//div[string-length(text()) > 0 and contains(concat(' ',normalize-space(@class),' '),' box-0 ')]

Upvotes: 1

Views: 752

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

<div class="box box-0 box-hover-null">10828</div>

I have a series of these in an html document, with variable number as text().

I need to find the first div with the highest and lowest text value.

As far as I know, Selenium uses the XPath engine of the respective browser -- at present all browsers only support XPath 1.0.

Thus, what is required is an XPath 1.0 expression selecting the first <div> element whose string value is castable to number and this number is the minimum of all such <div> numeric values.

A similar XPath expression is required but that selects the first such <div> element with maximum numeric value.

Here are such two XPath expressions that select the wanted <div> elements:

1st <div> element with minimum numeric value:

(//div[number(.) = number(.)
     and number(.) = //div[number(.) = number(.)
                         and not(number(.) > //div[number(.) = number(.)]/text())
                          ]
      ]
)[1]

1st <div> element with maximum numeric value:

(//div[number(.) = number(.)
     and number(.) = //div[number(.) = number(.)
                         and not(number(.) < //div[number(.) = number(.)]/text())
                          ]
      ]
)[1]

Here is an XSLT 1.0 -- based verification. The transformation below evaluates these two XPath expressions against an XML document and copies to the output the results of this evaluation (the selected nodes):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    First node with Minimum value: <xsl:copy-of select=
    "(//div[number(.) = number(.)
    and number(.) = //div[number(.) = number(.)
                        and not(number(.) > //div[number(.) = number(.)]/text())
                         ]
           ]
      )[1]"/>

=======================

    First node with Maximum value: <xsl:copy-of select=
    "(//div[number(.) = number(.)
    and number(.) = //div[number(.) = number(.)
                        and not(number(.) &lt; //div[number(.) = number(.)]/text())
                         ]
           ]
      )[1]"/>
  </xsl:template>
</xsl:stylesheet>

When this transformation is applied on this XML document:

<html>
  <div class="box box-0 box-hover-null">100</div>
  <div class="box box-1 box-hover-null">100</div>
  <div class="box box-2 box-hover-null">0500</div>
  <div class="box box-3 box-hover-null">00500</div>
  <div class="box box-4 box-hover-null">300</div>
  <div class="box box-5 box-hover-null">200</div>
</html>

the wanted, correct result is produced:

    First node with Minimum value: <div class="box box-0 box-hover-null">100</div>

=======================

    First node with Maximum value: <div class="box box-2 box-hover-null">0500</div>

Upvotes: 1

supputuri
supputuri

Reputation: 14135

To get the max

max(//div[@class='box box-0 box-hover-null'])

you can get the element using

(//div[.=max(//div[@class='box box-0 box-hover-null'])][[@class='box box-0 box-hover-null'])[1]

For min value

min(//div[@class='box box-0 box-hover-null'])

You can get the element using

(//div[.=min(//div[@class='box box-0 box-hover-null'])][[@class='box box-0 box-hover-null'])[1]

Upvotes: 0

Related Questions