Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4247
  • Last Modified:

XPath 1.0 - Getting maximum and minimum value of a node

Can anyone suggest a clean way of grabbing the maximum and minimum value of an XML document  ( say datetime of an event ).

e.g say I have a list of event nodes and I want to grab the min and max event date in the xml.

I  can not use XPath 2.0 so any functions that are available for 2.0 to solve this problem can not be used.

Any ideas guys?
  • 3
  • 2
1 Solution
Geert BormansCommented:
I see two questions here
1. How to find the maximum (or minimum) using a single (not simple) XPath expression
Here is an example XML
XPath for maximum
/bar/foo[not(. <= following-sibling::foo)][not(. <= preceding-sibling::foo)][1]
use >= for the minimum.
Note that this is a slow process, in a sence that for every element foo it is checking all the following and preceding siblings...
there is a lot of treewalking going on.
It might be more elegant to load the nodeste in whatevre programming language and use that programming languages min and max functions (most programming languages allow you find the min nd max in an array, using algorithms that beat this approach)

2. How to find the min/max of a datetime.
DataTime is an unknown datatype in XPath 1
So that means that you need to transform teh date time in to number type
That really depends on how your datetime looks like in your XML,
If you would show an example, I could help you on that
sydronAuthor Commented:
The dateformat is yyyy-mm-ddThh:mm:ss.

Hope this is what you need.
sydronAuthor Commented:
I have to agree with you that it is slower to do this in xpath. I might actually just do it in the application language itself.
Geert BormansCommented:
I thought of stripping the non digit characters from teh date, but that doesn't work on the nodeset of course,
only option that is elegant enough to maintain seems to me that you get the max and min through application code
sydronAuthor Commented:
Perfect answer.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now