Repeating structures

When creating tabular output from source elements, or some other very regular structure, a technique is available that reduces the number of templates needed, and in so doing improves the clarity of the stylesheet. For example, consider the need to transform the following structure into a tabular format:

<countries>
  <country>
    <name>United Kingdom</name>
    <capital>london</capital>
  </country>
  <country>
    <name>United States</name>
    <capital>Washington</capital>
    <borders>Canada</borders>
    <borders>Mexico</borders>
  </country>
</countries>

To create an HTML table for this data, with one row for each country, the following four templates could be defined:

 <template match="countries"> <html:TABLE><apply-templates/></html:TABLE> </template> ...

Get XML Companion, The, Third Edition 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.