XSLT - 利用template实现for循环

来源:互联网 发布:java终止线程 编辑:程序博客网 时间:2024/05/29 06:31

ForLoop.xslt:

[html] view plaincopyprint?在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 plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Chimomo></Chimomo>  


The transformed xml:

[html] view plaincopyprint?在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>  
0 0
原创粉丝点击