XSLT for each

XSLT <xsl:for-each> Element

If we want to do looping in XSL, the <xsl:for-each> element is a must.

The <xsl:for-each> Element:

We can select each XML element of a specified node-set using the XSL <xsl:for-each> element.

Example:

Books.xml:




  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    2020
    300.00
  


XSLT Code:




 

  

List of Books

Title Price

Output:

Filtering the Output:

In the <xsl:for-each> element, we can add a criterion to the select attribute to filter the output from the XML file. An XPath expression is present in the select attribute, that works like navigating a file system, where a forward slash (/) selects subdirectories.

Example:


Legal filter operators:

  • = (equal)
  • != (not equal)
  • &lt; less than
  • &gt; greater than

Example: Adjusted XSL Stylesheet:

Books.xml:




  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    2020
    300.00
  


XSLT Code:




 

  

List of Books

Title Price

Output: