Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old March 1st, 2011, 12:07 AM
Authorized User
 
Join Date: May 2007
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
 
Old March 1st, 2011, 06:42 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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
Code:
7
10
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT-escape-sequence sunilkswain XSLT 7 March 23rd, 2009 03:03 AM
how to create sequence no in c# bettiahamit .NET Framework 3.5 1 November 6th, 2008 06:25 AM
comparison using a sequence pcase XSLT 1 December 3rd, 2007 07:33 PM
value from a sequence suhabassam JSP Basics 0 September 27th, 2005 01:33 AM
Sequence of For-each elements? brendonsmith XSLT 2 January 14th, 2004 05:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.