How to get max value with Xpath

G

goldtech

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

Martin Honnen

goldtech said:
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>
 
M

Mayeul

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)
 
J

Joe Kesselman

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top