 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 1st, 2011, 12:07 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Index-of in a sequence
I have the variables:
<xsl:variable name="pricesList">0.00,0.49,0.99,1.49,1.99,2.49,2. 99,3.49,3.99,4.49,4.99,5.49,5.99,6.49,6.99,7.49,7. 99,8.49,8.99,9.49,9.99,10.99,11.99,12.99,13.99,14. 99,15.99,16.99,17.99,18.99,19.99,20.99,21.99,22.99 ,23.99,24.99,25.99,26.99,27.99,28.99,29.99,30.99,3 1.99,32.99,33.99,34.99,35.99,36.99,37.99,38.99,39. 99,42.99,44.99,47.99,49.99,52.99,54.99,57.99,59.99 ,64.99,69.99,79.99,89.99,99.99</xsl:variable>
<xsl:variable name="prices" select="tokenize($priceList,',')"/>
I am trying to locate the position of the price that is the next highest given the input. For example, if the input is:
<price>2.55</price>
Then the output would be: 7
Or if
<price>4.15</price>
Then the output would be: 10
Any help would be greatly appreciated.
|
|

March 1st, 2011, 06:42 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Here is an XSLT 2.0 sample:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf"
version="2.0">
<xsl:variable name="pricesList">0.00,0.49,0.99,1.49,1.99,2.49,2.99,3.49,3.99,4.49,4.99,5.49,5.99,6.49,6.99,7.49,7.99,8.49,8.99,9.49,9.99,10.99,11.99,12.99,13.99,14.99,15.99,16.99,17.99,18.99,19.99,20.99,21.99,22.99,23.99,24.99,25.99,26.99,27.99,28.99,29.99,30.99,31.99,32.99,33.99,34.99,35.99,36.99,37.99,38.99,39.99,42.99,44.99,47.99,49.99,52.99,54.99,57.99,59.99 ,64.99,69.99,79.99,89.99,99.99</xsl:variable>
<xsl:variable name="prices" as="xs:double*" select="for $s in tokenize($pricesList, ',') return xs:double($s)"/>
<xsl:function name="mf:index-of" as="xs:integer">
<xsl:param name="numbers" as="xs:double*"/>
<xsl:param name="value" as="xs:double*"/>
<xsl:sequence select="(for $pos in 1 to count($numbers)
return $pos[$numbers[$pos] gt $value])[1]"/>
</xsl:function>
<xsl:template match="price">
<xsl:value-of select="mf:index-of($prices, .)"/>
</xsl:template>
</xsl:stylesheet>
Saxon 9.3, when used against the input
Code:
<prices>
<price>2.55</price>
<price>4.15</price>
</prices>
outputs
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
 |