Subject: Re: Filter by id and date
From: "Tony Graham" <tgraham@xxxxxxxxxx>
Date: Mon, 5 Sep 2011 11:18:58 +0100 (IST)
|
On Mon, September 5, 2011 10:36 am, Emma Burrows wrote:
...
> <items>
> <item id='1' date='2011'>Item1</item>
> <item id='2' date='2009'>Item2</item>
> <item id='3' date='2002'>Item3</item>
> <item id='2' date='2011'>Item2</item>
> <item id='3' date='2002'>Item3</item>
> </items>
>
> For example, the Xpath in question looks like this right now (long-winded
> but more efficient path-finding syntax simplified to // for this example,
> and $idref obviously contains a valid id):
>
> <xsl:value-of select="normalize-space(//item[@id=$idref])"/>
>
> What is the best way to filter that to return only the item whose @date is
> the highest for all items with @id=$idref?
The one-line solution is:
normalize-space(//item[@id=$item][@date = max(../item[@id=$item]/@date)][1])
Note the '[1]' to get rid of duplicates such as 'Item3' in your example.
You've already received a solution from Andrew Welch that first makes a
variable for all the possible matching values. That would generally be
clearer and easier to understand when you come back to it in six months
time.
If you have a lot of data with a lot of duplicates in any order, it may be
simplest to process the data once to sort it by date and then change your
@select to "normalize-space(//item[@id=$idref][1])".
BTW, have you looked at using xsl:key? Would it be possible to replace
the long and complicated XPath with a xsl:key that indexes the <item> with
a key based on, say, the string-join() of the significant variable data
from the XPath so you can just lookup the <item>s?)
Regards,
Tony Graham tgraham@xxxxxxxxxx
Consultant http://www.mentea.net
Mentea 13 Kelly's Bay Beach, Skerries, Co. Dublin, Ireland
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
XML, XSL FO and XSLT consulting, training and programming
|