当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > <xsl:for-each>

XSL/XSLT
XSL 语言
XSLT 介绍
XSLT 浏览器
XSLT 转换
<xsl:template>
<xsl:value-of>
<xsl:for-each>
<xsl:sort>
<xsl:if>
<xsl:choose>
<xsl:apply-templates>
XSLT - 客户端
XSLT - 服务器端
XSLT - 编辑XML
XSLT 摘要
XSLT 元素参考
XSLT 函数
XSL 编辑器

XSL/XSLT 中的 <xsl:for-each>


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 647 ::
收藏到网摘: n/a

The <xsl:for-each> element allows you to do looping in XSLT.
可以在XSLT中使用<xsl:for-each>元素来实现循环功能。


The <xsl:for-each> Element
<sxl:-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:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>

<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>

</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

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:
上面的转换结果如下:

My CD Collection
我的CD集

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 文件, 以及 最终结果


Filtering the Output
过滤结果

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:
合法的过滤操作是:

  • =  (equal)
    =  (等于)
  • != (not equal)
    != (不等于)
  • &lt; less than
    < 小于
  • &gt; greater than
    > 大于

Take a look at the adjusted XSL style sheet:
让我们来看一下调整后的XSL样式表:

<?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="/">
<html>
<body>
<h2>My CD Collection</h2>

<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>

</tr>
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


The result of the transformation above will look like this:
上面的转换结果如下:

My CD Collection
我的CD集

Title Artist
Empire Burlesque Bob Dylan

View the XML file, View the XSL file, View the result
XML文件, XSL文件, 最终结果

评论 (0) All

登陆 | 还没注册?