15.6.5.Programmatic Examples

Example15.19.Executing an XML template from the file system

First step is to setup the /xmlt directory on file system. The function vhost_define() will be used to enable execution of XML templates. DBA privileges are required to use this function:

SQL> vhost_define (lpath=>'/xmlt', ppath=>'/xmlt/',
  vsp_user=>'demo', opts=>vector('xml_templates', 'yes'));

Now the following files must be stored as: <www-root>/xmlt/file1.xml

<?xml version ='1.0' encoding='UTF-8'?>
 <root xmlns:sql='urn:schemas-openlink-com:xml-sql' sql:xsl='shippers.xsl'>
   <!-- XML template example -->

   <!-- parameters declaration -->

      <sql:param name=":ShipperID">2</sql:param>
      <sql:param name=":CompanyName">United Package (you should see me)</sql:param>
      <sql:param name=":Phone">(503) 555-3199</sql:param>
   <!-- XML updategram , this will update the second record -->

            <Shippers sql:id="1" ShipperID=":ShipperID"/>
            <Shippers sql:id="1" ShipperID=":ShipperID" CompanyName=":CompanyName" Phone=":Phone"/>
   <!-- make a parametrized query -->

      SELECT ShipperID, CompanyName,Phone FROM Shippers where ShipperID = :ShipperID FOR XML AUTO
   <!-- make an error to see what happens -->

      select * from NotExist for xml auto

Now we want an XSL stylesheet stored as: <www-root>/xmlt/shippers.xslt containing the following:

<?xml version="1.0"?>

    <xsl:output method="html" indent="yes" />

  <xsl:template match="/">
            <title>Shippers list</title>

  <xsl:template match="root">

  <xsl:template match="Shippers">
            <td><xsl:value-of select="@ShipperID"/></td>
            <td><xsl:value-of select="@CompanyName"/></td>
            <td><xsl:value-of select="@Phone"/></td>

Point your web browser to:


and then at:


for the results.

Example15.20.Executing an XML template from WebDAV

The first step is to create a new DAV collection and configure it to allow XML template execution. DBA privileges are required for these operations. To create a DAV collection the function dav_col_create() can be used, followed by the vhost_define() function to allow XML template execution:

SQL> dav_col_create ('/DAV/xmlt/', '110100100', 'dav', 'dav', 'dav', 'dav');
SQL> vhost_define (lpath=>'/DAV/xmlt', ppath=>'/DAV/xmlt/',
  is_dav=>1, vsp_user=>'demo', opts=>vector('xml_templates', 'yes'));

Now, the file can be uploaded. The same file as in the previous example will be used, copied from the file system location to the DAV location: <www-root>/DAV/xmlt/file1.xml

SQL> dav_res_upload ('/DAV/xmlt/file1.xml',
  file_to_string (concat (http_root(), '/xmlt/file1.xml')), 'text/xml',
  '111101101N', 'dav', 'dav', 'dav', 'dav');

Likewise the XSLT stylesheet from the previous example will also be used in the same way, placing into the DAV location: <www-root>/DAV/xmlt/shippers.xslt

SQL> dav_res_upload ('/DAV/xmlt/shippers.xsl',
  file_to_string (concat (http_root(), '/xmlt/shippers.xsl')),
  'text/xsl', '110100100N', 'dav', 'dav', 'dav', 'dav');

This example can now be demonstrated by trying the following URLs in your web browser: