Definition and Usage
¶¨ÒåºÍÓ÷¨
The <xsl:for-each> element loops through each node in a specified node set.
<xsl:for-each>ÔªËØµÄ×÷ÓÃÊÇ£ºÔÚÖ¸¶¨µÄ½Úµã×éÖÐÑ»·²Ù×÷ÿ¸ö½Úµã¡£
Syntax
Óï·¨
<xsl:for-each select="expression"> <!-- Content:(xsl:sort*,template) --> </xsl:for-each> |
Attributes
ÊôÐÔ
| ÊôÐÔ | Öµ | ÃèÊö |
|---|---|---|
| select | expression | Required. The node set to be processed ±ØÒª²ÎÊý¡£Ö¸¶¨ÐèÒª´¦ÀíµÄ½Úµã×é |
Example 1
°¸Àý1
Loop through each "cd "element and use <xsl:value-of> to write each title and artist to the output:
Ñ»·²Ù×÷ÿ¸ö“cd”ÔªËØ£¬²¢ÇÒʹÓÃ<xsl:value-of>ÔªËØÊéдÿ¸ötitle[±êÌâ]ºÍartist[ÒÕÊõ¼Ò]£¬È»ºóÊä³ö£º
<?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> |
²é¿´XML Îļþ¡¢XSL ÎļþÒÔ¼°½á¹û¡£
Example 2
°¸Àý2
Loop through each "cd "element and use <xsl:value-of> to write each title and artist to the output (sorted by artist):
Ñ»·²Ù×÷ÿ¸ö“cd”ÔªËØ£¬²¢ÇÒʹÓÃ<xsl:value-of>ÔªËØÊéдÿ¸ötitle[±êÌâ]ºÍartist[ÒÕÊõ¼Ò]£¬È»ºóÊä³ö£¨°´ÕÕartist ½øÐзÖÀàÅÅÐò£©£º
<?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">
<xsl:sort select="artist"/>
<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> |