How to create a node value from values

How can we create a set of node 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>
+3
source share
2 answers

So simple:

XSLT 1.0 Solution :

This conversion is:

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

if applied to any XML document (not used), produces the desired result :

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

Please note the following :

  • The template generateNumNodes calls itself recursively .

  • This recursion is time ( O(N) ), and space is ( O(log2(N)) )efficient and almost overflows the stack - not here!

  • DVC (DiVide and Conquer) .

  • tail-recursion , XSLT-.

  • , 1000000 ( ), 19.

XSLT 2.0:

, , XPath 2.0 to:

<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>
+8

XSLT - . , XML-, ( XML).

, "" XML, XSLT .

, .

0

Source: https://habr.com/ru/post/1706944/


All Articles