Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3603614
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:53:38+00:00 2026-05-18T20:53:38+00:00

I have work out the example for factorial of given number in xml by

  • 0

I have work out the example for factorial of given number in xml by using xsl.
My XML code is,fact.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="fact.xsl"?>
<numbers>
<number> 5
</number>
</numbers>

and my XSL code is,fact.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:apply-templates>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template name="factorial">
        <xsl:param name="number" select="$number"/>
        <xsl:param name="result" select="1"/>
        <xsl:if test="$number &gt; 1">
            <xsl:call-template name="factorial"> 
                <xsl:with-param name="number" select="$number - 1"/>
                <xsl:with-param name="result">
                    <xsl:value-of select="$number * $result"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:if>   
        <xsl:if test="$number = 1">
            <xsl:value-of select= "$result"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

But, When I run this fact.xml, the factorial output shows only 5 instead of 120.

So what is the error in the code, either in XML or XSL?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:53:38+00:00Added an answer on May 18, 2026 at 8:53 pm

    Your first template matches numbers node, but does nothing.

    Your second template doesn’t match anything, and is never called by any other template. In other words, it is never executed.

    That’s why the output is 5.


    Let’s see what happens. In XSLT, templates can be called in two ways.

    • First, they can match an element of an XML input. Your first template is doing exactly this thing: in your original question, it matched /numbers node; in your revision, it matches any root element (which is, in this case, the same thing).
    • Second, they can be called from a template through call-template. That’s what you are doing in your second template, calling itself recursively.

    The problem is that the second template is called only from… guess… the second template itself. So since it’s never called, it will never execute.

    Now, we will add a call to this template from the outside, and more precisely from the first template.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="numbers">
        <output>
          <!-- Here's a missing call to the second template -->
          <xsl:call-template name="factorial">
            <xsl:with-param name="number" select="number"/>
            <xsl:with-param name="result" select="1"/>
          </xsl:call-template>
        </output>
      </xsl:template>
      <xsl:template name="factorial">
        <xsl:param name="number" />
        <xsl:param name="result" />
        <intermediary number="{$number}">
          <xsl:value-of select="$result"/>
        </intermediary>
        <xsl:if test="$number &gt; 1">
          <xsl:call-template name="factorial">
            <xsl:with-param name="number" select="$number - 1"/>
            <xsl:with-param name="result" select="$number * $result"/>
          </xsl:call-template>
        </xsl:if>
        <xsl:if test="$number = 1">
          <result>
            <xsl:value-of select="$result"/>
          </result>
        </xsl:if>
      </xsl:template>
    </xsl:stylesheet>
    

    We also want to be sure that the template is executing properly, so we output the intermediary states.

    <?xml version="1.0" encoding="utf-8"?>
    <output>
      <intermediary number="&#xA;    5&#xA;  ">1</intermediary>
      <intermediary number="4">5</intermediary>
      <intermediary number="3">20</intermediary>
      <intermediary number="2">60</intermediary>
      <intermediary number="1">120</intermediary>
      <result>120</result>
    </output>
    

    The final result is correct, but the first intermediary state is still weird. It shows that the number 5 was treated as a string, with leading and trailing whitespace.

    To get rid of that, the number element innerXML must be converted to a real number. To do that, you can use the number() method:

    <xsl:call-template name="factorial">
      <xsl:with-param name="number" select="number(number)"/>
      <xsl:with-param name="result" select="1"/>
    </xsl:call-template>
    

    Now, the output is correct from the start to the end, and intermediary output can be removed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to work out how to load a module using example http://dojotoolkit.org/reference-guide/dojo/registerModulePath.html I
I have an odd sorting case I'm struggling to work out using LINQs GroupBy
I am trying to work out the code igniter routing I have a url
I have been working through some code and I would like to work out
I have a jqgrid from trirand (asp.net version) and cannot work out how to
For a long time i have tried to work out the best way to
I have been trying to work out how to call a method in a
i have been looking around to try and work out how to pass 2
I have a generic Collection and am trying to work out how I can
So I have a factory class and I'm trying to work out what the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.