How to create node set from values

divide-and-conquerxslt

How can we create a node set from values….

I have n numbers 1,2,3…….n.

I want to create a node set

<MYNMUMS>
<MYNUM>1</MYNUM>
<MYNUM>2</MYNUM>
<MYNUM>3</MYNUM>
<MYNUM>4</MYNUM>
....
<MYNUM>N</MYNUM>
</MYNMUMS>

Best Answer

As easy as that:

XSLT 1.0 solution:

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="/">
     <MyNums>
       <xsl:call-template name="generateNumNodes">
         <xsl:with-param name="pStart" select="1"/>
         <xsl:with-param name="pEnd" select="10"/>
       </xsl:call-template>
     </MyNums>
    </xsl:template>

    <xsl:template name="generateNumNodes">
      <xsl:param name="pStart"/>
      <xsl:param name="pEnd"/>

      <xsl:if test="$pEnd >= $pStart">
        <xsl:variable name="vNumNodes"
           select="$pStart -$pEnd+1"/>

        <xsl:choose>
          <xsl:when test="$vNumNodes = 1">
            <MyNum><xsl:value-of select="$pStart"/></MyNum>
          </xsl:when>
          <xsl:otherwise>
            <xsl:variable name="vHalf" select=
              "floor(($pStart+$pEnd) div 2)"/>
            <xsl:call-template name="generateNumNodes">
              <xsl:with-param name="pStart" select="$pStart"/>
              <xsl:with-param name="pEnd" select="$vHalf"/>
            </xsl:call-template>

            <xsl:call-template name="generateNumNodes">
              <xsl:with-param name="pStart" select="$vHalf+1"/>
              <xsl:with-param name="pEnd" select="$pEnd"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>
    </xsl:template>
</xsl:stylesheet>

when applied on any XML document (not used), produces the desired output:

<MyNums>
   <MyNum>1</MyNum>
   <MyNum>2</MyNum>
   <MyNum>3</MyNum>
   <MyNum>4</MyNum>
   <MyNum>5</MyNum>
   <MyNum>6</MyNum>
   <MyNum>7</MyNum>
   <MyNum>8</MyNum>
   <MyNum>9</MyNum>
   <MyNum>10</MyNum>
</MyNums>

Do note the following:

  1. The template generateNumNodes calls itself recursively.

  2. This recursion is both time ( O(N) ), and space ( O(log2(N)) ) efficient and practically does overflow the stack -- no SO here!

  3. The above feature is achieved by implementing the recursion in a DVC (DiVide and Conquer) style.

  4. Unlike tail-recursion it will be successfully executed on any compliant XSLT processor.

  5. The maximum recursion depth needed to generate 1000000 (one million numbers) is just 19.

XSLT 2.0 solution:

Even more elementary, no recursion, just using the XPath 2.0 to operator:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
     <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <MyNums>
          <xsl:for-each select="1 to 10">
            <MyNums>
              <xsl:sequence select="."/>
            </MyNums>
          </xsl:for-each>
        </MyNums>
    </xsl:template>
</xsl:stylesheet>
Related Topic