XSLT <for-each> Element

The XSLT <xsl:for-each> element allows you to loop through multiple nodes that match the selection criteria. This in turn, enables you to retrieve data from these nodes.

For example, imagine if our XML file had two elements called name — each under the tutorial element. Like this:

To extract data from both name elements, we can use <xsl:for-each> in conjunction with <xsl:value-of>.

<xsl:for-each> Example

Here, we use <xsl:for-each> to loop through each name element, and <xsl:value-of> to extract data from each node.

Note the value of the select attribute (.). This expression specifies the current node. The <xsl:element name="br"/> element/attribute is there simply for readibility purposes — it provides a line break after each iteration.