XSLT Source Code

Now that we’ve discussed the design issues we went through as we defined our XML document structure, we’ll talk about how our XSLT stylesheets transform XML-tagged tutorials into the files we want.

Stylesheets and Modes

To start with, we use the XSLT mode attribute to process the same set of nodes several times. Our template for the root element is similar to a Java or C++ program whose main() method contains nothing but subroutine calls:

<xsl:template match="/">
  <xsl:apply-templates select="tutorial" mode="build-main-index"/>
  <xsl:apply-templates select="tutorial" mode="build-section-indexes"/>
  <xsl:apply-templates select="tutorial" mode="build-individual-panels"/>
  <xsl:apply-templates select="tutorial" mode="generate-graphics"/>
  <xsl:apply-templates select="tutorial" mode="generate-pdf-file">
    <xsl:with-param name="page-size" select="'letter'"/>
  </xsl:apply-templates>
  <xsl:apply-templates select="tutorial" mode="generate-pdf-file">
    <xsl:with-param name="page-size" select="'a4'"/>
  </xsl:apply-templates>
  <xsl:apply-templates select="tutorial" mode="generate-zip-file"/>
</xsl:template>

If this were a Java program, we might create a main() method that looks like this:

public static void main(String[] argv)
{
  buildMainIndex();
  buildSectionIndexes();
  buildIndividualPanels();
  generateGraphics();
  generatePDFFile("letter");
  generatePDFFile("a4");
  generateZipFile();
}

This style of coding facilitates maintenance; if the PDF files aren’t generated correctly, the templates ...

Get XSLT 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.