عنصر <xsl:for-each> في XSLT

<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 هذا,ونرى النتيجة.