Use XSLT to obtain the Fibonacci sequence

Source: Internet
Author: User
Tags xsl xslt

In the Fibonacci series, all digits except 1st and 2 are the sum of the first two digits, for example, 13...
XSLT (XSLT is a language for transforming XML documents into XHTML documents or to other XML documents) is a language for XML Conversion. There are some basic process controls in XSLT, such as <XSL: If> <XSL: Choose> <XSL: When> <XSL: otherwise>.
We know that to implement the Fibonacci series, we can call the series cyclically or recursively. The following describes how to use XSLT to output the Fibonacci series and obtain the nth value of the Fibonacci series. (It should be noted that because the number of the Fibonacci series is growing very fast, N should be as small as possible)
1. output the Fibonacci series
<? XML version = "1.0" encoding = "UTF-8"?>

<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: template match = "/">
<XSL: Call-Template Name = "repeat">
<XSL: With-Param name = "times" select = "5"/>
</XSL: Call-template>
</XSL: Template>
<XSL: Template Name = "repeat">
<XSL: Param name = "times" select = "0"/>
<XSL: Param name = "f1" select = "1"/>
<XSL: Param name = "F2" select = "1"/>
<XSL: If test = "$ times & gt 0">
<XSL: value-of select = "$ F1"/>
<XSL: Text>, </XSL: Text>
<XSL: value-of select = "$ F2"/>
<XSL: Text>, </XSL: Text>
<XSL: Call-Template Name = "repeat">
<XSL: With-Param name = "times" select = "$ times-1"/>
<XSL: With-Param name = "f1" select = "$ F1 + $ F2"/>
<XSL: With-Param name = "F2" select = "$ F2 + $ F1 + $ F2"/>
</XSL: Call-template>
</XSL: If>
</XSL: Template>
</XSL: stylesheet>
2. The value of item n in the Fibonacci series
<? XML version = "1.0" encoding = "UTF-8"?>

<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: template match = "/">
<XSL: Call-Template Name = "Fibonacci">
<XSL: With-Param name = "N" select = "$ N"/>
</XSL: Call-template>
</XSL: Template>

<XSL: Template Name = "Fibonacci">
<XSL: Param name = "N"/>
<XSL: Choose>
<XSL: When test = "$ N & lt; = 0">
<XSL: Text> 0 </XSL: Text>
</XSL: When>

<XSL: When test = "$ n = 1">
<XSL: Text> 1 </XSL: Text>
</XSL: When>
<XSL: otherwise>
<XSL: Call-Template Name = "fiboloop">
<XSL: With-Param name = "fn1" select = "1"/>
<XSL: With-Param name = "FN2" select = "0"/>
<XSL: With-Param name = "remains" select = "$ n-2"/>
</XSL: Call-template>
</XSL: otherwise>
</XSL: Choose>
</XSL: Template>

 
<XSL: Template Name = "fiboloop">
<XSL: Param name = "fn1"/>
<XSL: Param name = "FN2"/>
<XSL: Param name = "remains"/>
<XSL: variable name = "current" select = "$ fn1 + $ FN2"/>
<XSL: Choose>
<XSL: When test = "$ remains = 0">
<XSL: value-of select = "$ current"/>
</XSL: When>
<XSL: otherwise>
<XSL: Call-Template Name = "fiboloop">
<XSL: With-Param name = "fn1" select = "$ current"/>
<XSL: With-Param name = "FN2" select = "$ fn1"/>
<XSL: With-Param name = "remains" select = "$ remains-1"/>
</XSL: Call-template>
</XSL: otherwise>
</XSL: Choose>
</XSL: Template>
</XSL: stylesheet>
(Note: The above code may come from the network. This is the code I found on my computer. Haha)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.