Example: Using XSLT for data exchange
This example illustrates how to use the built-in XSLTRANSFORM function to convert XML documents for data exchange.
This example uses parameters with the stylesheet to produce different data exchange formats at runtime.
We use a stylesheet that incorporates xsl:param elements to capture data from a parameter file.
INSERT INTO Display_productdetails values(1, '<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="headline"/>
<xsl:param name="supermarketname"/>
<xsl:template match="product">
<html>
<head/>
<body>
<h1><xsl:value-of select="$headline"/></h1>
<table border="1">
<th>
<tr>
<td width="80">product ID</td>
<td width="200">product name</td>
<td width="200">price</td>
<td width="50">details</td>
<xsl:choose>
<xsl:when test="$supermarket =''true'' ">
<td width="200">BIG BAZAAR super market</td>
</xsl:when>
</xsl:choose>
</tr>
</th>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="product">
<tr>
<td><xsl:value-of select="@pid"/></td>
<td><xsl:value-of select="/product/description/name"/></td>
<td><xsl:value-of select="/product/description/price"/></td>
<td><xsl:value-of select="/product/description/details"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>'
);
The parameter file contains parameters corresponding to the ones in the XSLT template, with content:
CREATE TABLE PARAM_TAB (DOCID INTEGER, PARAM VARCHAR (10K));
INSERT INTO PARAM_TAB VALUES
(1,
'<?xml version="1.0"?>
<params xmlns="http://www.ibm.com/XSLTransformParameters">
<param name="supermarketname" value="true"/>
<param name="headline">BIG BAZAAR super market</param>
</params>'
);
You can then apply the parameter file at runtime using the following command:
SELECT XSLTRANSFORM (XML_DOC USING XSL_DOC WITH PARAM AS CLOB (1M))
FROM product_details X, PARM_TAB P WHERE X.DOCID=P.DOCID;
The result is HTML, but with content determined by the parameter file and tests done against the content of the XML document:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1></h1>
<table border="1">
<th>
<tr>
<td width="80">product ID</td>
<td width="200">product Name</td>
<td width="200">price</td>
<td width="50">Details</td>
</tr>
</th>
</table>
</body>
</html>
In other applications, the output of XSLTRANSFORM might not be HTML but rather another XML document or a file using a different data format, such as an EDI file.
For data exchange applications, the parameter file could contain EDI or SOAP file header information such as e-mail or port addresses, or other critical data unique to a particular transaction. Since the XML used in the above examples is an inventory record, it is easy to imagine using XSLT to repackage this record for exchange with a client's purchasing system.