Direct processing

It is possible, under certain circumstances, effectively to embed templates within other templates. The For Each element is used for this purpose. It selects other elements and applies an embedded template to each occurrence. The number of templates needed can in this manner be radically reduced. Consider the following example, where information on a number of customers is present.

<customers>

  <customer>
    <name>J Smith</name>
    <order>123</order>
    <order>456</order>
  </customer>

  <customer>
    <name>P Jones</name>
    <order>987</order>
    <order>654</order>
  </customer>

</customers>

To create an HTML table from this structure, with one row for each customer, four templates would normally be required:

 <xsl:template match="customers"> <TABLE><TBODY><xsl:apply-templates/></TBODY></TABLE> ...

Get XSL companion, The now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.