当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > <xsl:for-each>
The <xsl:for-each> element allows you to do looping in XSLT.
可以在XSLT中使用<xsl:for-each>元素来实现循环功能。
The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set:
XSL<xsl:for-each>元素的作用是:选择所有具有指定节点集的XML元素:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:template match="/"> |
Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.
注意: 选择(select)属性值是一个XPath的表达式值。XPath表达式的作用,如:文件系统的导航,而这个文件系统是通过在前端添加斜杠 “/” 来选择子目录的。
The result of the transformation above will look like this:
上面的转换结果如下:
Title | Artist |
---|---|
Empire Burlesque | Bob Dylan |
Hide your heart | Bonnie Tyler |
Greatest Hits | Dolly Parton |
Still got the blues | Gary More |
Eros | Eros Ramazzotti |
One night only | Bee Gees |
Sylvias Mother | Dr.Hook |
Maggie May | Rod Stewart |
Romanza | Andrea Bocelli |
When a man loves a woman | Percy Sledge |
Black angel | Savage Rose |
1999 Grammy Nominees | Many |
For the good times | Kenny Rogers |
Big Willie style | Will Smith |
Tupelo Honey | Van Morrison |
Soulsville | Jorn Hoel |
The very best of | Cat Stevens |
Stop | Sam Brown |
Bridge of Spies | T`Pau |
Private Dancer | Tina Turner |
Midt om natten | Kim Larsen |
Pavarotti Gala Concert | Luciano Pavarotti |
The dock of the bay | Otis Redding |
Picture book | Simply Red |
Red | The Communards |
Unchain my heart | Joe Cocker |
View the XML file, View the XSL file, and View the result
XML 文件, XSL 文件, 以及 最终结果
We can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:for-each> element.
我们也可以在<xsl:for-each>元素中添加一个选择属性来过滤XML文件中的结果。
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
Legal filter operators are:
合法的过滤操作是:
Take a look at the adjusted XSL style sheet:
让我们来看一下调整后的XSL样式表:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:template match="/"> |
The result of the transformation above will look like this:
上面的转换结果如下:
Title | Artist |
---|---|
Empire Burlesque | Bob Dylan |
View the XML file, View the XSL file, View the result
XML文件, XSL文件, 最终结果