XSLT:重复输入n次,复制输入但改变一些属性,具体取决于传递的参数

时间:2015-06-30 20:09:13

标签: xslt xslt-1.0

我是XSLT初学者,需要一些帮助(XSLT 1.0)。 基本上我需要重复输入给定的次数,按原样复制它,但改变某些属性的值。

我已经看到这样做的“标准方式”是使用apply-templates和xsl:copy,但问题是我需要根据从“上层”传递的参数来更改这些属性的值,所以我认为我需要使用call-template,但我无法弄明白:)

输入:

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

期望的输出:

<!-- 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(错误):

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

所以我得到了迭代部分,但无法准确地弄清楚如何复制部分,所以所有id都以当前行号为后缀。

任何帮助非常感谢:)

2 个答案:

答案 0 :(得分:2)

我建议你这样试试:

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 :(得分:0)

这是另一种选择。如果你需要添加进一步的转换,它不像迈克尔的答案那么干净,但它很简单。

XML输入

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

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输出

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