Example: 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.
XSLT uses stylesheets to convert XML into other data formats. You can convert part or all of an XML document and select or rearrange the data using the XPath query language and the built-in functions of XSLT. XSLT is commonly used to convert XML to HTML, but can also be used to transform XML documents that comply with one XML schema into documents that comply with another schema. XSLT can also be used to convert XML data into unrelated formats, like comma-delimited text or formatting languages such as troff. XSLT has two main areas of applicability:
- Formatting (conversion of XML into HTML)
- Data exchange (querying, reorganizing and converting data from one XML schema to another, or into a data exchange format such as SOAP)
Both cases may require that an entire XML document or only selected parts of it be transformed. XSLT incorporates the XPath specification, permitting query and retrieval of arbitrary data from the source XML document. An XSLT template may also contain or create additional information such as file headers and instruction blocks that will be added to the output file.
How XSLT Works
XSLT stylesheets
are written in Extensible Stylesheet Language (XSL), an XML schema.
XSL is a template language rather than an algorithmic language such
as C or Perl, a feature that limits XSL's power but makes it uniquely
suited to its purpose. XSL stylesheets contain one or more 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.
The following example shows typical elements of an XSLT template. In this case the target will be XML documents containing inventory information, such as this record describing an ice scraper:
<?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 above, 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.