# Use pure XPath to test a sequence for being a valid F

 Subject: Use pure XPath to test a sequence for being a valid Fibonacci sequence From: Abel Braaksma Date: Wed, 31 Jan 2007 18:12:44 +0100
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```

### PURCHASE STYLUS STUDIO ONLINE TODAY!

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