Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 821347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:37:32+00:00 2026-05-15T02:37:32+00:00

I have a xml as below that I’d like to copy n times while

  • 0

I have a xml as below that I’d like to copy n times while incrementing one of its element and one of its attribute.

XML input:

<Person position=1>
<name>John</name>
<number>1</number>
<number>1</number>
</Person>

and I’d like something like below with the number of increment to be a variable.

XML output:

<Person position=1>
<name>John</name>
<number>1</number>
</Person>
<Person position=2>
<name>John</name>
<number>2</number>
</Person>
....
<Person position=n>
<name>John</name>
<number>n</number>
</Person>

Any clue

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:37:32+00:00Added an answer on May 15, 2026 at 2:37 am

    I. XSLT 1.0 solution:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pTimes" select="5"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*">
      <xsl:call-template name="applyNTimes">
        <xsl:with-param name="pTimes" select="$pTimes"/>
        <xsl:with-param name="pPosition" select="1"/>
      </xsl:call-template>
     </xsl:template>
    
     <xsl:template name="applyNTimes">
      <xsl:param name="pTimes" select="0"/>
      <xsl:param name="pPosition" select="1"/>
    
      <xsl:if test="$pTimes > 0">
       <xsl:apply-templates select="*">
        <xsl:with-param name="pPosition" select="$pPosition"/>
       </xsl:apply-templates>
    
       <xsl:call-template name="applyNTimes">
        <xsl:with-param name="pTimes" select="$pTimes -1"/>
        <xsl:with-param name="pPosition" select="$pPosition+1"/>
       </xsl:call-template>
      </xsl:if>
     </xsl:template>
    
     <xsl:template match="Person">
      <xsl:param name="pPosition" select="1"/>
    
      <Person position="{$pPosition}">
       <xsl:apply-templates>
         <xsl:with-param name="pPosition" select="$pPosition"/>
       </xsl:apply-templates>
      </Person>
     </xsl:template>
    
     <xsl:template match="number">
      <xsl:param name="pPosition" select="1"/>
    
      <number><xsl:value-of select="$pPosition"/></number>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the following XML document:

    <t>
        <Person position="1">
            <name>John</name>
            <number>1</number>
        </Person>
    </t>
    

    the wanted result is produced:

    <Person position="1">
       <name>John</name>
       <number>1</number>
    </Person>
    <Person position="2">
       <name>John</name>
       <number>2</number>
    </Person>
    <Person position="3">
       <name>John</name>
       <number>3</number>
    </Person>
    <Person position="4">
       <name>John</name>
       <number>4</number>
    </Person>
    <Person position="5">
       <name>John</name>
       <number>5</number>
    </Person>
    

    II. XSLT 2.0 solution:

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     exclude-result-prefixes="xs"
     >
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:param name="pNumTimes" as="xs:integer" select="5"/>
    
      <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/">
       <xsl:variable name="vDoc" select="/"/>
    
         <xsl:for-each select="1 to $pNumTimes">
           <xsl:apply-templates select="$vDoc/*/*">
            <xsl:with-param name="pPosition" select="."/>
           </xsl:apply-templates>
         </xsl:for-each>
     </xsl:template>
    
     <xsl:template match="Person">
      <xsl:param name="pPosition" select="1"/>
    
      <Person position="{$pPosition}">
       <xsl:apply-templates>
         <xsl:with-param name="pPosition" select="$pPosition"/>
       </xsl:apply-templates>
      </Person>
     </xsl:template>
    
     <xsl:template match="number">
      <xsl:param name="pPosition" select="1"/>
    
      <number><xsl:value-of select="$pPosition"/></number>
     </xsl:template>
     </xsl:stylesheet>
    

    III. Using a DVD-style recursion to avoid stack overflow on large N (XSLT 1.0)

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pTimes" select="5"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*">
      <xsl:call-template name="applyNTimes">
        <xsl:with-param name="pTimes" select="$pTimes"/>
        <xsl:with-param name="pPosition" select="1"/>
      </xsl:call-template>
     </xsl:template>
    
     <xsl:template name="applyNTimes">
      <xsl:param name="pTimes" select="0"/>
      <xsl:param name="pPosition" select="1"/>
    
      <xsl:if test="$pTimes > 0">
       <xsl:choose>
         <xsl:when test="$pTimes = 1">
               <xsl:apply-templates select="*">
                <xsl:with-param name="pPosition" select="$pPosition"/>
               </xsl:apply-templates>
         </xsl:when>
         <xsl:otherwise>
           <xsl:variable name="vHalf" select="floor($pTimes div 2)"/>
    
           <xsl:call-template name="applyNTimes">
            <xsl:with-param name="pTimes" select="$vHalf"/>
            <xsl:with-param name="pPosition" select="$pPosition"/>
           </xsl:call-template>
    
           <xsl:call-template name="applyNTimes">
            <xsl:with-param name="pTimes" select="$pTimes - $vHalf"/>
            <xsl:with-param name="pPosition" select="$pPosition + $vHalf"/>
           </xsl:call-template>
         </xsl:otherwise>
       </xsl:choose>
      </xsl:if>
     </xsl:template>
    
     <xsl:template match="Person">
      <xsl:param name="pPosition" select="1"/>
    
      <Person position="{$pPosition}">
       <xsl:apply-templates>
         <xsl:with-param name="pPosition" select="$pPosition"/>
       </xsl:apply-templates>
      </Person>
     </xsl:template>
    
     <xsl:template match="number">
      <xsl:param name="pPosition" select="1"/>
    
      <number><xsl:value-of select="$pPosition"/></number>
     </xsl:template>
    </xsl:stylesheet>
    

    The problem with deep recursion is the exhaustion of the call stack resulting in stack overflow. On different systems this happens with different levels of nesting, but this is typical at around N = 1000.

    The above transformation practically has no such problem. The maximum recursion depth with DVC is log2(N), which means that if we need to repeat the action 1000000 (1M) times, the maximum recursion depth is only 19.

    IV. Finally, how to avoid recursion at all (XSLT 1.0)

    For known, not too big N, the following non-recursive technique works well:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pTimes" select="5"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*">
      <xsl:call-template name="applyNTimes">
        <xsl:with-param name="pTimes" select="$pTimes"/>
      </xsl:call-template>
     </xsl:template>
    
     <xsl:template name="applyNTimes">
      <xsl:param name="pTimes" select="0"/>
    
     <xsl:variable name="vCurrent" select="."/>
    
      <xsl:if test="$pTimes > 0">
       <xsl:for-each select=
         "document('')//node() | document('')//@* | document('')//namespace::*">
        <xsl:if test="not( position() > $pTimes )">
            <xsl:apply-templates select="$vCurrent/*">
             <xsl:with-param name="pPosition" select="position()"/>
            </xsl:apply-templates>
        </xsl:if>
       </xsl:for-each>
      </xsl:if>
     </xsl:template>
    
     <xsl:template match="Person">
      <xsl:param name="pPosition" select="1"/>
    
      <Person position="{$pPosition}">
       <xsl:apply-templates>
         <xsl:with-param name="pPosition" select="$pPosition"/>
       </xsl:apply-templates>
      </Person>
     </xsl:template>
    
     <xsl:template match="number">
      <xsl:param name="pPosition" select="1"/>
    
      <number><xsl:value-of select="$pPosition"/></number>
     </xsl:template>
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a xml as below that I'd like to copy n times while
So I have xml that looks like this: <todo-list> <id type=integer>#{id}</id> <name>#{name}</name> <description>#{description}</description> <project-id
I'm attempting to remove Component elements from the XML below that have File children
I have a xml given below: <root title=الصفحة الرئيسة> <item title=الصفحة الرئيسة itemuri=tcm:8-29-4 ShowInNav=True
I have xml where some of the element values are unicode characters. Is it
I have XML files in a directory that I wish to get over to
I have XML with a value like the following: <products> <product id=1 name=All Products>
My question is the following. I have xml that is versioned by a namespace.
I'm trying to execute an E4X query (on the xml below) that will return
I have a page (below) that has a datagrid that lists item's returned from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.