xsl-list
[Top] [All Lists]

[xsl] Use pure XPath to test a sequence for being a valid Fibonacci sequence

2007-01-31 10:14:16
Hi Xslt'ers,

I was wondering about the following: using some/every XPath expressions, is it possible to test values in a sequence against preceding values? The requirement pops up when you want to test pairs of values, where, say, the even indexed value must be greater than the odd indexed value, etc. I often have this requirement, and although it is resolvable with for-each and/or nested for-in-return, I was looking for a simpler solution.

Since many people understand the requirements of Fibonacci numbers, I think it is a good example use case and represents my actual use case pretty well (without the blur):

0, 1, 1, 2, 3, 5, 8, 13, 21...

Now, for each given Fibonacci number, it is correct if:

F(n) = F(n-1) + F(n-2)

For positions 1 and 2, if the value for F(n-1) and F(n-2) are zero.

This is what I could think of. It is not working, because index-of() function return two indices for the the number '1' (because it appears twice). It works if I omit these numbers, but then I cannot apply the 'every'. Does someone know of a way to use some/every instructions in a way that it satisfies the Fibonacci series above with 'every'? Perhaps it is simply not possible (or requires an awkward construction)?

some $i in $fib satisfies
   $fib[index-of($fib, $i) - 1] + $fib[index-of($fib, $i) - 2] = $i

$fib was defined as:
<xsl:variable name="fib" as="xs:integer*" select="(0, 1, 1, 2, 3, 5, 8, 13, 21)" />

Cheers,
-- Abel Braaksma
  http://www.nuntia.nl




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--