Example XSL

What You Need:

The XML file

...a small part of it actually:

...
<scene>
  <FX>General Road Building noises.</FX>
  <speech speaker="Prosser">
    Come off it Mr Dent, you can't win
    you know. There's no point in lying
    down in the path of progress.
  </speech>
  <speech speaker="Arthur">
    I've gone off the idea of progress.
    It's overrated
  </speech>
...

This XML file doesn't contain any presentation information, which is contained in the style sheet. Separating the document's content and the document's styling information allows displaying the same document on different media (like screen, paper, cell phone), and it also enables users to view the document according to their preferences and abilities, just by modifying the style sheet.

The XSL file

...<xsl:template match="FX">
  <fo:block font-weight="bold">
    <xsl:apply-templates/>
  </fo:block>
</xsl:templates>

<xsl:template match="speech[@speaker="Arthur"]">
  <fo:block background-color="blue">
    <xsl:value-of select="@speaker">:
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>
...

The stylesheet can be used to transform any instance of the DTD it was designed for.

The first rule says that an FX element will be transformed into a block with a bold font. is a recursive call to the template rules for the contents of the current element.

The second template applies to all speech elements that have the speaker attribute set to Arthur, and formats them as blue blocks within which the value speaker attribute is added before the text.

 

Results rendering

© Lynne Grewe