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?
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.

Geert BormansInformation ArchitectCommented:
/Employees/Employee[not(distance < ../Employee/distance)]/distance
0
Geert BormansInformation 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
0
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.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Geert BormansInformation 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
0
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 ...
0
Geert BormansInformation 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?
0
Geert BormansInformation ArchitectCommented:
Just force casting to number... will work in both versions

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

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.
0
Geert BormansInformation ArchitectCommented:
welcome
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.