如何在 XSLT 中保留变量值并更新它
我有一个名为 ID 的属性,每次为其分配值时该属性都会递增 1。我有一种情况,每次进入循环时该值都会递增。外循环有2条记录,内循环有3条记录。
外循环 1 - 内循环 1、2 和 3,ID 属性递增为 1、2 和 3,但
外循环 2 - 内循环 ....ID 属性再次从 1 开始。我需要将其设置为 4 、 5 等等。
此增量 ID 没有与输入 XML 文件相关的数据。所有处理都必须在 XSLT 中完成。
XSLT 文件:
<xsl:variable name="localISYMPid" select="0"/>
<xsl:element name="Test1">
<xsl:for-each select="Solutions/Solution">
<xsl:if test="Observations/Observation!= '' ">
<xsl:for-each select="Observations/Observation">
<xsl:element name="Roles">
<!-- Generating the ID value -->
<xsl:variable name="ids" select="generate-id(.)"/>
<!-- ***************************-->
<xsl:attribute name="CK"><xsl:value-of select="substring-after($ids,'Solution')"/></xsl:attribute>
<xsl:attribute name="ID"><xsl:value-of select="position() + $localISYMPid "/></xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:element>
请帮我打印 ID 4 的值,因为当内循环针对外循环 1 和外循环 2 循环 3 次时,当内循环为 1 时,ID 值应为 4。到目前为止,它再次打印 1..
ID 1
ID 2
ID 3
----------
ID 1(it should print 4)
我正在使用 XSLT 1.0
谢谢, 拉姆
I have a attribute called ID, which is incremented 1 everytime the value is assigned it to.I have a situation where the value to be incremented everytime the loop is entered. The outer loop has 2 records and inner loop has 3 records.
Outer loop 1 - Inner loop 1, 2, and 3, the ID attribute gets incremented as 1, 2 and 3, but
Outer loop 2 - Inner loop .... again the ID attribute starts with 1. I need this to be 4, 5 and so on..
This increment ID has no data related to the input XML file. All the processing has to be done in the XSLT.
XSLT FIle :
<xsl:variable name="localISYMPid" select="0"/>
<xsl:element name="Test1">
<xsl:for-each select="Solutions/Solution">
<xsl:if test="Observations/Observation!= '' ">
<xsl:for-each select="Observations/Observation">
<xsl:element name="Roles">
<!-- Generating the ID value -->
<xsl:variable name="ids" select="generate-id(.)"/>
<!-- ***************************-->
<xsl:attribute name="CK"><xsl:value-of select="substring-after($ids,'Solution')"/></xsl:attribute>
<xsl:attribute name="ID"><xsl:value-of select="position() + $localISYMPid "/></xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:element>
Please help me in printing the value of ID 4 as when the inner loop is looped 3 times for outer loop 1 and for outer loop 2 and when the inner loop is 1, the ID value shall be 4 .as of now, its printing 1 again..
ID 1
ID 2
ID 3
----------
ID 1(it should print 4)
I am using XSLT 1.0
Thank you,
Ramm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显示源文档总是有用的。但我怀疑你可以通过类似的东西来实现你想要的
It's always useful to show your source document. But I suspect you can achieve what you want with something like
您使用
position()
函数来确定其父元素的本地 ID(在本例中为Observation
)。如果您确实想要一个全局节点,那么您可以执行诸如count(preceding::Roles) + 1
之类的操作,这将计算当前节点之前的所有Roles
节点,然后添加一个。您可能需要根据您的要求进一步限制
preceding::Roles
谓词,例如preceding::Roles[parent::Observation]
,以使确保您只计算Observation
元素子级的Roles
元素。You're using the
position()
function to determine the ID which is local to it's parent element (Observation
in this case). If you really want a global one then you could do something likecount(preceding::Roles) + 1
instead, which will count allRoles
nodes prior to the current one, and then add one.You might need to restrict the
preceding::Roles
predicate a little further depending on your requirements, such aspreceding::Roles[parent::Observation]
for example, to make sure you're only countingRoles
elements that are a child of anObservation
element.