Transforming with XSLT stylesheets
The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas.
- Formatting (conversion of XML into HTML or formatting languages such as FOP);
- Data exchange (querying, reorganizing and converting data from one XML schema to another, or into a data exchange format such as SOAP).
How XSLT Works
template
elements,
which describe what action to take when a given XML element or query
is encountered in the target file. A typical XSLT template element
will start by specifying which element it applies to. For instance, <xsl:template match="product">
declares
that the contents of this template will be used to replace the content
of any <product>
tag encountered in the target
XML file. An XSLT file consists of a list of such templates, in no
necessary order.<?xml version="1.0"?>
<product pid="100-201-01">
<description>
<name>Ice Scraper, Windshield 4 inch</name>
<details>Basic Ice Scraper 4 inches wide, foam handle</details>
<price>3.99</price>
</description>
</product>
This record includes such information as the part number,
description and price of a windshield ice scraper. Some of this information
is contained within elements, such as <name>
.
Some, like the part number, are contained in attributes (in this case
the pid
attribute of the <product>
element).
To display this information as a web page, you could apply the following
XSLT template:<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h1><xsl:value-of select="/product/description/name"/></h1>
<table border="1">
<th>
<xsl:apply-templates select="product"/>
</th>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="product">
<tr>
<td width="80">product ID</td>
<td><xsl:value-of select="@pid"/></td>
</tr>
<tr>
<td width="200">product name</td>
<td><xsl:value-of select="/product/description/name"/></td>
</tr>
<tr>
<td width="200">price</td>
<td>$<xsl:value-of select="/product/description/price"/></td>
</tr>
<tr>
<td width="50">details</td>
<td><xsl:value-of select="/product/description/details"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
When an XSLT processor receives as
input both the template and target documents shown previously, it
will output the following HTML document:<html>
<body>
<h1>Ice Scraper, Windshield 4 inch</h1>
<table border="1">
<th>
<tr>
<td width="80">product ID</td><td>100-201-01</td>
</tr>
<tr>
<td width="200">product name</td><td>Ice Scraper, Windshield 4 inch</td>
</tr>
<tr>
<td width="200">price</td><td>$3.99</td>
</tr>
<tr>
<td width="50">details</td><td>Basic Ice Scraper 4 inches wide, foam handle</td>
</tr>
</th>
</table>
</body>
</html>
The XSLT processor tests the incoming XML document for given conditions (typically one condition per template). If a condition is true the template contents are inserted into the output, and if they are false the template is passed over by the processor. The stylesheet may add its own data to the output, for example in the HTML table tagging and strings such as "product ID."
XPath
can be used both to define template conditions, as in <xsl:template
match="product">
and to select and insert data from anywhere
in the XML stream, as in <h1><xsl:value-of select="/product/description/name"/></h1>
.
Using XSLTRANSFORM
You can use the XSLTRANSFORM function to apply XSLT stylesheets to XML data. If you supply the function with the name of an XML document and an XSLT stylesheet, the function will apply the stylesheet to the document and return the result.
If you specify the document function of XSLT in your XSLT stylesheet, ensure that the DB2_XSLT_ALLOWED_PATH registry variable is set to the directories from where you can download additional XML documents.