使用 xsl:variable 的 Xslt 循环 [英] Xslt Looping using xsl:variable
本文介绍了使用 xsl:variable 的 Xslt 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据计数使用 xsl:variable 和循环,但我不确定在 Xslt 中是否可行.例如,如果我有一个变量名 count
I wanted to use xsl:variable and loop based on it's count, but I am not sure if its possible in Xslt. for example if I have a variable name count
<xsl:variable name="count" as="xs:integer" select="4"/>
我可以用下面的形式使用变量吗!!!
Can I make use of variable, in below form!!!
<xsl:if test="some condition"/>
loop from 0 to $count
...do something here
end loop
</xsl:if>
有可能吗?
我的输入 XML:
<Root>
<Element>
<Value>1</Value>
<Value>2</Value>
</Element>
<Element>
<Value>1</Value>
<Value>2</Value>
<Value>3</Value>
<Value>4</Value>
</Element>
<Element>
<Value>1</Value>
</Element>
</Root>
平面文件中的预期输出是(带换行符):
Expected output in flat file is (with line-breaks):
1,2,,
1,2,3,4
1,,,
任何帮助表示赞赏.谢谢姆.
Any help appreciated. Thanks Mh.
推荐答案
是的,这在 XSLT 2.0 中是可能的(从 as="xs:integer"
在你的例子中,我假设你正在使用).以下转换将从您的示例输入生成您预期的输出:
Yes, this is possible in XSLT 2.0 (which, from the as="xs:integer"
in your example, I assume you're using). The following transform will produce your expected output from your example input:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="count" select="4"/>
<xsl:template match="text()" />
<xsl:template match="Element">
<xsl:variable name="curElement" select="."/>
<xsl:for-each select="1 to $count">
<xsl:variable name="curVal" select="."/>
<xsl:value-of select="$curElement/Value[. = $curVal]"/>
<xsl:if test="$curVal != $count">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
这篇关于使用 xsl:variable 的 Xslt 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文