How to create node set from values


How can we create a node set from values….

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

I want to create a node set


Best Solution

As easy as that:

XSLT 1.0 solution:

This transformation:

<xsl:stylesheet version="1.0"
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
       <xsl:call-template name="generateNumNodes">
         <xsl:with-param name="pStart" select="1"/>
         <xsl:with-param name="pEnd" select="10"/>

    <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:when test="$vNumNodes = 1">
            <MyNum><xsl:value-of select="$pStart"/></MyNum>
            <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 name="generateNumNodes">
              <xsl:with-param name="pStart" select="$vHalf+1"/>
              <xsl:with-param name="pEnd" select="$pEnd"/>

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


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"
     <xsl:output omit-xml-declaration="yes" indent="yes"/>

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