XSLT - 利用template实现for循环

ForLoop.xslt:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">  
  3.   <xsl:output method="xml" indent="yes"/>  
  4.   <xsl:template name="for-loop">  
  5.     <xsl:param name="i"/>  
  6.     <xsl:param name="count"/>  
  7.     <xsl:if test="$i < $count">  
  8.       <!--Put what you want to do in for loop here.-->  
  9.       <xsl:element name="Loop">  
  10.         <xsl:value-of select="$i"/>  
  11.       </xsl:element>  
  12.     </xsl:if>  
  13.     <xsl:if test="$i < $count">  
  14.       <xsl:call-template name="for-loop">  
  15.         <xsl:with-param name="i">  
  16.           <xsl:value-of select="$i + 1"/>  
  17.         </xsl:with-param>  
  18.         <xsl:with-param name="count">  
  19.           <xsl:value-of select="$count"/>  
  20.         </xsl:with-param>  
  21.       </xsl:call-template>  
  22.     </xsl:if>  
  23.   </xsl:template>  
  24.   <xsl:template match="/">  
  25.     <xsl:element name="Chimomo">  
  26.       <xsl:call-template name="for-loop">  
  27.         <xsl:with-param name="i">1</xsl:with-param>  
  28.         <xsl:with-param name="count">10</xsl:with-param>  
  29.       </xsl:call-template>  
  30.     </xsl:element>  
  31.   </xsl:template>  
  32. </xsl:stylesheet>  


ForLoop.xml:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Chimomo></Chimomo>  


The transformed xml:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Chimomo>  
  3.   <Loop>1</Loop>  
  4.   <Loop>2</Loop>  
  5.   <Loop>3</Loop>  
  6.   <Loop>4</Loop>  
  7.   <Loop>5</Loop>  
  8.   <Loop>6</Loop>  
  9.   <Loop>7</Loop>  
  10.   <Loop>8</Loop>  
  11.   <Loop>9</Loop>  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值