xsl:for-each

Selects a set of nodes in the source document and instantiates the contained template once for each node in the set.

Format

<xsl:for-each select="pattern">
               
  [<xsl:sort[select="expression"][optional_attribute]/>]
               
  template_body 
               
</xsl:for-each>
               

            

Description

The select attribute is required and the pattern must evaluate to a node set. The XSLT processor instantiates the embedded template with the selected node as the current node and with a list of all selected nodes as the current node list.

By default, the new list of source nodes is processed in document order. However, you can use the xsl:sort instruction to specify that the selected nodes are to be processed in a different order. See xsl:sort.

The xsl:for-each instruction is useful when the result document has a regular, known structure. When you know that you want to instantiate the same template for each node in the current node list, the xsl:for-each instruction eliminates the need to find a template that matches each node.

Tip

 

You can create an xsl:for-each element automatically using the XSLT mapper.

Example

For example, suppose your source document includes the following XML:

<books>
               
  <author>
               
    <name>Sara Peretsky</name>
               
    <booktitle>Bitter Medicine</booktitle>
               
    <booktitle>Killing Orders</booktitle>
               
  </author>
               
  <author>
               
    <name>Dick Francis</name>
               
    <booktitle>Reflex</booktitle>
               
    <booktitle>Proof</booktitle>
               
    <booktitle>Nerve</booktitle>
               
  </author>
               
</books>
               

            

The following stylesheet creates an HTML document that contains a list of authors. Each author is followed by the titles of the books the author wrote. It does not matter how many authors there are nor how many titles are associated with each author. The stylesheet uses the xsl:for-each instruction to process each author and to process each title associated with each author.

<xsl:stylesheet 
               
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
               
<xsl:template match = "/">
               
  <html>
               
    <head><title>Authors and Their Books</title></head>
               
    <body> 
               
      <xsl:for-each select = "books/author">
               
    <p>
               
      <xsl:value-of select = "name"/>
               
    <br>
               
      <xsl:for-each select = "booktitle">
               
      <xsl:value-of select = "."/>
               
    <br>
               
      </xsl:for-each>
               
    </p>
               
      </xsl:for-each>
               
    </body>
               
  </html>
               
</xsl:template>
               
</xsl:stylesheet>
               

            

The result document looks like this:

<html>
               
<head>
               
<title>Authors and Their Books</title>
               
</head>
               
<body>
               
<p>
               
Sara Peretsky<br>
               
Bitter Medicine<br>
               
Killing Orders<br>
               
</p>
               
<p>
               
Dick Francis<br>
               
Reflex<br>
               
Proof<br>
               
Nerve<br>
               
</p>
               
</body>
               
</html>
               

            

XML-DEV Discussion Forum

XML-DEV is a leading technical forum for XML discussion and a valuable free XML learning resource. Subscribe to the XML-DEV email discussion forum today to help further your XML education.

Java Code Generation

The Stylus Studio Java Codce Generator lets you generate deployable Java code for XQuery and XSLT at the push of a button!

Web Service Data-to-XML Mapper

Stylus Studio provides a powerful standards-based solution for accessing and transforming live Web service XML data. Intuitive tools like the Web Service Call Composer and UDDI Browser make developing Web service applications a snap.

XML Publisher - XSLT Generator and XSL:FO Generator

The XML Publisher lets you easily design beautiful documents in HTML or PDF format from relational, XML and other data sources using XSL:FO or XSLT, using an intuitive visual interface.

Stylus Most Wanted

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.