A Stylesheet That Generates a Stylesheet That Emulates a for Loop
We’ve emulated a for loop now, but what about a stylesheet that generates another stylesheet that emulates the for loop? As we beat this dead horse one more time, we’ll create a stylesheet that generates the iteration for us, along with an XML syntax that automates the process.
XML Input
Here’s the XML template we’ll use to generate the stylesheet:
<?xml version="1.0"?>
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<head>
<title>Text generated by our for loop processor</title>
</head>
<body>
<h1>Text generated by our for loop processor</h1>
<table border="1">
<tr>
<th>Iteration #</th>
<th>Value of <i>i</i></th>
</tr>
<for-loop index-variable="0" increment="1"
operator="<=" test-value="10">
<tr>
<td align="center">
<xsl:value-of select="$iteration"/>
</td>
<td align="center">
<xsl:value-of select="$i"/>
</td>
</tr>
</for-loop>
</table>
</body>
</html>Template Design
The design of our stylesheet-generating stylesheet is as follows:
Output the
<xsl:stylesheet>element.Generate the for-loop template. This will be a named template that we’ll invoke while processing the rest of the document.
Generate the root element template. To do this, everything except the
<for-loop>element is copied to the output document. The<for-loop>element will be converted into a call to the for-loop template we generated in the previous step.Close out the
<xsl:stylesheet>element.
Complications
There are a couple of complications ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access