怎样在xslt中写一个循环n次的语句 就是类试于 for(int i=0;i<3;i++) { }

方法一:

 1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 2<xsl:template match="/">
 3<xsl:call-template name="repeat">
 4<xsl:with-param name="times" select="1000"></xsl:with-param>
 5</xsl:call-template>
 6</xsl:template>
 7<xsl:template name="repeat">
 8<xsl:param name="times" select="0"></xsl:param>
 9<xsl:if test="$times &gt; 0">
10<!--do some action here -->
11<xsl:value-of select="123"></xsl:value-of>
12<xsl:call-template name="repeat">
13<xsl:with-param name="times" select="$times - 1"></xsl:with-param>
14</xsl:call-template>
15</xsl:if>
16</xsl:template>
17</xsl:stylesheet>

方法二:
test.xml

1<test></test>

test.xsl

 1<xsl:stylesheet version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:schemas-chinaedustar-com:user" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 2<xsl:output omit-xml-declaration="yes"></xsl:output>
 3<xsl:param name="i">3</xsl:param>
 4<xsl:template match="/">
 5<xsl:value-of disable-output-escaping="yes" select="user:getContent(string($i))"></xsl:value-of>
 6</xsl:template>
 7<msxsl:script implements-prefix="user" language="JavaScript">
 8<![CDATA[   
 9function getContent(str)   
10{   
11var i=Number(str)   
12var content=""   
13for(j=0;j<i;j++)   
14{   
15content+="<div>这是循环:第"+j+"次</div>"   
16}   
17return content   
18}   
19]]>
20</msxsl:script>
21</xsl:stylesheet>
Published At
Categories with Web编程
comments powered by Disqus