xpath to return the highest element value

Hi Experts,

My question relates to the following Employees.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
    <Employee id="1">
        <age>42</age>
        <name>Martina</name>
        <gender>Female</gender>
        <role>Cashier</role>
      <distance unit="m">1300.0</distance>
    </Employee>
      <Employee id="2">
        <age>30</age>
        <name>Soletho</name>
        <gender>Male</gender>
        <role>Tester</role>
      <distance unit="m">7203.5</distance>
    </Employee>
      <Employee id="3">
        <age>19</age>
        <name>Tom</name>
        <gender>Male</gender>
        <role>Manager</role>
      <distance unit="m">9042.0</distance>
    </Employee>
    <Employee id="4">
        <age>25</age>
        <name>Meghna</name>
        <gender>Female</gender>
        <role>Clerk</role>
      <distance unit="m">4100.0</distance>
    </Employee>
      <Employee id="5">
        <age>42</age>
        <name>Martha</name>
        <gender>Female</gender>
        <role>Cashier</role>
      <distance unit="m">3000.0</distance>
    </Employee>
      <Employee id="6">
        <age>3</age>
        <name>Sindrathi</name>
        <gender>Male</gender>
        <role>Tester</role>
      <distance unit="m">2300.5</distance>
    </Employee>
      <Employee id="7">
        <age>19</age>
        <name>Timothy</name>
        <gender>Male</gender>
        <role>Manager</role>
      <distance unit="m">13950.0</distance>
    </Employee>
    <Employee id="8">
        <age>225</age>
        <name>Solange</name>
        <gender>Female</gender>
        <role>Clerk</role>
      <distance unit="m">9975.0</distance>
    </Employee>
</Employees>



I want to write an xpath expression that returns the highest distance value, in this case it should be 13950.0

So far, what I have:
doc("D://Employees.xml") /Employees/Employee/distance[not(. <= preceding::Employee/distance) and not(. <= following::Employee/distance)]

which returns:
<?xml version="1.0" encoding="UTF-8"?>
<distance unit="m">9975.0</distance>
which is wrong.

I also get the same wrong result (9975.0) if I do:
doc("D://Employees.xml") //distance[not(. < //distance)][1]

How can I get the highest value (13950.0 in this case) returned instead?

Thanks for your help.
Smanyx
SmanyxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
/Employees/Employee[not(distance < ../Employee/distance)]/distance
Gertone (Geert Bormans)Information ArchitectCommented:
by using '<' and being strict that way, you can easily go for all children of the parent (which includes self!!)

Note: using XPath 2.0 I would do this
/Employees/Employee[distance  = max(../Employee/distance)]/distance
SmanyxAuthor Commented:
Thanks.
But even when I use '<', it doesn't make any difference since I still get the same wrong answer 9975.0 instead of the highest value 13950.0
I'm after a XPath 1.0 syntax, not 2.0, sorry ... forgot to mention that.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Gertone (Geert Bormans)Information ArchitectCommented:
Have you tried my XSLT1 solution? The first post I did.
It will work.
The problem with your XPath is that it gives a false positive when the following (or preceding) axis is empty. Remember that XPAth 1 comparissons in predicates are set comparissons. So look at my (tested and working) first posted XPath 1 and you will see what I mean
SmanyxAuthor Commented:
Yes, I did try it:
doc("D://Employees.xml") /Employees/Employee[not(distance < ../Employee/distance)]/distance

But it returns:
<?xml version="1.0" encoding="UTF-8"?>
<distance unit="m">9975.0</distance>

Which is not correct considering the set of data in the file ...
Gertone (Geert Bormans)Information ArchitectCommented:
Ah, but then you are using an XPath 2.0 processor most likely

The thing is, an XPath 1.0 processor only understands string, number and boolean and does automatic casting
An XPath 2.0 processor does not do casting automatically but understands string sorting

XPATH1 =>  <distance unit="m">13950.0</distance>
XPATH2 =>  <distance unit="m">9975.0</distance>

on my solution... you said it was XPAth 1, what is your processor?
Gertone (Geert Bormans)Information ArchitectCommented:
Just force casting to number... will work in both versions

/Employees/Employee[not(number(distance) < ../Employee/distance)]/distance

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SmanyxAuthor Commented:
Thanks for your help.
The casting is required ... and it worked.
Gertone (Geert Bormans)Information ArchitectCommented:
welcome
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.