XSLT: repeat the input n times, copy the input, but change some attributes, depending on the passed parameters

I am a beginner XSLT and need some help (XSLT 1.0). Basically I need to repeat the input a given number of times, copying it as it is, but changing the values โ€‹โ€‹of some attributes.

I saw that the "standard way" of doing this is to use the apply and xsl: copy patterns, but the problem is that I need to change the values โ€‹โ€‹of these attributes depending on the parameters passed from the "top level", so I think I need use call pattern but I just can't figure it out :)

Input:

<Repeater repeatCount="3">
  <!-- Only one direct child of Repeater (e.g. Panel or TextBox).
       Types of children are unknown but all children have id attribute.
       Everything should be copied as-is except for:
       1) All Repeater children id should be suffixed with row number, e.g.:
         LastName becomes LastName_1, LastName_2, and so on...
       2) Repeater direct child (here: MainPanel) top attribute value should be accumulated from previous top+height -->
  <Panel id="MainPanel" top="0" left="0" width="160" height="12">
    <Panel id="FirstChildPanel" top="0" left="0" width="80" height="12">
      <TextBox id="FirstName" top="0" left="0" width="30" height="12" />
      <TextBox id="LastName" top="0" left="35" width="30" height="12" />
    </Panel>
    <Panel id="SecondChildPanel" top="12" left="80" width="80" height="12">
      <TextBox id="Address" top="0" left="0" width="70" height="12" />
    </Panel>
  </Panel>
</Repeater>

      

Desired output:

<!-- Repeater content is repeated 3 times.
     All ids are suffixed with a row number.
     The outermost repeated element (e.g. MainPanel) has increasing top value -->
<Panel id="MainPanel_1" top="0" left="0" width="160" height="12">
  <Panel id="FirstChildPanel_1" top="0" left="0" width="80" height="12">
    <TextBox id="FirstName_1" top="0" left="0" width="30" height="12" />
    <TextBox id="LastName_1" top="0" left="35" width="30" height="12" />
  </Panel>
  <Panel id="SecondChildPanel_1" top="12" left="80" width="80" height="12">
    <TextBox id="Address_1" top="0" left="0" width="70" height="12" />
  </Panel>
</Panel>
<Panel id="MainPanel_2" top="12" left="0" width="160" height="12">
  <Panel id="FirstChildPanel_2" top="0" left="0" width="80" height="12">
    <TextBox id="FirstName_2" top="0" left="0" width="30" height="12" />
    <TextBox id="LastName_2" top="0" left="35" width="30" height="12" />
  </Panel>
  <Panel id="SecondChildPanel_2" top="12" left="80" width="80" height="12">
    <TextBox id="Address_2" top="0" left="0" width="70" height="12" />
  </Panel>
</Panel>
<Panel id="MainPanel_3" top="24" left="0" width="160" height="12">
  <Panel id="FirstChildPanel_3" top="0" left="0" width="80" height="12">
    <TextBox id="FirstName_3" top="0" left="0" width="30" height="12" />
    <TextBox id="LastName_3" top="0" left="35" width="30" height="12" />
  </Panel>
  <Panel id="SecondChildPanel_3" top="12" left="80" width="80" height="12">
    <TextBox id="Address_3" top="0" left="0" width="70" height="12" />
  </Panel>
</Panel>

      

XSLT (wrong):

  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="Repeater">
    <xsl:apply-templates />
  </xsl:template>

  <!-- repeat first direct child of Repeater -->
  <xsl:template match="Repeater/*[1]">
    <xsl:call-template name="ContentTemplate">
      <xsl:with-param name="count" select="../@repeatCount" />
      <xsl:with-param name="top" select="@top" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="ContentTemplate">
    <xsl:param name="index" select="1"></xsl:param>
    <xsl:param name="count" select="1"></xsl:param>
    <xsl:param name="top"></xsl:param>
    <!-- generate suffix for id from current iteration index -->
    <xsl:variable name="rowId" select="concat('_', $index)"></xsl:variable>
    <xsl:variable name="calculatedTop">
      <xsl:choose>
        <xsl:when test="$index = 1">
          <xsl:value-of select="$top"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$top + ./*[1]/@height" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- iterate Repeater/@repeatCount times. -->
    <xsl:if test="$index &lt;= $count">

      <!-- should just be copying Repeater direct child as-is (unknown element type), but change id and top -->
      <Panel id="{concat(@id, $rowId)}"
             top="{$calculatedTop}"
             left="{@left}"
             width="{@width}"
             height="{@height}">

        <!-- should copy all children as-is, but change their id -->
        <xsl:call-template name="CopyTemplate">
          <xsl:with-param name="currentNode" select="."></xsl:with-param>
          <xsl:with-param name="rowId" select="$rowId"></xsl:with-param>
        </xsl:call-template>

      </Panel>

      <!-- call recursively passing over new row index and calculatedTop -->
      <xsl:call-template name="ContentTemplate">
        <xsl:with-param name="index" select="$index + 1" />
        <xsl:with-param name="count" select="$count" />
        <xsl:with-param name="top" select="$calculatedTop" />
      </xsl:call-template>

    </xsl:if>

  </xsl:template>

  <!-- this is all wrong :) -->
  <xsl:template name="CopyTemplate" match="@*|node()" mode="copy">
    <xsl:param name="currentNode"></xsl:param>
    <xsl:param name="rowId"></xsl:param>

    <xsl:for-each select="$currentNode/node()">

      <xsl:choose>
        <xsl:when test="name(current()) = 'id'">
          <xsl:attribute name="id">
            <xsl:value-of select="concat(@id, $rowId)"/>
          </xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy-of select="current()"/>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:call-template name="CopyTemplate">
        <xsl:with-param name="currentNode" select="current()"/>
        <xsl:with-param name="rowId" select="$rowId"></xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>

  </xsl:template>

      

So I got the iteration part, but can't figure out exactly how to make a copy, so the entire suffix id contains the current line number.

Any help is greatly appreciated :)

+3


source to share


2 answers


I suggest you try it like this:

XSLT 1.0



<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/Repeater">
    <root>
        <xsl:call-template name="repeat">
            <xsl:with-param name="n" select="@repeatCount" />
        </xsl:call-template>
    </root>     
</xsl:template>

<xsl:template name="repeat">
    <xsl:param name="n"/>
    <xsl:if test="$n > 0">
        <!-- recursive call -->
        <xsl:call-template name="repeat">
            <xsl:with-param name="n" select="$n - 1" />
        </xsl:call-template>
        <!-- apply templates with current $n -->        
        <xsl:apply-templates>
            <xsl:with-param name="n" select="$n" />
            <xsl:with-param name="h" select="*/@height" />
        </xsl:apply-templates>
    </xsl:if>
</xsl:template>

<!-- identity transform (modified to carry parameters) -->
<xsl:template match="@*|node()">
    <xsl:param name="n"/>
    <xsl:param name="h"/>
    <xsl:copy>
        <xsl:apply-templates select="@*|node()">
            <xsl:with-param name="n" select="$n" />
            <xsl:with-param name="h" select="$h" />
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<!-- append $n to all ids -->
<xsl:template match="@id">
    <xsl:param name="n"/>
    <xsl:attribute name="id">
        <xsl:value-of select="concat(., '_', $n)"/>
    </xsl:attribute>
</xsl:template>

<!-- increase top -->
<xsl:template match="/Repeater/*/@top">
    <xsl:param name="n"/>
    <xsl:param name="h"/>
    <xsl:attribute name="top">
        <xsl:value-of select="$h * ($n - 1)"/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

      

+1


source


Here's another option. It's not as clean as Michael if you need to add further transformation, but it's pretty straightforward.

XML input

<Repeater repeatCount="3">
    <!-- Only one direct child of Repeater (e.g. Panel or TextBox).
       Types of children are unknown but all children have id attribute.
       Everything should be copied as-is except for:
       1) All Repeater children id should be suffixed with row number, e.g.:
         LastName becomes LastName_1, LastName_2, and so on...
       2) Repeater direct child (here: MainPanel) top attribute value should be accumulated from previous top+height -->
    <Panel id="MainPanel" top="0" left="0" width="160" height="12">
        <Panel id="FirstChildPanel" top="0" left="0" width="80" height="12">
            <TextBox id="FirstName" top="0" left="0" width="30" height="12" />
            <TextBox id="LastName" top="0" left="35" width="30" height="12" />
        </Panel>
        <Panel id="SecondChildPanel" top="12" left="80" width="80" height="12">
            <TextBox id="Address" top="0" left="0" width="70" height="12" />
        </Panel>
    </Panel>
</Repeater>

      

XSLT 1.0



<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="repeat" select="/Repeater/@repeatCount"/>

    <xsl:template match="/Repeater">
        <results>
            <xsl:apply-templates select="*[1]">
                <xsl:with-param name="repeatCount" select="1"/>
                <xsl:with-param name="topLevel" select="true()"/>
            </xsl:apply-templates>            
        </results>
    </xsl:template>

    <xsl:template match="*">
        <xsl:param name="repeatCount"/>
        <xsl:param name="topLevel"/>
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:if test="@id"><!--Override id attribute if it exists.-->
                <xsl:attribute name="id">
                    <xsl:value-of select="concat(@id,'_',$repeatCount)"/>
                </xsl:attribute>                
            </xsl:if>
            <xsl:if test="$topLevel and @top"><!--Override top attribute if it exists.-->
                <xsl:attribute name="top">
                    <xsl:value-of select="($repeatCount - 1) * @height"/>
                </xsl:attribute>                
            </xsl:if>
            <xsl:apply-templates select="*">
                <xsl:with-param name="repeatCount" select="$repeatCount"/>
                <xsl:with-param name="topLevel" select="false()"/>
            </xsl:apply-templates>
        </xsl:copy>
        <xsl:if test="$topLevel and $repeat > $repeatCount">
            <xsl:apply-templates select=".">
                <xsl:with-param name="repeatCount" select="$repeatCount + 1"/>
                <xsl:with-param name="topLevel" select="true()"/>
            </xsl:apply-templates>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

      

XML output

<results>
   <Panel id="MainPanel_1" top="0" left="0" width="160" height="12">
      <Panel id="FirstChildPanel_1" top="0" left="0" width="80" height="12">
         <TextBox id="FirstName_1" top="0" left="0" width="30" height="12"/>
         <TextBox id="LastName_1" top="0" left="35" width="30" height="12"/>
      </Panel>
      <Panel id="SecondChildPanel_1" top="12" left="80" width="80" height="12">
         <TextBox id="Address_1" top="0" left="0" width="70" height="12"/>
      </Panel>
   </Panel>
   <Panel id="MainPanel_2" top="12" left="0" width="160" height="12">
      <Panel id="FirstChildPanel_2" top="0" left="0" width="80" height="12">
         <TextBox id="FirstName_2" top="0" left="0" width="30" height="12"/>
         <TextBox id="LastName_2" top="0" left="35" width="30" height="12"/>
      </Panel>
      <Panel id="SecondChildPanel_2" top="12" left="80" width="80" height="12">
         <TextBox id="Address_2" top="0" left="0" width="70" height="12"/>
      </Panel>
   </Panel>
   <Panel id="MainPanel_3" top="24" left="0" width="160" height="12">
      <Panel id="FirstChildPanel_3" top="0" left="0" width="80" height="12">
         <TextBox id="FirstName_3" top="0" left="0" width="30" height="12"/>
         <TextBox id="LastName_3" top="0" left="35" width="30" height="12"/>
      </Panel>
      <Panel id="SecondChildPanel_3" top="12" left="80" width="80" height="12">
         <TextBox id="Address_3" top="0" left="0" width="70" height="12"/>
      </Panel>
   </Panel>
</results>

      

0


source







All Articles