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 846267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:35:12+00:00 2026-05-15T06:35:12+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:

<?xml version="1.0"?>
<header xmlns="http://test.com" >
<Batch>
<test document="dump" >
<Person position=1>
    <properties>
        <name>John</name>
        <number>1</number>
    </properties>
</Person>
</test>
</Batch>
</header>

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

XML output:

<?xml version="1.0"?>
<header xmlns="http://test.com" >
<Batch>
<test document="dump" >
<Person position=1>
    <properties>
        <name>John</name>
        <number>1</number>
    </properties>
</Person>
<Person position=2>
    <properties>
        <name>John</name>
        <number>2</number>
    </properties>
</Person>
...
<Person position=n>
    <properties>
        <name>John</name>
        <number>n</number>
    </properties>
</Person>
</test>
</Batch>
</header>

To solve this, I’ve started with the xslt below:

 <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="2"/>


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

     <xsl:value-of select="$newline"/>
     <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>

but the output includes the namespace in elements. The element and attribute @position are always set to 1. Also, the header surrounds each element.
Please refer to the output below with n=2

<Batch xmlns="http://test.com">
<test document="dump">
<Person position="1">
    <properties>
        <name>John</name>
        <number>1</number>
    </properties>
</Person>
</test>
</Batch>
<Batch xmlns="http://test.com">
<test document="dump">
    <Person position="1">
        <properties>
            <name>John</name>
            <number>1</number>
        </properties>
    </Person>
</test>
</Batch>

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-15T06:35:13+00:00Added an answer on May 15, 2026 at 6:35 am

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:t="http://test.com"
    >
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
         <xsl:strip-space elements="*"/>
    
         <xsl:param name="pTimes" select="2"/>
    
         <xsl:template match="node()|@*">
          <xsl:param name="pPosition" select="1"/>
          <xsl:copy>
           <xsl:apply-templates select="node()|@*">
             <xsl:with-param name="pPosition" select="$pPosition"/>
           </xsl:apply-templates>
          </xsl:copy>
         </xsl:template>
    
         <xsl:template match="t:test">
             <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="t:Person">
             <xsl:param name="pPosition" select="1"/>
    
             <xsl:copy>
                 <xsl:copy-of select="@*"/>
                 <xsl:attribute name="position">
                  <xsl:value-of select="$pPosition"/>
                 </xsl:attribute>
                 <xsl:apply-templates>
                 <xsl:with-param name="pPosition" select="$pPosition"/>
                 </xsl:apply-templates>
              </xsl:copy>
         </xsl:template>
    
         <xsl:template match="t:number">
              <xsl:param name="pPosition" select="1"/>
                <xsl:copy>
                  <xsl:value-of select="$pPosition"/>
                </xsl:copy>
         </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <header xmlns="http://test.com" >
        <Batch>
            <test document="dump" >
                <Person position="1">
                    <properties>
                        <name>John</name>
                        <number>1</number>
                    </properties>
                </Person>
            </test>
        </Batch>
    </header>
    

    produces the wanted results:

    <header xmlns="http://test.com">
        <Batch>
            <Person position="1">
                <properties>
                    <name>John</name>
                    <number>1</number>
                </properties>
            </Person>
            <Person position="2">
                <properties>
                    <name>John</name>
                    <number>2</number>
                </properties>
            </Person>
        </Batch>
    </header>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have xml that looks like this: <todo-list> <id type=integer>#{id}</id> <name>#{name}</name> <description>#{description}</description> <project-id
I have: An XML with some elements. A sub-element that may or may not
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 have a xml as below. <list> <map> <map name=CLAIM_TICKET_1> <val name=CLAIM_TICKET_Groups>Kalyan</val> <val name=Testing>Ram</val>
I have the below view that I need to create. I have the following
I have xml files I want to read in and store in the database.

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.