Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: Repeat input n-times, copy input but change some attributes, depending on passed parameters

Tags:

xslt

xslt-1.0

I'm a XSLT beginner and need some help (XSLT 1.0). Basically I need to repeat input given number of times, copying it as-is but changing values of some attributes.

I've seen that the "standard way" of doing this is by using apply-templates and xsl:copy, but the problem is I need to change values of those attributes depending on parameters passed from the "upper level", so I think I need to use call-template, but I just can't figure this 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's children id's should be suffixed with row number, e.g.:
         LastName becomes LastName_1, LastName_2, and so on...
       2) Repeater's 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's 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's 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's 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 exactly figure out how to do the copy part, so that all id's are suffixed with current row number.

Any help much appreciated :)

like image 341
user2066034 Avatar asked Apr 10 '26 21:04

user2066034


1 Answers

I'd suggest you try it this way:

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>
like image 190
michael.hor257k Avatar answered Apr 15 '26 03:04

michael.hor257k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!