gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:36:14 25 4
gpt4 key购买 nike

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

我已经看到执行此操作的“标准方法”是使用应用模板和 xsl:copy,但问题是我需要根据从“上层”传递的参数更改这些属性的值,所以我想我需要使用调用模板,但我就是想不通:)

输入:

<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 都以当前行号作为后缀。

非常感谢任何帮助:)

最佳答案

我建议你这样试试:

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>

关于XSLT:重复输入 n 次,复制输入但更改一些属性,具体取决于传递的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31147573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com