Node:Step 2, Next:, Previous:Step 1, Up:Tutorial



Step 2: Performing a transformation

Transbuild gets more interesting when source files are processed to build target files. In this step, we will transform the XML files using XSLT. Modify the first rule from the previous step by change it into:

<rule source-suffix=".xml" target-suffix=".html">
  <xslt stylesheet="script/tr02.xsl"/>
</rule>

When a source file is processed, it finds the first rule in the build script where the source-suffix attribute matches the end of the filename. This is a exact literal character-by-character match, and characters such as ., *, and ? do not have any special meaning.

The name of the generated target file is created by taking the original source file name, removing the source-suffix from the end and appending the target-suffix. If the target-suffix is exactly the same as the source-suffix, the filename will be unchanged (in this case, the target-suffix attribute can be left out - as in the rule that matches the CSS file). The target file is always created in the same directory in the target tree as the source file is in the source tree.

All the files in the source tree must match a rule, otherwise an error will be generated. This way, no unexpected files will end up in the target tree.

To create the target file, the contents of a source file is processed by the list of processing steps in the rule. The output from each processing step becomes input to the next processing step - like a pipeline. The input into the first processing step is the contents of the source file. The output from the last step is saved into the target file. The file-copy processing step can be thought of as a straight-through processing step that does not change the data.

The xslt processing step is used in this example. As you may guess, it applies a XSLT transformation to the data (which must be in XML format). The stylesheet attribute specifies the XSLT file relative to the location of the build script. In this step, the tr02.xsl XSLT script converts the XML file into a simple XHTML Web page.

<?xml version="1.0"?>

<!-- A very simple transformation of the XML into XHTML -->

<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/1999/xhtml"
	>

<xsl:output method="xml"
	    encoding="UTF-8"
	    standalone="yes"
	    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
	    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
	    media-type="text/xhtml"
	    indent="yes"
	    />

<xsl:template match="/">
  <html xml:lang="en" lang="en">
    <xsl:comment>Do not edit: generated by Transbuild</xsl:comment>
    <head>
      <meta http-equiv="Content-Type"
            content="text/xhtml; charset=UTF-8"/>
      <title>Step 2: <xsl:value-of select="article/title"/></title>
    </head>
    <xsl:apply-templates/>
  </html>
</xsl:template>

<xsl:template match="article">
  <body>
    <h1><xsl:value-of select="title"/></h1>
    <div class="main">
      <xsl:apply-templates/>
    </div>
  </body>
</xsl:template>

<xsl:template match="title">
  <!-- ignore, since already used in h1 -->
</xsl:template>

<xsl:template match="para">
  <p><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="ulink">
  <a href="{@url}"><xsl:apply-templates/></a>
</xsl:template>

</xsl:stylesheet>

The meta tag was added because some browsers (e.g. Opera) do not pick up the character encoding from the XML declaration.

Running Transbuild on the second build script, tb-step02.xml, produces a target tree containing a collection of XHTML files (with .html file extensions) and a CSS file that is a direct copy of the one in the source tree.

$ transbuild -a -f tb-step02.xml

Transbuild does not delete any files from the target tree. So if you run this after running the first step, in addition to the new .html files you will find the target tree cluttered up with the old .xml files. It is recommended that you delete the entire target tree to start afresh (rm -r target).

These XHTML Web pages are good, but they are still individual pages. A Web site must be connected together with hyperlinks. In the next step, links between the pages will be added.