views:

765

answers:

2

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>
A: 

XSLT is a transformation language. It is usually used when you already have some data in the form of an XML document, that you wish to transform into a different document (that may or may not be in XML format).

For the task of starting with "raw" data and generating an XML representation, XSLT is not well-suited.

I suggest you look into different langauges to solve this.

unwind
Dimitre Novatchev
+1  A: 

As easy as that:

XSLT 1.0 solution:

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <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>
Dimitre Novatchev