DB2 Version 10.1 for Linux, UNIX, and Windows

Example: Using the document function of XSLT

These examples illustrate all the steps required to use the document function of XSLT.

The use of built-in functions of XSLT is not recommended in pureXML. However, if you decide to use the document function of XSLT, you must set up the DB2_XSLT_ALLOWED_PATH registry variable to the list of directories containing your XML documents to limit the references to a list of URIs. By default, the document function of XSLT cannot access any XML document.

The following examples illustrate how to use the document function of XSLT in a secure fashion.

The examples store the XML document and the XSLT stylesheets in XML columns and use the XSLTRANSFORM function to convert the XML document using one of the XSLT stylesheets. The examples use the XMLDATA and XMLTRANS tables created for Example: Using XSLT to remove namespaces.

Example for Linux and UNIX environments

In this example, the XML files are located in the /home/user/xml_files directory. Set the DB2_XSLT_ALLOWED_PATH registry variable to this directory.
db2set DB2_XSLT_ALLOWED_PATH="/home/user/xml_files"

Restart the instance to make this registry variable setting effective.

Add the stylesheet to the XMLTRANS table using the INSERT statement.
INSERT INTO XMLTRANS (XSLID, XSLT) 
 VALUES ( 3, '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
               <xsl:template match="/">
                <xsl:value-of select="document(''/home/user/xml_files/n.xml'')/*"/>
               </xsl:template>
              </xsl:stylesheet>') ;
The following SELECT statement converts the sample XML document using the XSLT stylesheet with XSLID = 3.
SELECT XSLTRANSFORM (XMLDOC USING XSLT ) FROM XMLDATA, XMLTRANS 
   WHERE ID = 1 and XSLID = 3

The XSLTRANSFORM command converts the XML document using the XSLT stylesheet and returns the converted XML document. If the database manager cannot open the document specified in the document function of XSLT, then call to the document function is ignored.

Example for Windows environments

In this example, the XML files are located in the C:\Documents and Settings\user\xml_files directory. Set the DB2_XSLT_ALLOWED_PATH registry variable to this directory using one of the following ways:
  • db2set DB2_XSLT_ALLOWED_PATH="C:\Documents%20and%20Settings\user\xml_files"
  • db2set DB2_XSLT_ALLOWED_PATH="file:///C:/Documents%20and%20Settings/user/xml_files"
Use %20 to represent a blank space.

Restart the instance to make this registry variable setting effective.

Add the stylesheet to the XMLTRANS table using the INSERT statement.
INSERT INTO XMLTRANS (XSLID, XSLT) 
 VALUES ( 4, 
         '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:template match="/">
              <xsl:value-of select="document(''C:\Documents and Settings\user\xml_files\t.xml'')/*"/>
            </xsl:template>
          </xsl:stylesheet>') ;
The following SELECT statement converts the sample XML document using the XSLT stylesheet with XSLID = 4.
SELECT XSLTRANSFORM (XMLDOC USING XSLT ) FROM XMLDATA, XMLTRANS 
   WHERE ID = 1 and XSLID = 4

The XSLTRANSFORM command converts the XML document using the XSLT stylesheet and returns the converted XML document. If the database manager cannot open the document specified in the document function of XSLT, then call to the document function is ignored.