Ash*_*Ash 4 xslt variables xpath for-loop count
我正在使用XPath for循环等效 -
<xsl:for-each select="for $i in 1 to $length return $i">...
Run Code Online (Sandbox Code Playgroud)
我真的需要一个计数变量,我将如何实现这一目标?
谢谢,
灰.
Mic*_*Kay 10
首先要注意的是
for $i in 1 to $length return $i
Run Code Online (Sandbox Code Playgroud)
只是一种冗长的写作方式
1 to $length
Run Code Online (Sandbox Code Playgroud)
在for-each中,您可以将当前整数值作为"."访问.或者作为position().
在里面
<xsl:for-each select="for $i in 1 to $length return $i">...</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
上下文项是整数值,因此您只需要访问.或current().
以下不需要其他名称空间.该解决方案包含一个名为的模板iterate,该模板从内部调用,$length并$i相应地进行更新:
XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<root>
<xsl:call-template name="iterate"/>
</root>
</xsl:template>
<xsl:template name="iterate">
<xsl:param name="length" select="5"/>
<xsl:param name="i" select="1"/>
<pos><xsl:value-of select="$i"/></pos>
<xsl:if test="$length > 1">
<xsl:call-template name="iterate">
<xsl:with-param name="length" select="$length - 1"/>
<xsl:with-param name="i" select="$i + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
产量
<?xml version="1.0" encoding="UTF-8"?>
<root>
<pos>1</pos>
<pos>2</pos>
<pos>3</pos>
<pos>4</pos>
<pos>5</pos>
</root>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26799 次 |
| 最近记录: |