Skip to Content
XSLT
book

XSLT

by Doug Tidwell
August 2001
Intermediate to advanced
480 pages
11h 16m
English
O'Reilly Media, Inc.
Content preview from XSLT

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:

  1. Output the <xsl:stylesheet> element.

  2. Generate the for-loop template. This will be a named template that we’ll invoke while processing the rest of the document.

  3. 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.

  4. 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

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning XSLT

Learning XSLT

Michael Fitzgerald
Inside XSLT

Inside XSLT

Steven Holzner
XSLT Cookbook

XSLT Cookbook

Sal Mangano
XSLT for Dummies

XSLT for Dummies

Richard Wagner

Publisher Resources

ISBN: 0596000537Catalog PageErrata