XML教程
XQuery教程
XSLT教程
XPath教程

XSLT <xsl: for-each>

XSLT <xsl: for-each>元素

XSLT <xsl: for-each>元素用于为每个节点重复应用模板。
<xsl:for-each
   select = Expression>  
</xsl:for-each>

参数说明

选择: 要在当前上下文中评估的XPath表达式,以确定要迭代的节点集。

XSLT <xsl: for-each> 元素示例

让我们以创建一个<employee>元素表为例,该表的属性为" id",其子元素为<firstname>,<lastname> <nickname>和<salary>遍历每个员工。
Employee.xml
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> 
<class> 
   <employee id = "001">
      <firstname>Aryan</firstname> 
      <lastname>Gupta</lastname> 
      <nickname>Raju</nickname> 
      <salary>30000</salary>
   </employee> 
   <employee id = "024"> 
      <firstname>Sara</firstname> 
      <lastname>Khan</lastname> 
      <nickname>Zoya</nickname> 
      <salary>25000</salary>
   </employee> 
   <employee id = "056"> 
      <firstname>Peter</firstname> 
      <lastname>Symon</lastname> 
      <nickname>John</nickname> 
      <salary>10000</salary> 
   </employee> 
</class>
Employee.xsl
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" 
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">   
   <xsl:template match = "/"> 
      <html> 
         <body> 
            <h2>Employee</h2> 
            <table border = "1">
               <tr bgcolor = "pink"> 
                  <th>ID</th> 
                  <th>First Name</th> 
                  <th>Last Name</th> 
                  <th>Nick Name</th> 
                  <th>Salary</th> 
               </tr> 
               <!--for-each processing instruction 
               Looks for each element matching the XPath expression 
              --> 
               <xsl:for-each select="class/employee"> 
                  <tr> 
                     <td> 
                        <!--value-of processing instruction 
                        process the value of the element matching the XPath expression 
                       --> 
                        <xsl:value-of select = "@id"/> 
                     </td> 
                     <td><xsl:value-of select = "firstname"/></td> 
                     <td><xsl:value-of select = "lastname"/></td> 
                     <td><xsl:value-of select = "nickname"/></td> 
                     <td><xsl:value-of select = "salary"/></td>   
                  </tr> 
               </xsl:for-each> 
            </table> 
         </body> 
      </html>
   </xsl:template> 
</xsl:stylesheet>
输出:
每个元素1的XSLT Xsl
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4