for each元素

基本介紹

  • 中文名:for each元素
  • 又稱:xsl:for-each
  • 允許:在 XSLT 中進行循環
  • 選取:指定的節點集中的每個 XML 元素
正文,簡介,元素,轉換結果,

正文

簡介

<xsl:for-each> 元素允許您在 XSLT 中進行循環。

元素

<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> 注釋:select 屬性的值是一個 XPath 表達式。此表達式的工作方式類似於定位某個檔案系統,在其中正斜槓可選擇子目錄。

轉換結果

結果過濾
通過在 <xsl:for-each> 元素中添加一個選擇屬性的判別式,我們也可以過濾從 XML 檔案輸出的結果。
<xsl:for-each select="catalog/cd[artist='Bob Dylan']"> 合法的過濾運算符:
= (等於)
!= (不等於)
&lt; (小於)
&gt; (大於)
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www. <CITE class=highlight style="BACKGROUND: none transparent scroll repeat 0% 0%" highlight="true">w3. org</CITE>/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>

相關詞條

熱門詞條

聯絡我們