XPath 1.0 - Getting maximum and minimum value of a node

Posted on 2009-12-17
Last Modified: 2013-11-11
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?
Question by:sydron
    LVL 60

    Accepted Solution

    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

    Author Comment

    The dateformat is yyyy-mm-ddThh:mm:ss.

    Hope this is what you need.

    Author Comment

    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.
    LVL 60

    Expert Comment

    by:Geert Bormans
    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

    Author Closing Comment

    Perfect answer.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now