[英]current index value in xsl
Hey I have a scenario where I have a xml structure like 嘿,我有一个类似xml结构的场景
<deftms>
<tn>abc</tn>
<td>xyz</td>
<tn>abc1</tn>
<td>xyz1</td>
<tn>abc2</tn>
<td>xyz2</td>
</deftms>
want to convert it as : 想要将其转换为:
<deftms>
<newtms>
<tn>abc</tn>
<td>xyz</td>
</newtms>
<newtms>
<tn>abc1</tn>
<td>xyz1</td>
</newtms>
<newtms>
<tn>abc2</tn>
<td>xyz2</td>
</newtms>
</deftms>
am using following transform xsl code to achieve the output 我正在使用以下transform xsl代码来实现输出
<xsl:template name = "deftms">
<deftms>
<xsl:for-each select="//deftms/tn">
<xsl:variable name="tn"><xsl:value-of select="current()" /></xsl:variable>
<xsl:variable name="td"><xsl:value-of select="(current())" /></xsl:variable>
<newtms>
<tn><xsl:copy-of select="$tn" /></tn>
<td><xsl:copy-of select="$td" /></td>
</newtms>
</xsl:for-each>
</deftms>
</xsl:template>
Anyone any idea ? 有人知道吗?
Within the for-each
the position()
function will give you what you need. 在
for-each
, position()
函数将为您提供所需的内容。 It returns the (one-based) index of the node currently being processed, within the list of nodes that the for-each
selected - informally the "iteration number" (though for-each
need not necessarily be implemented as a loop within the processor). 它在
for-each
选择的节点列表中返回当前正在处理的节点的(基于(一))索引-非正式地称为“ iteration number”(尽管for-each
不一定要实现为处理器内的循环) )。
<xsl:template name = "deftms">
<deftms>
<xsl:for-each select="//deftms/tn">
<xsl:variable name="pos" select="position()" />
<xsl:variable name="td" select="../td[$pos]" />
<newtms>
<tn><xsl:copy-of select="." /></tn>
<td><xsl:copy-of select="$td" /></td>
</newtms>
</xsl:for-each>
</deftms>
</xsl:template>
Try: 尝试:
<xsl:template name = "deftms">
<deftms>
<xsl:for-each select="tn">
<newtms>
<xsl:copy-of select=". | following-sibing::td[1]" />
</newtms>
</xsl:for-each>
</deftms>
</xsl:template>
Try below one 尝试以下一项
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="deftms" name ="ParentCopy">
<xsl:copy>
<xsl:param name="LoopCountA">1</xsl:param>
<xsl:variable name = "CountDate" >
<xsl:value-of select = "count(tn)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test = " $LoopCountA <= $CountDate " >
<newtms>
<xsl:copy-of select="tn[position()= $LoopCountA]"/>
<xsl:copy-of select ="td[position()= $LoopCountA]"/>
</newtms>
<xsl:call-template name="ParentCopy">
<xsl:with-param name="LoopCountA">
<xsl:value-of select="$LoopCountA + 1"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.