xslt 循环怎么在循环里套循环

如下代码定义了一个循环模板的例子:
&&xsl:template name=&for-loop&&
&&xsl:param name=&i&/&
&&xsl:param name=&count&/&
&&xsl:if test=&$i &= $count&&
&& &&xsl:value-of select=&$i& disable-output-escaping = &yes& /&
&&/xsl:if&
&&xsl:if test=&$i &= $count&&
& &xsl:call-template name=&for-loop&&
&& &xsl:with-param name=&i&&
&& &&xsl:value-of select=&$i + 1&/&
&& &/xsl:with-param&
&& &xsl:with-param name=&count&&
&& &&xsl:value-of select=&$count&/&
&& &/xsl:with-param&
& &/xsl:call-template&
&&/xsl:if&
&/xsl:template&
调用方法:
&xsl:call-template name=&for-loop&&
&&xsl:with-param name=&i&&1&/xsl:with-param&
&&xsl:with-param name=&count&&10&/xsl:with-param&
&/xsl:call-template&
结果输出1-10这10个数字。
阅读(...) 评论()XSLT &xsl:for-each& 元素
XSLT &xsl:for-each& 元素
&xsl:for-each& 元素允许您在 XSLT 中进行循环。
&xsl:for-each& 元素
&xsl:for-each& 元素可用于选取指定的节点集中的每个 XML 元素。
&?xml version=&1.0& encoding=&ISO-8859-1&?&
&xsl:stylesheet version=&1.0&
xmlns:xsl=&http://www.w3.org/1999/XSL/Transform&&
&xsl:template match=&/&&
&h2&My CD Collection&/h2&
&table border=&1&&
&tr bgcolor=&#9acd32&&
&th&Title&/th&
&th&Artist&/th&
&xsl:for-each select=&catalog/cd&&
&td&&xsl:value-of select=&title&/&&/td&
&td&&xsl:value-of select=&artist&/&&/td&
&/xsl:for-each&
&/xsl:template&
&/xsl:stylesheet&
注释:select 属性的值是一个 XPath 表达式。此表达式的工作方式类似于定位某个文件系统,在其中正斜杠可选择子目录。
上面的转换结果类似这样:
通过在 &xsl:for-each& 元素中添加一个选择属性的判别式,我们也可以过滤从 XML 文件输出的结果。
&xsl:for-each select=&catalog/cd[artist='Bob Dylan']&&
合法的过滤运算符:
!= (不等于)
&?xml version=&1.0& encoding=&ISO-8859-1&?&
&xsl:stylesheet version=&1.0&
xmlns:xsl=&http://www.w3.org/1999/XSL/Transform&&
&xsl:template match=&/&&
&h2&My CD Collection&/h2&
&table border=&1&&
&tr bgcolor=&#9acd32&&
&th&Title&/th&
&th&Artist&/th&
&xsl:for-each select=&catalog/cd[artist='Bob Dylan']&&
&td&&xsl:value-of select=&title&/&&/td&
&td&&xsl:value-of select=&artist&/&&/td&
&/xsl:for-each&
&/xsl:template&
&/xsl:stylesheet&
上面的转换结果类似这样:2009年11月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第二2009年6月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第二
2009年12月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第三2009年10月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第三2009年8月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第三
2013年8月 Web 开发大版内专家分月排行榜第一2010年2月 Web 开发大版内专家分月排行榜第一
2013年7月 Web 开发大版内专家分月排行榜第二2013年6月 Web 开发大版内专家分月排行榜第二
2013年8月 Web 开发大版内专家分月排行榜第一2010年2月 Web 开发大版内专家分月排行榜第一
2013年7月 Web 开发大版内专家分月排行榜第二2013年6月 Web 开发大版内专家分月排行榜第二
本帖子已过去太久远了,不再提供回复功能。35370人阅读
ForLoop.xslt:
&?xml version=&1.0& encoding=&utf-8&?&
&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&&
&xsl:output method=&xml& indent=&yes&/&
&xsl:template name=&for-loop&&
&xsl:param name=&i&/&
&xsl:param name=&count&/&
&xsl:if test=&$i & $count&&
&!--Put what you want to do in for loop here.--&
&xsl:element name=&Loop&&
&xsl:value-of select=&$i&/&
&/xsl:element&
&xsl:if test=&$i & $count&&
&xsl:call-template name=&for-loop&&
&xsl:with-param name=&i&&
&xsl:value-of select=&$i + 1&/&
&/xsl:with-param&
&xsl:with-param name=&count&&
&xsl:value-of select=&$count&/&
&/xsl:with-param&
&/xsl:call-template&
&/xsl:template&
&xsl:template match=&/&&
&xsl:element name=&Chimomo&&
&xsl:call-template name=&for-loop&&
&xsl:with-param name=&i&&1&/xsl:with-param&
&xsl:with-param name=&count&&10&/xsl:with-param&
&/xsl:call-template&
&/xsl:element&
&/xsl:template&
&/xsl:stylesheet&
ForLoop.xml:
&?xml version=&1.0& encoding=&UTF-8&?&
&Chimomo&&/Chimomo&
The transformed xml:
&?xml version=&1.0& encoding=&utf-8&?&
&Loop&1&/Loop&
&Loop&2&/Loop&
&Loop&3&/Loop&
&Loop&4&/Loop&
&Loop&5&/Loop&
&Loop&6&/Loop&
&Loop&7&/Loop&
&Loop&8&/Loop&
&Loop&9&/Loop&
&/Chimomo&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:3323373次
积分:112788
积分:112788
排名:第5名
原创:270篇
评论:65条
(3)(1)(1)(2)(5)(2)(9)(2)(5)(5)(4)(3)(19)(6)(1)(1)(7)(4)(40)(20)(2)(3)(4)(6)(3)(5)(5)(3)(2)(2)(2)(1)(3)(2)(1)(3)(2)(34)(4)(1)(2)(1)(1)(1)(20)(1)(2)(1)(2)(6)(4)(1)}

我要回帖

更多关于 491234蓝月亮xslt 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信