Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Xpath to find element with highest attribute value

180 views
Skip to first unread message

Wozza

unread,
Feb 26, 2007, 6:55:07 AM2/26/07
to
Hi,
I have a set of elements that record different revisions for a specific item
...

I was wondering if there is a XPath query that would prevent iterating
through all element to find the latest revision??

<RESPONSE templateid="E28A9E58-ACCF-40B9-9CEE-8D99D53152D0"
revisionnumber="2" uniqueid="" value="26/02/2007 11:32:43" />
<RESPONSE templateid="E28A9E58-ACCF-40B9-9CEE-8D99D53152D0"
revisionnumber="1" uniqueid="" value="22/02/2007 14:49:21" />
<RESPONSE templateid="E6F4CAC9-C6F8-4F8E-8D49-F860D00E5DE8"
revisionnumber="1" uniqueid="" value="22/02/2007 14:49:21" />
<RESPONSE templateid="E6F4CAC9-C6F8-4F8E-8D49-F860D00E5DE8"
revisionnumber="2" uniqueid="" value="26/02/2007 11:32:43" />

I want the latest revision for (say)
templateid='E28A9E58-ACCF-40B9-9CEE-8D99D53152D0'.

At the moment the only way I know of doing this is to iterate through all
elements where templateid='E28A9E58-ACCF-40B9-9CEE-8D99D53152D0' and finding
the one with the highest revision number.

any help appreciated.
Wozza


stephen...@pfizer.com

unread,
Feb 26, 2007, 8:47:39 AM2/26/07
to
You could make use of the max function in the XSLT functions library.
See: http://www.w3schools.com/xpath/xpath_functions.asp for more
details.

For example:

<xsl:template match="foo">
<xsl:variable name="tmpltId">E6F4CAC9-C6F8-4F8E-8D49-
F860D00E5DE8</xsl:variable>
<xsl:variable name="maxRevision"
select="max(RESPONSE[@templateid='E6F4CAC9-C6F8-4F8E-8D49-
F860D00E5DE8']/@revisionnumber"/>

<xsl:for-each select="RESPONSE[@templateid = $tmpltId and
revisionnumber = $maxRevision]">

</xsl:for-each>

</xsl:template>

stephen...@pfizer.com

unread,
Feb 26, 2007, 8:54:52 AM2/26/07
to
On Feb 26, 8:47 am, "stephen.p.ow...@pfizer.com"

"There is a typo in the example above, you need to close the opening
( character on the max function:

select="max(RESPONSE[@templateid='E6F4CAC9-C6F8-4F8E-8D49-
F860D00E5DE8']/@revisionnumber)"

Wozza

unread,
Feb 27, 2007, 5:33:44 AM2/27/07
to
Stephen,
Brilliant, thanks for that!
Wozza

<stephen...@pfizer.com> wrote in message
news:1172498092.3...@s48g2000cws.googlegroups.com...

0 new messages