Note that there are some explanatory texts on larger screens.

plurals
  1. POPrint numbers from one to one million
    primarykey
    data
    text
    <p>Assume you have a highly synthetic task to print numbers from 1 to 1.000.000 without appropriate input XML. Of course, straight-forward recursion will fail due to, ironic enough, stack overflow.</p> <p>I came up with the solution listed below:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="text"/&gt; &lt;xsl:variable name="end" select="number(1000000)"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:call-template name="batches"/&gt; &lt;/xsl:template&gt; &lt;xsl:template name="batches"&gt; &lt;xsl:param name="start" select="number(1)"/&gt; &lt;xsl:param name="stop" select="$end"/&gt; &lt;xsl:param name="ololo"/&gt; &lt;xsl:if test="$start &amp;lt;= ($end)"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$stop = 0"&gt; &lt;xsl:value-of select="$start"/&gt;:&lt;xsl:value-of select="$ololo"/&gt; &lt;xsl:text&gt;&amp;#xa;&lt;/xsl:text&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:call-template name="batches"&gt; &lt;xsl:with-param name="start" select="$start"/&gt; &lt;xsl:with-param name="stop" select="floor($stop div 2)"/&gt; &lt;xsl:with-param name="ololo" select=" 'A' "/&gt; &lt;/xsl:call-template&gt; &lt;xsl:call-template name="batches"&gt; &lt;xsl:with-param name="start" select="floor($stop div 2) + $start + 1"/&gt; &lt;xsl:with-param name="stop" select="floor($stop div 2)"/&gt; &lt;xsl:with-param name="ololo" select=" 'B' "/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>It works both in libxslt and MSXML. But it prints some duplicate numbers and looks quite awkward in terms of efficiency. Can this be improved somehow?</p>
    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.
 

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