XSLT:重复输入n次,复制输入,但根据传递的参数更改某些属性
我是XSLT初学者,需要一些帮助(XSLT1.0)。 基本上,我需要重复输入给定的次数,复制它,但改变一些属性的值 我已经看到,实现这一点的“标准方法”是使用apply-templates和xsl:copy,但问题是我需要根据从“上层”传递的参数更改这些属性的值,所以我认为我需要使用call-template,但我就是想不出这一点:) 输入:XSLT:重复输入n次,复制输入,但根据传递的参数更改某些属性,xslt,xslt-1.0,Xslt,Xslt 1.0,我是XSLT初学者,需要一些帮助(XSLT1.0)。 基本上,我需要重复输入给定的次数,复制它,但改变一些属性的值 我已经看到,实现这一点的“标准方法”是使用apply-templates和xsl:copy,但问题是我需要根据从“上层”传递的参数更改这些属性的值,所以我认为我需要使用call-template,但我就是想不出这一点:) 输入: <Repeater repeatCount="3"> <!-- Only one direct child of Repeater
<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(错误):
因此,我得到了迭代部分,但无法准确地确定如何执行复制部分,因此所有id都以当前行号作为后缀
非常感谢您的帮助:)我建议您这样做: XSLT1.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>
<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>
这里是另一个选项。如果您需要添加进一步的转换,那么它不像Michael的答案那么清晰,但它非常简单 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>
<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>
XSLT1.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>
<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输出
<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>
<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>
我不知道您可以通过应用模板传递参数。要学的东西太多了:)谢谢你提供了如此优雅的解决方案:)肯定有效。我希望我能接受这两个答案。另一个是更一般的解决方案,虽然你的更容易理解。谢谢:)