XML technologies / XSLT / The transformation artist
(Montero Pineda, Manuel: Der Verwandlungskünstler, in: technische kommunikation, issue 2, 2018).
Converting XML files to formats such as PDF or HTML requires rules. XSLT can define these rules.
The language XSLT transforms XML documents. Its name stands for eXtensible Stylesheet Language Transformation, and it is a subset of XSL, which also includes XPath and XSL-FO. W3C published the language in 1999 and has continued developing it. The current version is 3.0, although 2.0 is most widely used.
What is XSLT for?
XSLT builds on XML documents’ logical tree structure to define transformation rules. It can reorganise XML documents and automate data migration. Another major application is automated publishing of XML data, producing HTML, PDF, EPUB or HTML Help, for example.
In simplified terms, XSLT and XPath transform XML documents into XHTML. The program performing the processing is an XSLT processor. Several processors are available on the market, all free. Among the best known are MSXML, Xalan and Saxon.
The following figure illustrates XSLT processing with an XML file and a stylesheet that processes poems into HTML.
Figure: Example transformation from XML to HTML.
In the editor, the XML file contains several poems within element <Gedicht>, including one by Rainer Maria Rilke, followed by poems by Goethe, Brecht and Wilhelm Busch. Each author (<Autor>) has a first name (<Vorname>) and surname (<Nachname>). There is also a title (<Titel>), an optional subtitle (<Untertitel>) and stanzas (<Strophe>) with several verses (<Vers>), as the following code shows.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Gedichtsammlung SYSTEM "Gedicht.dtd">
<Gedichtsammlung>
<Gedicht Erscheinungsjahr="1905" Sprache="de">
<Autor>
<Vorname>Rainer Maria</Vorname>
<Nachname>Rilke</Nachname>
</Autor>
<Titel>Der Panther</Titel>
<Untertitel>Im Jardin des Plantes, Paris</Untertitel>
<Strophe>
<Vers>Sein Blick ist vom Vorübergehn der Stäbe</Vers>
<Vers>so müd geworden, daß er nichts mehr hält.</Vers>
<Vers>Ihm ist, als ob es tausend Stäbe gäbe</Vers>
<Vers> und hinter tausend Stäben keine Welt.</Vers>
</Strophe>
<Strophe>
<Vers>Der weiche Gang geschmeidig starker Schritte,</Vers>
<Vers> der sich im allerkleinsten Kreise dreht,</Vers>
<Vers> ist wie ein Tanz von Kraft um eine Mitte,</Vers>
<Vers>in der betäubt ein großer Wille steht.</Vers>
</Strophe>
<Strophe>
<Vers> Nur manchmal schiebt der Vorhang der <Fett>Pupille</Fett></Vers>
<Vers>sich lautlos auf -. Dann geht ein Bild hinein,</Vers>
<Vers> geht durch der <Fett>Glieder</Fett> angespannte Stille -</Vers>
<Vers>und hört im <Fett>Herzen</Fett> auf zu sein.</Vers>
</Strophe>
</Gedicht>
<Gedicht Erscheinungsjahr="1799" Sprache="de">
<Autor>
<Vorname>Johann Wolfgang</Vorname>
<Nachname> von Goethe</Nachname>
</Autor>
<Titel>Der Zauberlehrling</Titel>
<Strophe>
<Vers>Hat der alte Hexenmeister</Vers>
<Vers>Sich doch einmal wegbegeben!</Vers>
<Vers>Und nun sollen seine Geister</Vers>
<Vers>Auch nach meinem Willen leben.</Vers>
<Vers>Seine Wort und Werke</Vers>
<Vers>Merkt ich und den Brauch,</Vers>
<Vers>Und mit Geistesstärke</Vers>
<Vers>Tu ich Wunder auch.</Vers>
</Strophe>
...
</Gedicht>
</Gedichtsammlung>Code example: The poem collection’s XML file and its elements.
We will find these element names again in the XSLT stylesheet. First, however, let us examine how XSLT works.
How does XSLT work?
XSLT can find and replace content. For example, we can replace element <Autor> with a <br/> tag and an <h4>, as shown below.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="iso-8859-1" version="1.0"/>
<xsl:template match="Gedichtsammlung">
<html>
<body><h1>Gedichtsammlung</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Autor">
<br/>
<h4>
<xsl:apply-templates/>
</h4>
</xsl:template>
<xsl:template match="Vorname">
<xsl:apply-templates/>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="Nachname">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Titel">
<h2>
<xsl:apply-templates/>
</h2>
</xsl:template>
<xsl:template match="Strophe">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="Vers">
<xsl:apply-templates/>
<br/>
</xsl:template>
<xsl:template match="Gedicht">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Fett">
<b>
<xsl:apply-templates/>
</b>
</xsl:template>
<xsl:template match="Untertitel">
<h4>
<xsl:apply-templates/>
</h4>
</xsl:template>
</xsl:stylesheet>Code example: Replacing element <Autor> with an XSLT rule.
Next, a <Titel> element becomes <h2>, while <Strophe> becomes a <p> element, each <Vers> becomes a <br/>, <Fett> becomes a <b>, and <Untertitel> becomes an <h4>.
How does processing actually work? The processor traverses the file, looking for a rule for each element, such as <Titel>. If a matching template exists, it executes it. Here, the stylesheet’s rule for <Titel> says to create an <h2>, as shown below.
<xsl:template match="Titel">
<h2>
<xsl:apply-templates/>
</h2>
</xsl:template>Code example: The stylesheet’s transformation rule for <Titel>.
To try it, supply the processor with the XML file and stylesheet. In Oxygen XML Editor, create a new transformation, open the stylesheet and select the XML file. On the “Ausgabedatei” tab, specify that the output should open in the editor. Then start processing.
The result appears in a new file, as shown in the next figure.
Figure: Transformation result.
In the result, <Titel> has become <h2>, <Untertitel> has become <h4>, and <Strophe> has become <p>, exactly as the stylesheet defines. The following figure shows the browser view.
Figure: Browser view of the example file.
XSLT has become the standard way to convert XML into other formats. Technical writers have few alternatives, but it works very well.
Copyright © 2018 tcworld GmbH
You may print the online version for your private use.
Otherwise, this article from “technische kommunikation” (issue 2, 2018) is subject to the same conditions as the printed edition. The work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
tcworld GmbH, Rotebühlstraße 64, 70178 Stuttgart, info@tekom.de, www.tekom.de