XSLT <xsl:for-each> ตัวแทน

<xsl:for-each> องค์ประกอบสามารถใช้วนรอบใน XSLT

<xsl:for-each> องค์ประกอบ

<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 ที่ทำงานเหมือนการตำหนิไฟล์ระบบแฟ้ม ในที่นี้ตัวเลขเศษกำลังเลือกประกาศย่อย

上面的转换结果类似这样:

查看此 XML 文件查看此 XSL 文件และดูผลลัพธ์

การกรองผลลัพธ์

ผ่านการเพิ่มตัวแทนคำนึงถึงในองค์ประกอบ <xsl:for-each> เราก็สามารถกรองผลลัพธ์ที่ออกมาจาก XML ไฟล์ด้วย

<xsl:for-each select="catalog/cd[artist='Bob Dylan']">

ประเด็นประยุกต์ตัวแทนประมวลผล

  • =  (เท่า)
  • != (ไม่เท่า)
  • < (น้อยกว่า)
  • > (มากกว่า)
<?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>

上面的转换结果类似这样:

查看此 XML 文件查看此 XSL 文件并查看结果