XSLT: Extensible Stylesheet Transformation Language

Part 1: Running a transformation using oXygen

What is XSLT?

XSLT is a language that allows us to transform one XML document into another XML document. We can take a document in TEI and turn into XHTML, for example.

XSLT documents contain set of rules for making the transformation. We call these documents stylesheets, so they are often confused with other stylesheets, such as CSS stylesheets. Unlike CSS, however, XSLT is not used to alter just the presentation, but the actual structure.

How does XSLT work?

XSLT stylesheets defines a list of rules that are executed, and those rules use XPath expression to select elements for processing. For example, say we wanted to do this:

<text>
<title>My Story</title>
<para>As the day began…</para>
</text>

 

<html>

<h1>My Story</h1>
<p>As the day began…</p>
</html>

We could use this stylesheet:

<xsl:stylesheet version = '1.0' 
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    
    <xsl:template match="/"> 
        <html>
            <xsl:apply-templates />			
        </html>
    </xsl:template>
    
    <xsl:template match="title">
        <h1><xsl:value-of select="."/></h1>
    </xsl:template>
    
    <xsl:template match="para">
        <p><xsl:value-of select="."/> </p>
    </xsl:template>
    
</xsl:stylesheet>

Don't worry about understanding this example fully; it's mainly here to given an idea what XSLT looks like.

Running an XSLT transformation using Oxygen

  1. First, create an XML file or open an existing one in Oxygen. Use the Lucy Larcon TEI diary entry that we marked up from day 1 (make sure the file validates).
  2. Next, we need to find or download an XSTL stylesheet. You can use the one at this URL: http://dev.colleges.org/~xml2005/xslt/tei-to-html-plain.xsl
  3. With the XML file open, in oXygen, click on the "Configure Transformation Scenario" button.
    It looks like this:
  4. Click the "New" button. This is the panel that will appear:
  5. For "Name" use a title like "Simple-TEI-XHTML". In the box that says "XSL URL" type in the URL or path to the stylesheet: http://dev.colleges.org/~xml2005/xslt/tei-to-html-plain.xsl
  6. Click "OK" to save the transformation.
  7. On the screen which lists the transformations, click "Transform Now"

By default, oXygen shows the XHTML file as text in the lower panel, but you can also ask oXygen to open the file in a browser window. To do this:

  1. With the XML file open, in oXygen, click on the "Configure Transformation Scenario" button
  2. Select the transformation you just created and click "Edit".
  3. Click on the "Output" tab and select "Open in Browser" and "Prompt for File"
  4. Click OK
  5. On the screen which lists the transformations, click "Transform Now". You will be prompted for a place to save the resulting HTML file. Select one and click "OK".
...and the file will open in a browser

Transforming TEI to XHTML

This is a common goal for TEI projects, and since TEI markup is so complex, there is an existing project to develop an "authoritative" set of stylesheets to perform this task. These stylesheets come packaged in oXygen as a default transformation. They appear under the name "TEI HTML" in the list of available transformations.

These stylesheets can accept parameters which affect the output XHTML. The customization guide is available at: http://www.tei-c.org/Stylesheets/teic/customize.xml

As a practice for customization, add the parameter "numberParagraphs" and set it to "true"

(These TEI styesheets are maintained by the TEI consortium and are available online at: http://sourceforge.net/project/showfiles.php?group_id=106328 Look under the selection called "Stylesheets". Information on these stylesheets are available here: http://www.tei-c.org/Stylesheets/teic/)

Resources

There is a public archive of TEI stylesheets in the TEI.org wiki at: http://www.tei-c.org.uk/wiki/index.php/Category:XSLT

Explore

Practice running XSLT transformations using the marked up materials from day 1.
Practice setting XSLT parameters.

Practice running transformations on TEI files using the stylesheets from the TEI wiki above.