[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Filter by id and date

Subject: Re: Filter by id and date
From: "Tony Graham" <tgraham@xxxxxxxxxx>
Date: Mon, 5 Sep 2011 11:18:58 +0100 (IST)
Re:  Filter by id and date
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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.