[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Filter by id and date


Subject: Re: [xsl] Filter by id and date
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Mon, 5 Sep 2011 11:02:37 +0100

On 5 September 2011 10:36, Emma Burrows <Emma.Burrows@xxxxxxxxxxx> wrote:
> Hopefully a quick question. Using XSLT 2.0, I am filtering entries in an
input file based on their ids. The only problem is that I've been given an
update to the original input file which includes many duplicates, some of
which are identical and some of which have been updated. For any given id, I
need to look up the item with the most recent date.
>
> So say I have the following input file:
>
> <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 easiest way to store them in a variable first:

<xsl:variable name="items" select="//item[@id= $idref]"/>

then you can get the max of those items:

<xsl:value-of select="normalize-space($items[@date =
max($items/@date/xs:integer(.))])"/>

...but unless you are sure you won't get 2 $items with the same date,
you need to change that to something like:

<xsl:value-of select="$items[@date =
max($items/@date/xs:integer(.))]/normalize-space(.)"/>

so you only ever pass a sequence of one item to the function.

--
Andrew Welch
http://andrewjwelch.com


Current Thread
Keywords