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

RE: Using an index with a sorted list of elements

Subject: RE: Using an index with a sorted list of elements
From: "Conal Tuohy" <conalt@xxxxxxxxxxxxxxx>
Date: Wed, 19 Mar 2003 20:38:17 +1200
find element in sorted list
Hugh Dixon wrote:

> I have a number of elements, similar to <element @value/>
> I want to find the element that has the biggest value of "value" less
> than a value $upperBound.
> Eg
> <elements>
> <element value = "10"/>
> <element value = "80"/>
> <element value = "100"/>
> <element value = "5"/>
> </elements>
>
> If $upperBound is 85, I want <element value = "80"/>
> If $upperBound is  7, I want <element value = "5"/>
>
> I am trying to do something like:
> 	<xsl:for-each select="element[$upperBound>./@value][1]">
> 		<xsl:sort data-type="number" select="./@value"
> order="descending"/>
> 			<xsl:value-of select="."/>
> 	</xsl:for-each>

I tried to test your system, but I noticed for a start that you are using
<xsl:value-of select="."> to output the string value of the element which
matches, but ... the elements have no string value because they are empty.
I'm sure in your real data the elements have content! ;-)

Anyway ... a solution:

You could select the element you want with a single xpath expression. You
want the first element, whose value is less than the upperbound, and whose
value is NOT less than the value of the OTHER elements whose values are less
than the upperbound. That is to say:

element[(@value &lt; $upperBound)][not(@value &lt; ../element[(@value &lt;
$upperBound)]/@value)]

Or equivalently, using a variable to refer to the bounded set of the nodes <
upperBound:

<xsl:variable
	name="bounded"
	select="element[(@value &lt; $upperBound)]"
/>
<xsl:copy-of
	select="$bounded[not(@value &lt; $bounded/@value)][1]"
/>

Cheers!

Con


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.