views:

59

answers:

1

Hi, 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"&gt;
 <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"&gt;
<test document="dump">
<Person position="1">
    <properties>
        <name>John</name>
        <number>1</number>
    </properties>
</Person>
</test>
</Batch>
<Batch xmlns="http://test.com"&gt;
<test document="dump">
    <Person position="1">
        <properties>
            <name>John</name>
            <number>1</number>
        </properties>
    </Person>
</test>
</Batch>

Any clue?

+1  A: 

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"&gt;
    <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>
Dimitre Novatchev
great...it works but I still need to understand thoroughly this solution...anyway, it helps a lot for me to learn.
Daniel