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

  • SEARCH
  • Home
  • 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 8344203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:17:30+00:00 2026-06-09T06:17:30+00:00

The xml document below represents 3 number, 2, 2 and 2. A node <s>

  • 0

The xml document below represents 3 number, 2, 2 and 2. A node <s> is counted as a number and ended with <zero/>.

    <?xml version="1.0" encoding="UTF-8"?>
    <nat xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="nat.xsd">
      <s>
        <s>
          <zero/>
        </s>
      </s>
      <s>
        <s>
          <zero/>
        </s>
      </s>
      <s>
        <s>
          <zero/>
        </s>
      </s>
    </nat>

I just started learning with xslt and this is one of the exercise for recursion. I could do plus recursively for adding up all numbers but this multiplying more than two number just blows my mind. I have no idea how to do it.

The expected answer for above xml doc is 8s(ignore the format) :

<s><s><s><s><s><s><s><s><zero/></s></s></s></s></s></s></s></s>
My idea was this, I can have a template to do multiplication for two number by adding. So for this 2x2x2, I would do the 2nd 2 times the 3rd 2 that returns 4 and finally do 2*4. But call template don’t return value in xslt unlike java or scheme so I appreciate any hints/helps.

Update:
I got my answer by adding in a print template to Dimitre’s answer. Here it is:

    <xsl:template name="print">
    <xsl:param name="pAccum"/>
        <xsl:choose>
            <xsl:when test="$pAccum > 0">
                <s>
                    <xsl:call-template name="print">
                        <xsl:with-param name="pAccum" select="$pAccum - 1"/>
                    </xsl:call-template>
                </s>
             </xsl:when>
             <xsl:otherwise>
                <zero/>
            </xsl:otherwise>    
        </xsl:choose>
</xsl:template>
  • 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-06-09T06:17:32+00:00Added an answer on June 9, 2026 at 6:17 am

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
      <xsl:call-template name="product">
       <xsl:with-param name="pArgs" select="//zero"/>
      </xsl:call-template>
     </xsl:template>
    
     <xsl:template name="product">
      <xsl:param name="pAccum" select="1"/>
      <xsl:param name="pArgs" select="/.."/>
    
      <xsl:choose>
       <xsl:when test="not($pArgs)">
        <xsl:value-of select="$pAccum"/>
       </xsl:when>
       <xsl:otherwise>
        <xsl:call-template name="product">
         <xsl:with-param name="pAccum"
              select="$pAccum * count($pArgs[1]/ancestor::s)"/>
         <xsl:with-param name="pArgs" select="$pArgs[position() > 1]"/>
        </xsl:call-template>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <nat>
        <s>
            <s>
                <zero/>
            </s>
        </s>
        <s>
            <s>
                <zero/>
            </s>
        </s>
        <s>
            <s>
                <zero/>
            </s>
        </s>
    </nat>
    

    produces the wanted, correct result:

    8
    

    Explanation:

    Primitive recursion with stop condition — empty argument node-set and an accumulator – parameter for passing the currently accumulated result to the next recursive call.

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

Sidebar

Related Questions

Hi I'm reading in an XML document like below <?xml version=1.0 encoding=utf-8?> <ArrayOfSYMBOL xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
I have the following XML Document: <?xml version=\1.0\ encoding=\UTF-8\?> <atom:entry xmlns:atom=\http://www.w3.org/2005/Atom\ xmlns:apps=\http://schemas.google.com/apps/2006\ xmlns:gd=\http://schemas.google.com/g/2005\> <apps:property
My XML Document is as below - <?xml version=1.0 encoding=utf-8?> <Parents> <Parent id=A description=A
I have an XML document as below: <?xml version=1.0 encoding=utf-8 ?> <Providers> <Provider> <ProviderType>Email</ProviderType>
This is XML Document. <?xml version=1.0 encoding=UTF-8 standalone=yes?> <w:document xmlns:w=http://schemas.openxmlformats.org/wordprocessingml/2006/main> <w:body> <w:p> <w:pPr> <w:pStyle
I've xml as below, <?xml version=1.0 encoding=utf-16 ?> <AllResidentAndUnitInfo xmlns:i=http://www.w3.org/2001/XMLSchema-instance i:type=ResidentsByUnitInfo xmlns=http://schemas.datacontract.org/2004/07/FSRSchema> <BillingAddresses> <BillingAddress>
XML document: <?xml version=1.0 encoding=UTF-8?> <ProcessData> <SOAPAction>urn:echo</SOAPAction> <Content_Type>text/xml;charset=UTF-8</Content_Type> <uname>sarah_brcm</uname> <pwd>BRCM_UVLwNhjrA5fbgqkUNdxQXMfcCDJ</pwd> </ProcessData> XSLT: <?xml version=1.0
Below is the XML schema I am trying to conform to: <?xml version=1.0 encoding=UTF-8?>
I have following CDATA inside xml document: <![CDATA[ <p xmlns=>Refer to the below: <br/>
I want to extract all comments below a specific node within an XML document,

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.