Note that there are some explanatory texts on larger screens.

plurals
  1. POxslt recursive multiplication
    primarykey
    data
    text
    <p>The xml document below represents 3 number, 2, 2 and 2. A node <code>&lt;s&gt;</code> is counted as a number and ended with <code>&lt;zero/&gt;</code>.</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;nat xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="nat.xsd"&gt; &lt;s&gt; &lt;s&gt; &lt;zero/&gt; &lt;/s&gt; &lt;/s&gt; &lt;s&gt; &lt;s&gt; &lt;zero/&gt; &lt;/s&gt; &lt;/s&gt; &lt;s&gt; &lt;s&gt; &lt;zero/&gt; &lt;/s&gt; &lt;/s&gt; &lt;/nat&gt; </code></pre> <p>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.</p> <p>The expected answer for above xml doc is 8s(ignore the format) :</p> <p><code>&lt;s&gt;&lt;s&gt;&lt;s&gt;&lt;s&gt;&lt;s&gt;&lt;s&gt;&lt;s&gt;&lt;s&gt;&lt;zero/&gt;&lt;/s&gt;&lt;/s&gt;&lt;/s&gt;&lt;/s&gt;&lt;/s&gt;&lt;/s&gt;&lt;/s&gt;&lt;/s&gt;</code> 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. </p> <p><strong>Update:</strong> I got my answer by adding in a print template to Dimitre's answer. Here it is:</p> <pre><code> &lt;xsl:template name="print"&gt; &lt;xsl:param name="pAccum"/&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$pAccum &gt; 0"&gt; &lt;s&gt; &lt;xsl:call-template name="print"&gt; &lt;xsl:with-param name="pAccum" select="$pAccum - 1"/&gt; &lt;/xsl:call-template&gt; &lt;/s&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;zero/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload