How to get max value with Xpath

Discussion in 'XML' started by goldtech, Jun 10, 2011.

  1. goldtech

    goldtech Guest

    given:

    <links>
    <l><a>1</a><n>Building</n></l>
    <l><a>4</a><n>Community</n></l>
    <l><a>2</a><n>Comedy</n></l>
    <l><a>3</a><n>Computer</n></l>
    </links>

    What is an Xpath statement to find the maximum value in the "a"
    element node. It would be 4. I have searched and tried many examples
    with no luck. It must be simple? I could easily parse into an array
    and use an array's max function but would like to learn the xpath way.

    Help appreciated. I am using libxml2 in Python 2.6. I don't know if I
    have Xpath 1.0 or 2.0(?)

    Thanks,

    Lee G.
     
    goldtech, Jun 10, 2011
    #1
    1. Advertising

  2. goldtech wrote:
    > given:
    >
    > <links>
    > <l><a>1</a><n>Building</n></l>
    > <l><a>4</a><n>Community</n></l>
    > <l><a>2</a><n>Comedy</n></l>
    > <l><a>3</a><n>Computer</n></l>
    > </links>
    >
    > What is an Xpath statement to find the maximum value in the "a"
    > element node. It would be 4. I have searched and tried many examples
    > with no luck. It must be simple? I could easily parse into an array
    > and use an array's max function but would like to learn the xpath way.
    >
    > Help appreciated. I am using libxml2 in Python 2.6. I don't know if I
    > have Xpath 1.0 or 2.0(?)


    XPath 2.0 has a max function so there you could do
    max(links/l/a/number(.))
    But as far as I know libxml only supports XPath 1.0.
    If you are using XPath inside of an XSLT stylesheet then the usual way
    with XSLT 1.0 is sorting in descending order and taking the first e.g.
    <xsl:for-each select="links/l/a">
    <xsl:sort select="." data-type="number" order="descending"/>
    <xsl:if test="position() = 1">
    <xsl:value-of select="."/>
    </xsl:if>
    </xsl:for-each>


    --

    Martin Honnen --- MVP Data Platform Development
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Jun 10, 2011
    #2
    1. Advertising

  3. goldtech

    Mayeul Guest

    On 10/06/2011 11:57, Martin Honnen wrote:
    > goldtech wrote:
    >> given:
    >>
    >> <links>
    >> <l><a>1</a><n>Building</n></l>
    >> <l><a>4</a><n>Community</n></l>
    >> <l><a>2</a><n>Comedy</n></l>
    >> <l><a>3</a><n>Computer</n></l>
    >> </links>
    >>
    >> What is an Xpath statement to find the maximum value in the "a"
    >> element node. It would be 4. I have searched and tried many examples
    >> with no luck. It must be simple? I could easily parse into an array
    >> and use an array's max function but would like to learn the xpath way.
    >>
    >> Help appreciated. I am using libxml2 in Python 2.6. I don't know if I
    >> have Xpath 1.0 or 2.0(?)

    >
    > XPath 2.0 has a max function so there you could do
    > max(links/l/a/number(.))
    > But as far as I know libxml only supports XPath 1.0.
    > If you are using XPath inside of an XSLT stylesheet then the usual way
    > with XSLT 1.0 is sorting in descending order and taking the first e.g.
    > <xsl:for-each select="links/l/a">
    > <xsl:sort select="." data-type="number" order="descending"/>
    > <xsl:if test="position() = 1">
    > <xsl:value-of select="."/>
    > </xsl:if>
    > </xsl:for-each>


    An inefficient, but effective, method working with XPath 1.0 would be:

    number(/links/l[not(../l/a > a)]/a)

    --
    Mayeul
     
    Mayeul, Jun 10, 2011
    #3
  4. When working with XPath and XSLT you need to learn to think functionally
    rather than procedurally, and find the simplest description of your
    problem rather than thinking about the details of its implementation.

    Funcitonally, one way to define the maximum value is the one such that
    no value exists which is greater. So one approach that could work would be

    /links/l[not(../l/a > a)]

    This should work even if there are multiple <a> children of the <l>. If
    two <l>s are tied, it will return them both.

    As with any programming language there are other ways to get the same
    result; this is just the first one that occurred to me.

    --
    Joe Kesselman,
    http://www.love-song-productions.com/people/keshlam/index.html

    {} ASCII Ribbon Campaign | "may'ron DaroQbe'chugh vaj bIrIQbej" --
    /\ Stamp out HTML mail! | "Put down the squeezebox & nobody gets hurt."
     
    Joe Kesselman, Jun 12, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,032
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,533
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Summercool
    Replies:
    9
    Views:
    927
    dorayme
    Oct 23, 2007
  4. Greg Ferris

    Textarea max rows and max characters per row

    Greg Ferris, Jan 16, 2004, in forum: Javascript
    Replies:
    2
    Views:
    630
    Greg Ferris
    Jan 16, 2004
  5. kriton
    Replies:
    0
    Views:
    370
    kriton
    Apr 22, 2005
Loading...

Share This Page