brief introduction to XSL transformations
XSL Transformations
XSLT is a language to transform XML documents into other formats, like
XHTML, XML, Plain Text, (etc.)
XPath is a language for
anvigating XML documents
Basic Transformation
An XSL Transformations is done by one XSLT docuemnt that takes
an XML docuemnt as input. The result of a transformation is a Textual
document. The result can be another XML document, a plain text
document, an XHTML document, or any other kind of textual document.
here is an example
sample input
This sample input describes a list of persons, with its personal
data
<persons> <person id="KF14" name="Karl Friederick"> <address>LudwigStrasse 14, Zurich</address> </person> <person id="MS12" name="Marc Schiller"> <address>Rue del la vie 12, Nice</address> </person> </persons>
sample transform
This sample transform produces an HTML document that shows data
in a table
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/persons"> <html> <head> <title>Persons</title> </head> <body> <h1>Persons</h1> <table border="1"> <tr> <td>id</td> <td>name</td> <td>address</td> </tr> <xsl:for-each select="person" > <tr> <td><xsl:value-of select="@id"/></td> <td><xsl:value-of select="@name"/></td> <td><xsl:value-of select="address/text()"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
output
The transformation output is a webpage that contains XML informations
presented into an HTML webpage.
The webpage looks like the
following image.
basic example files
Here you can download the files of the basic example
Intermediate Transformations
In this transformation we take an XML input document as source,
and we transform it, by hiding some informations and transforming some
others
sample input
We take the sample input seen before, but we put additional
info, like telephone number and email
<persons> <person id="KF14" name="Karl Friederick"> <address>LudwigStrasse 14, Zurich</address> <telephone>(41) 444 222 899</telephone> <email>c.friederick@dot.com</email> </person> <person id="MS12" name="Marc Schiller"> <address>Rue del la vie 12, Nice</address> <telephone>(33) 993 212 888</telephone> <telephone private="yes">(33) 448 333 717</telephone> <email>m.schiller@dot.com</email> </person> </persons>
sample transform
In this transform we make a deep copy of the input, but we
exclude some nodes, and transform some others.
- we want to hide private telephone
- we want to transform email address by replacing ‘@‘
with #
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="node() | @*"> <xsl:choose> <xsl:when test="local-name()='telephone' and @private='yes'"> <!-- does nothing --> </xsl:when> <xsl:when test="local-name()='email'"> <email> <xsl:value-of select="translate(text(),'@', '#')"/> </email> </xsl:when> <xsl:otherwise> <xsl:copy> <xsl:apply-templates select="@* | node()" /> </xsl:copy> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
Note: this solution works with single characters,
since XSLT 1.0 does not provides a
string-replace
function. To replace an entire string you have two ways:
- use the
replace( string, string, string)
function, with an xslt 2.0 processor - write a an XSL 1.0 template that implements an equivalent of
replace(...)
function
output
The output, is the xml document we expect: without
‘private’ telephone, and with ‘@’ character
substituted by ‘#’
<?xml version="1.0" encoding="utf-8"?><persons> <person id="KF14" name="Karl Friederick"> <address>LudwigStrasse 14, Zurich</address> <telephone>(41) 444 222 899</telephone> <email>c.friederick#dot.com</email> </person> <person id="MS12" name="Marc Schiller"> <address>Rue del la vie 12, Nice</address> <telephone>(33) 993 212 888</telephone> <email>m.schiller#dot.com</email> </person> </persons>
intermediate example files
Here you can download the files of the advanced example