Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > 71d40963b505df4524269198e237b3e3 > files > 1013

virtuoso-opensource-doc-6.1.4-2.fc14.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
 <head profile="http://internetalchemy.org/2003/02/profile">
  <link rel="foaf" type="application/rdf+xml" title="FOAF" href="http://www.openlinksw.com/dataspace/uda/about.rdf" />
  <link rel="schema.dc" href="http://purl.org/dc/elements/1.1/" />
  <meta name="dc.title" content="13. XML Support" />
  <meta name="dc.subject" content="13. XML Support" />
  <meta name="dc.creator" content="OpenLink Software Documentation Team ;&#10;" />
  <meta name="dc.copyright" content="OpenLink Software, 1999 - 2009" />
  <link rel="top" href="index.html" title="OpenLink Virtuoso Universal Server: Documentation" />
  <link rel="search" href="/doc/adv_search.vspx" title="Search OpenLink Virtuoso Universal Server: Documentation" />
  <link rel="parent" href="webandxml.html" title="Chapter Contents" />
  <link rel="prev" href="updategrams.html" title="Using UpdateGrams to Modify Data" />
  <link rel="next" href="xmlschema.html" title="XML DTD and XML Schemas" />
  <link rel="shortcut icon" href="../images/misc/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" type="text/css" href="doc.css" />
  <link rel="stylesheet" type="text/css" href="/doc/translation.css" />
  <title>13. XML Support</title>
  <meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8" />
  <meta name="author" content="OpenLink Software Documentation Team ;&#10;" />
  <meta name="copyright" content="OpenLink Software, 1999 - 2009" />
  <meta name="keywords" content="" />
  <meta name="GENERATOR" content="OpenLink XSLT Team" />
 </head>
 <body>
  <div id="header">
    <a name="xmltemplates" />
    <img src="../images/misc/logo.jpg" alt="" />
    <h1>13. XML Support</h1>
  </div>
  <div id="navbartop">
   <div>
      <a class="link" href="webandxml.html">Chapter Contents</a> | <a class="link" href="updategrams.html" title="Using UpdateGrams to Modify Data">Prev</a> | <a class="link" href="xmlschema.html" title="XML DTD and XML Schemas">Next</a>
   </div>
  </div>
  <div id="currenttoc">
   <form method="post" action="/doc/adv_search.vspx">
    <div class="search">Keyword Search: <br />
        <input type="text" name="q" /> <input type="submit" name="go" value="Go" />
    </div>
   </form>
   <div>
      <a href="http://www.openlinksw.com/">www.openlinksw.com</a>
   </div>
   <div>
      <a href="http://docs.openlinksw.com/">docs.openlinksw.com</a>
   </div>
    <br />
   <div>
      <a href="index.html">Book Home</a>
   </div>
    <br />
   <div>
      <a href="contents.html">Contents</a>
   </div>
   <div>
      <a href="preface.html">Preface</a>
   </div>
    <br />
   <div class="selected">
      <a href="webandxml.html">XML Support</a>
   </div>
    <br />
   <div>
      <a href="forxmlforsql.html">Rendering SQL Queries as XML (FOR XML Clause)</a>
   </div>
   <div>
      <a href="composingxmlinsql.html">XML Composing Functions in SQL Statements (SQLX)</a>
   </div>
   <div>
      <a href="xmlservices.html">Virtuoso XML Services</a>
   </div>
   <div>
      <a href="queryingxmldata.html">Querying Stored XML Data</a>
   </div>
   <div>
      <a href="updategrams.html">Using UpdateGrams to Modify Data</a>
   </div>
   <div class="selected">
      <a href="xmltemplates.html">XML Templates</a>
    <div>
        <a href="#xmltemplatesyntax" title="Syntax">Syntax</a>
        <a href="#saveqrytoxmltemplatessql" title="Saving SQL Queries to XML Template">Saving SQL Queries to XML Template</a>
        <a href="#saveqrytoxmltemplatessql" title="Saving XQUERY Queries to XML Template">Saving XQUERY Queries to XML Template</a>
        <a href="#saveqrytoxmltemplatessql" title="Saving XPATH Queries to XML Template">Saving XPATH Queries to XML Template</a>
        <a href="#xmltemplatesexamples" title="Programmatic Examples">Programmatic Examples</a>
    </div>
   </div>
   <div>
      <a href="xmlschema.html">XML DTD and XML Schemas</a>
   </div>
   <div>
      <a href="xq.html">XQuery 1.0 Support</a>
   </div>
   <div>
      <a href="xslttrans.html">XSLT Transformation</a>
   </div>
   <div>
      <a href="xmltype.html">XMLType</a>
   </div>
   <div>
      <a href="xmldom.html">Changing XML entities in DOM style</a>
   </div>
    <br />
  </div>
  <div id="text">
    <a name="xmltemplates" />
    <h2>13.6. XML Templates</h2>

 <p>Virtuoso XML templates allow execution of queries over HTTP in order to
 achieve any combination of the following:</p>

 <ul>
      <li>Execution of SQL queries returned in an XML formatted resultset.</li>
      <li>Execution of XQuery and XPath queries against XML and SQL-XML
  documents in the Virtuoso WebDAV repository.</li>
      <li>Use an XML notation (Updategrams) to insert/update/delete SQL data.</li>
    </ul>

  <p>An XML template is an XML file containing a query, optional parameters
  with default values for the query, a place to specify an XSL stylesheet, and a
  section for specifying updatagram based synchronization metrics. They are
  meant to be executed as an XML described short-cut to an query result, an XML
  document.  The XML document returned from calling an XML template can be
  served raw, or transformed using XSLT.</p>

 <p>XML templates can be executed from within Virtuoso procedure language
 using the <a href="fn_xml_template.html">xml_template()</a>
 function, or published as SOAP compliant XML Web Services, which makes
 them accessible to any SOAP and WSDL aware environment.</p>

  <p>XML templates provide quick easy access to results from a SQL query
  as usual, but now this can be saved to a file.  The results are not saved, just
  the query definition.  You can use this feature to rapidly produce dynamic
  reports that can potentially be rendered in different ways by providing
  an alternate stylesheet.  The report can be refined on the fly by providing
  parameters for the query.  The output is reachable via HTTP directly by
  providing the URL to the template.</p>

  <p>When XML templates are executed via HTTP, the XSLT transformation
  will be performed automatically if the &quot;sql:xsl&quot; attribute is specified
  in the templates root element.  This means that the default XSLT transformation
  cannot be avoided if the template is executed over HTTP.  However, you have
  the option whether to apply the transformation or not when using the
  <span class="computeroutput">xml_template()</span> function.  XML templates containing
  &lt;sql:query&gt; elements return either results or messages regarding
  the queries.  An XSLT transformation can be made on the result
  from <span class="computeroutput">xml_template()</span> using the returned XSLT
  sheet URL.  Hence it is conditional in PL programming.  The application
  developers can choose to either use the style sheet specified in the template,
  use another style sheet, or skip XSLT transformation entirely.  This feature
  provides full flexibility for the procedure developer.</p>

  <p>XML templates are intended for execution over HTTP.  The template
  file can be stored either on the file system, WebDAV repository or stored on
  another HTTP server being referenced by a URL parameter.  Specifying a
  template as a URL parameter naturally introduces a new potential security
  risk associated with the template owners web server.</p>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="Conceptual View of XML Templates" src="../images/xmltemplatediagram.jpg" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.1. Conceptual View of XML Templates</td>
    </tr>
    </table>

  <p>Queries and updategrams can be parameterized. The parameters must be
  defined in the header element, which consists of zero or more param elements.
  The default value of the parameter is enclosed within the param element whose
  values are typically replaced during the execution phases.  When XML templates
  are executed from the <span class="computeroutput">xml_template()</span> function the
  parameters are specified in a vector as an input parameter.  When XML templates
  are executed via HTTP the parameters are contained in the URL.
  <span class="important">
        <strong>Important:</strong> All &lt;sql:param&gt; parameter definitions in the template
  for SQL queries need to be named parameters, and the names must be preceded
  by a colon i.e. :ParamName (note colon at the front)</span>.  The
  colon is not required for updategrams.</p>

  <p>If an error occurs as a result of executing a SQL query or updategram,
  the comment will be included in the result detailing the error.  The
  subsequent queries and updategrams will still be executed.</p>

  <div class="tip">
      <div class="tiptitle">See Also:</div>
  <p>The <a href="expwsmodules.html">Exposing Persistent Stored Modules as Web
  Services</a> section as this also describes how XML Templates can be
  published to web services as a SOAP messages using a PL Wrapper.  This
  is achieved using the Administrative Interface.</p>
  <p>
        <a href="fn_xml_template.html">xml_template()</a>
      </p>
  </div>

  <a name="xmltemplatesyntax" />
    <h3>13.6.2. Syntax</h3>

  <p>The format of an XML template is shown below, illustrating how SQL
  queries and updategrams are specified:</p>

  <div>
      <pre class="programlisting">
&lt;root xmlns:sql=&quot;urn:schemas-openlink-com:xml-sql&quot; sql:xsl=&#39;xslt url&#39; &gt;
  &lt;sql:header&gt;
    &lt;sql:param name=&quot;:...&quot;&gt;..&lt;/sql:param&gt;
    &lt;sql:param name=&quot;:...&quot;&gt;..&lt;/sql:param&gt;...
  &lt;/sql:header&gt;
  &lt;sql:query&gt;
    select ... for xml ..
  &lt;/sql:query&gt;
  &lt;sql:sync&gt;
    Update gramm
  &lt;/sql:sync&gt;
&lt;/root&gt;
</pre>
    </div>

  <p>The root element can contain two attributes.  The first is the
  required namespace declaration of xmlns:sql=&quot;urn:schemas-openlink-com:xml-sql&quot;,
  required to identify the XML as an XML template. The second
  attribute allows the specification of an optional XSLT stylesheet to be
  applied to the resulting XML document, if applicable.  The XSLT stylesheet
  file can be specified as either a relative or absolute URL.</p>
  <p>The following elements can be defined in the &quot;sql&quot; namespace for an
  XML Template:</p>

  <ul>
    <li>The <strong>&quot;root&quot;</strong> tag can be any name, its purpose
    is to provide the top-level element required for any well-formed XML, and
    provides the root element for the resulting XML document, if any.</li>
    <li>
        <strong>&lt;sql:header&gt;</strong> This tag is used to hold
    the parameters for templates execution such as &lt;sql:param&gt; elements.
    The &lt;sql:header&gt; element allow us to define multiple parameters.</li>
    <li>
        <strong>&lt;sql:param&gt;</strong> This element is used to
    define a named parameter that is passed to the query and/or updategram
    inside the template.  Each &lt;xsl:param&gt; element defines one parameter.
    Multiple &lt;xsl:param&gt; elements can be specified within the
    &lt;sql:header&gt; element.</li>
    <li>
        <strong>&lt;sql:query&gt;</strong> This element is used to
    specify SQL/XML queries.  It is possible to specify multiple
    &lt;sql:query&gt; elements within a single template.</li>
    <li>
        <strong>&lt;sql:sync&gt;</strong> This element is used to
    contain updategrams.</li>
  </ul>

  <p>&lt;sql:query&gt; and &lt;sql:sync&gt; entries are executed
  sequentially in the order they appear as separate transactions.  Therefore, if
  you have multiple &lt;sql:query&gt; or &lt;sql:sync&gt; elements in the
  template, if one fails, the others will proceed.</p>

  <p>XML templates can be executed directly from Virtuoso PL using the
  <a href="fn_xml_template.html">xml_template()</a>
  function.</p>

  <p>To allow XML template execution from a Virtuoso virtual directory and
  it&#39;s descendants the special option: &#39;xml_templates&#39; must be used in the virtual
  directory definition.  This option can be set from the
  <a href="htmlconductorbar.html#httpvirtualdirs">Conductor / Web Application Server / Virtual Domains &amp; Directories</a> or from
  SQL (or ISQL) using the <a href="fn_vhost_define.html">vhost_define(.... , opts=&gt;vector(&#39;xml_templates&#39;, &#39;yes&#39;));</a> function.  In the usual
  way, the SQL user specified for VSP execution within the virtual directory
  definition will be used for executing the templates within such web
  directories.  If your XML Templates are to run from a DAV directory then
  you must allow suitable execute permissions for the file and directory.
  </p>

  <p>When a virtual directory is configured to allow execution of XML templates
  be aware that this also means that descendant directories inherit this
  property.  Also be aware that this also allows execution of VSP files in the
  normal way.  WebDAV also has another layer of security attributes that
  will need to be set to enable files to be executable.  By default
  files in DAV do not have execute privileges.</p>

  <p>Explicitly specifying &quot;contenttype=text/html&quot; as a URL parameter
  will instruct the Virtuoso HTTP server to return the HTML output.</p>

  <div class="tip">
      <div class="tiptitle">See Also</div>
  <p>Functions: <a href="fn_xml_template.html">xml_template()</a>,
    <a href="fn_xmlsql_update.html">xmlsql_update()</a>,
    <a href="fn_xml_auto.html">xml_auto()</a>.</p>
  <p>
        <a href="updategrams.html">UpdateGrams</a>.</p>
  <p>The <a href="forxmlforsql.html">FOR XML clause</a>.</p>
  </div>

  <br />

<a name="saveqrytoxmltemplatessql" />
    <h3>13.6.3. Saving SQL Queries to XML Template</h3>

  <p>Using the Conductor Administration Interface, we are going to make
  and save a query to an XML template in WebDav, and then demonstration its
  use from a browser.</p>
  <p>First we will make a new WebDAV directory. From Web Application Server -&gt; Content Management
-&gt; Repository click the &quot;New folder&quot; icon and in the shown form enter for &quot;Folder Name&quot;: xmlt. Choose for
&quot;Owner&quot; dav and click the &quot;Create&quot; button.</p>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="Creating a new directory (collection) in WebDAV" src="../images/xmltemplate001.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.3.1. Creating a new directory (collection) in WebDAV</td>
    </tr>
    </table>

  <p>Next we need to configure a virtual directory mapping to this so
  that XML Templates can be executed later. Go to Web Application Server / Virtual Domains &amp; Directories
and for your {Default Web Site} click the link &quot;New Directory&quot;. In the shown form click &quot;Next&quot;.</p>
    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="Configuring a Virtual Directory to respond to XML Template requests from our Dav" src="../images/xmltemplate004.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.3.2. Configuring a Virtual Directory to respond to XML Template requests from our Dav</td>
    </tr>
    </table>
<p>The programmatic equivalent for creating a virtual directory is the command in iSQL:</p>
<div>
      <pre class="programlisting">
SQL&gt;vhost_define(lpath=&gt;&#39;/DAV/xmlt&#39;,
                 ppath=&gt;&#39;/DAV/xmlt/&#39;,
                 is_dav=&gt;1,
                 vsp_user=&gt;&#39;demo&#39;,
                 opts=&gt;vector(&#39;xml_templates&#39;, &#39;yes&#39;));
</pre>
    </div>

  <p>Now we go to Database/Interactive SQL and will use a simple query. Here
  we are assuming that the Demo database is being used, so we will query the
  Customers table:</p>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="A SQL Query" src="../images/xmltemplate002.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.3.3. A SQL Query</td>
    </tr>
    </table>
    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="Results" src="../images/xmltemplate002a.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.3.4. Results</td>
    </tr>
    </table>

  <p>Now go to XML -&gt; SQL-XML and enter in the query area the sql statement from above.
Here we can supply some &quot;FOR XML&quot; clause, &quot;FOR XML AUTO&quot; being the simplest.
Enter in the field &quot;WebDAV resource path for the result&quot; the directory we created earlier, i.e.
it should be /DAV/xmlt and add the name of the xmltemplate to which the result will be saved,
for example: sql-template.xml So finally the value should be <span class="computeroutput">/DAV/xmlt/sql-template.xml</span>.</p>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="The Save XML Template page and settings" src="../images/xmltemplate005.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.3.5. The Save XML Template page and settings</td>
    </tr>
    </table>

  <p>Once the template has been saved we can test it.  Point your browser at
  you newly created file, a URL similar to:
  <span class="computeroutput">http://host:port/DAV/xmlt/sql-template.xml</span>:</p>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="The test results: http://host:port/DAV/xmlt/sql-template.xml" src="../images/xmltemplate006.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.3.6. The test results: http://host:port/DAV/xmlt/sql-template.xml</td>
    </tr>
    </table>

  <div class="tip">
      <div class="tiptitle">See Also:</div>
  <p>
        <a href="">Exposing XML Templates as Web Services</a>.</p>
  </div>

<br />

<a name="saveqrytoxmltemplatessql" />
    <h3>13.6.4. Saving XQUERY Queries to XML Template</h3>

  <p>Using the Conductor Administration Interface, we are going to make
  and save a query to an XML template in WebDav, and then demonstration its
  use from a browser.</p>
  <p>First we will make a new WebDAV directory. From Web Application Server -&gt; Content Management
-&gt; Repository click the &quot;New folder&quot; icon and in the shown form enter for &quot;Folder Name&quot;: xmlt. Choose for
&quot;Owner&quot; dav and click the &quot;Create&quot; button.</p>

  <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="Creating a new directory (collection) in WebDAV" src="../images/xmltemplate001.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.4.1. Creating a new directory (collection) in WebDAV</td>
    </tr>
    </table>

  <p>Next we need to configure a virtual directory mapping to this so
  that XML Templates can be executed later. Go to Web Application Server / Virtual Domains &amp; Directories
and for your {Default Web Site} click the link &quot;New Directory&quot;. In the shown form click &quot;Next&quot;.</p>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="Configuring a Virtual Directory to respond to XML Template requests from our Dav" src="../images/xmltemplate004.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.4.2. Configuring a Virtual Directory to respond to XML Template requests from our Dav</td>
    </tr>
    </table>
<p>The programmatic equivalent for creating a virtual directory is the command in iSQL:</p>
<div>
      <pre class="programlisting">
SQL&gt;vhost_define(lpath=&gt;&#39;/DAV/xmlt&#39;,
                 ppath=&gt;&#39;/DAV/xmlt/&#39;,
                 is_dav=&gt;1,
                 vsp_user=&gt;&#39;demo&#39;,
                 opts=&gt;vector(&#39;xml_templates&#39;, &#39;yes&#39;));
</pre>
    </div>

  <p>Now we go to XML -&gt; XQuery -&gt; XQuery Advanced.</p>

  <p>We will start by testing the following query against the Demo database</p>

<div>
      <pre class="programlisting">
&lt;bib&gt;
   {
   for $b in document(&quot;bib.xml&quot;)/bib/book
   where $b/publisher = &quot;Addison-Wesley&quot; and $b/@year &gt; 1991
   return
      &lt;book year = {$b/@year}&gt;
         {$b/title}
      &lt;/book&gt;
   }
&lt;/bib&gt;
</pre>
    </div>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="XQUERY query against the Demo database" src="../images/xmltemplate007.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.4.3. XQUERY query against the Demo database</td>
    </tr>
    </table>

    <p>Now we will save this as an XML template by pressing the Save button.
    The query we used will be copied to the save XML template page.  We will ensure
    that the name of the file is
    <span class="computeroutput">/DAV/xmlt/xquery-template.xml</span>:</p>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="Saving the XML template for the Xquery query" src="../images/xmltemplate008.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.4.4. Saving the XML template for the Xquery query</td>
    </tr>
    </table>

  <div class="tip">
      <div class="tiptitle">See Also:</div>
  <p>
        <a href="">Exposing XML Templates as Web Services</a>.</p>
  </div>

<br />

<a name="saveqrytoxmltemplatessql" />
    <h3>13.6.5. Saving XPATH Queries to XML Template</h3>

  <p>Using the Conductor Administration Interface, we are going to make
  and save a query to an XML template in WebDav, and then demonstration its
  use from a browser.</p>
  <p>First we will make a new WebDAV directory. From Web Application Server -&gt; Content 
  	Management -&gt; Repository click the &quot;New folder&quot; icon and in the shown form enter for 
  	&quot;Folder Name&quot;: xmlt. Choose for &quot;Owner&quot; dav and click the &quot;Create&quot; button.</p>

<table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="Creating a new directory (collection) in WebDAV" src="../images/xmltemplate001.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.5.1. Creating a new directory (collection) in WebDAV</td>
    </tr>
    </table>

  <p>Next we need to configure a virtual directory mapping to this so
  that XML Templates can be executed later. Go to Web Application Server -&gt; Virtual Domains &amp; Directories
and for your {Default Web Site} click the link &quot;New Directory&quot;. In the shown form click &quot;Next&quot;:</p>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="Configuring a Virtual Directory to respond to XML Template requests from our Dav" src="../images/xmltemplate004.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.5.2. Configuring a Virtual Directory to respond to XML Template requests from our Dav</td>
    </tr>
    </table>
<p>The programmatic equivalent for creating a virtual directory is the command in iSQL:</p>
<div>
      <pre class="programlisting">
SQL&gt;vhost_define(lpath=&gt;&#39;/DAV/xmlt&#39;,
                 ppath=&gt;&#39;/DAV/xmlt/&#39;,
                 is_dav=&gt;1,
                 vsp_user=&gt;&#39;demo&#39;,
                 opts=&gt;vector(&#39;xml_templates&#39;, &#39;yes&#39;));
</pre>
    </div>

    <p>Now we set our sights on the XML files in WebDAV.  We will perform a very
    simple XPATH query on an XML source file from this documentation.  We will
    search for the existence of <span class="computeroutput">&lt;title&gt;</span>
    tags in <span class="computeroutput">http://host:port/DAV/docsrc/adminui.xml</span>:</p>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="XPATH query on a file in WebDAV" src="../images/xmltemplate009.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.5.3. XPATH query on a file in WebDAV</td>
    </tr>
    </table>

    <p>Make a quick mental note of the trivial result.  Now we will save our query
    as an XML template to be executed over HTTP.  Pressing the Save button takes
    us to the save XML Template page.  We ensure that the file to save is set to
    <span class="computeroutput">/DAV/xmlt/xpath-template.xml</span>:</p>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="" src="../images/xmltemplate010.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.5.4. </td>
    </tr>
    </table>

    <p>Once our query has been saved in an XML template we can test it by
    pointing a web browser at the URL for the file as we configured our
    virtual directory for, which will be similar to:
    <span class="computeroutput">http://localhost:8890/DAV/xmlt/xpath-template.xml</span>:</p>

    <table class="figure" border="0" cellpadding="0" cellspacing="0">
    <tr>
     <td>
          <img alt="" src="../images/xmltemplate011.png" />
     </td>
    </tr>
    <tr>
        <td>Figure: 13.6.5.5. </td>
    </tr>
    </table>

  <div class="tip">
      <div class="tiptitle">See Also:</div>
  <p>
        <a href="">Exposing XML Templates as Web Services</a>.</p>
  </div>
<br />

  <a name="xmltemplatesexamples" />
    <h3>13.6.6. Programmatic Examples</h3>

<a name="ex_xmltemplatefs" />
    <div class="example">
      <div class="exampletitle">Executing an XML template from the file system</div>
  <p>First step is to setup the /xmlt directory on file system.  The function
  <a href="fn_vhost_define.html">vhost_define()</a>
  will be used to enable execution of XML templates.  DBA privileges are required
  to use this function:</p>
<div>
        <pre class="programlisting">
SQL&gt; vhost_define (lpath=&gt;&#39;/xmlt&#39;, ppath=&gt;&#39;/xmlt/&#39;,
  vsp_user=&gt;&#39;demo&#39;, opts=&gt;vector(&#39;xml_templates&#39;, &#39;yes&#39;));
</pre>
      </div>

  <p>Now the following files must be stored as: &lt;www-root&gt;/xmlt/file1.xml</p>

<div>
        <pre class="programlisting">
&lt;?xml version =&#39;1.0&#39; encoding=&#39;UTF-8&#39;?&gt;
 &lt;root xmlns:sql=&#39;urn:schemas-openlink-com:xml-sql&#39; sql:xsl=&#39;shippers.xsl&#39;&gt;
   &lt;!-- XML template example --&gt;
   &lt;!-- parameters declaration --&gt;
    &lt;sql:header&gt;
      &lt;sql:param name=&quot;:ShipperID&quot;&gt;2&lt;/sql:param&gt;
      &lt;sql:param name=&quot;:CompanyName&quot;&gt;United Package (you should see me)&lt;/sql:param&gt;
      &lt;sql:param name=&quot;:Phone&quot;&gt;(503) 555-3199&lt;/sql:param&gt;
    &lt;/sql:header&gt;
   &lt;!-- XML updategram , this will update the second record --&gt;
    &lt;sql:sync&gt;
        &lt;sql:before&gt;
            &lt;Shippers sql:id=&quot;1&quot; ShipperID=&quot;:ShipperID&quot;/&gt;
        &lt;/sql:before&gt;
        &lt;sql:after&gt;
            &lt;Shippers sql:id=&quot;1&quot; ShipperID=&quot;:ShipperID&quot; CompanyName=&quot;:CompanyName&quot; Phone=&quot;:Phone&quot;/&gt;
        &lt;/sql:after&gt;
    &lt;/sql:sync&gt;
   &lt;!-- make a parametrized query --&gt;
   &lt;sql:query&gt;
      SELECT ShipperID, CompanyName,Phone FROM Shippers where ShipperID = :ShipperID FOR XML AUTO
   &lt;/sql:query&gt;
   &lt;!-- make an error to see what happens --&gt;
   &lt;sql:query&gt;
      select * from NotExist for xml auto
   &lt;/sql:query&gt;
&lt;/root&gt;
</pre>
      </div>

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

<div>
        <pre class="programlisting">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    &lt;xsl:output method=&quot;html&quot; indent=&quot;yes&quot; /&gt;

    &lt;xsl:template match=&quot;/&quot;&gt;
	&lt;html&gt;
	    &lt;head&gt;
	    &lt;title&gt;Shippers list&lt;/title&gt;
	    &lt;/head&gt;
	    &lt;body&gt;
	    &lt;xsl:apply-templates/&gt;
	    &lt;/body&gt;
	&lt;/html&gt;
    &lt;/xsl:template&gt;

    &lt;xsl:template match=&quot;root&quot;&gt;
	&lt;table&gt;
	    &lt;tr&gt;&lt;td&gt;ID&lt;/td&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;Phone&lt;/td&gt;&lt;/tr&gt;
	    &lt;xsl:apply-templates/&gt;
	&lt;/table&gt;
    &lt;/xsl:template&gt;

    &lt;xsl:template match=&quot;Shippers&quot;&gt;
	&lt;tr&gt;
	    &lt;td&gt;&lt;xsl:value-of select=&quot;@ShipperID&quot;/&gt;&lt;/td&gt;
	    &lt;td&gt;&lt;xsl:value-of select=&quot;@CompanyName&quot;/&gt;&lt;/td&gt;
	    &lt;td&gt;&lt;xsl:value-of select=&quot;@Phone&quot;/&gt;&lt;/td&gt;
	&lt;/tr&gt;
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</pre>
      </div>

  <p>Point your web browser to: </p>
  <div>
        <pre class="programlisting">http://[host:port]/xmlt/file1.xml</pre>
      </div>
  <p>and then at: </p>
  <div>
        <pre class="programlisting">http://[host:port]/xmlt/file1.xml?:ShipperID=3</pre>
      </div>
  <p>for the results.</p>
</div>

<a name="ex_xmltemplatedav" />
    <div class="example">
      <div class="exampletitle">Executing an XML template from WebDAV</div>

  <p>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 <a href="fn_dav_api_add.html">
  dav_col_create()</a> can be used, followed by the
  <a href="fn_vhost_define.html">vhost_define()</a>
  function to allow XML template execution:</p>

<div>
        <pre class="programlisting">
SQL&gt; dav_col_create (&#39;/DAV/xmlt/&#39;, &#39;110100100&#39;, &#39;dav&#39;, &#39;dav&#39;, &#39;dav&#39;, &#39;dav&#39;);
</pre>
      </div>

<div>
        <pre class="programlisting">
SQL&gt; vhost_define (lpath=&gt;&#39;/DAV/xmlt&#39;, ppath=&gt;&#39;/DAV/xmlt/&#39;,
  is_dav=&gt;1, vsp_user=&gt;&#39;demo&#39;, opts=&gt;vector(&#39;xml_templates&#39;, &#39;yes&#39;));
</pre>
      </div>

  <p>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:
  &lt;www-root&gt;/DAV/xmlt/file1.xml</p>

<div>
        <pre class="programlisting">
SQL&gt; dav_res_upload (&#39;/DAV/xmlt/file1.xml&#39;,
  file_to_string (concat (http_root(), &#39;/xmlt/file1.xml&#39;)), &#39;text/xml&#39;,
  &#39;111101101N&#39;, &#39;dav&#39;, &#39;dav&#39;, &#39;dav&#39;, &#39;dav&#39;);
</pre>
      </div>

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

<div>
        <pre class="programlisting">
SQL&gt; dav_res_upload (&#39;/DAV/xmlt/shippers.xsl&#39;,
  file_to_string (concat (http_root(), &#39;/xmlt/shippers.xsl&#39;)),
  &#39;text/xsl&#39;, &#39;110100100N&#39;, &#39;dav&#39;, &#39;dav&#39;, &#39;dav&#39;, &#39;dav&#39;);
</pre>
      </div>

  <p>This example can now be demonstrated by trying the following URLs in
  your web browser: </p>
  <div>
        <pre class="programlisting">http://[host:port]/DAV/xmlt/file1.xml</pre>
      </div>
   <p>and</p>
   <div>
        <pre class="programlisting">http://[host:port]/DAV/xmlt/file1.xml?:ShipperID=3</pre>
      </div>
</div>
<br />

<table border="0" width="90%" id="navbarbottom">
    <tr>
        <td align="left" width="33%">
          <a href="updategrams.html" title="Using UpdateGrams to Modify Data">Previous</a>
          <br />Using UpdateGrams to Modify Data</td>
     <td align="center" width="34%">
          <a href="webandxml.html">Chapter Contents</a>
     </td>
        <td align="right" width="33%">
          <a href="xmlschema.html" title="XML DTD and XML Schemas">Next</a>
          <br />XML DTD and XML Schemas</td>
    </tr>
    </table>
  </div>
  <div id="footer">
    <div>Copyright© 1999 - 2009 OpenLink Software All rights reserved.</div>
   <div id="validation">
    <a href="http://validator.w3.org/check/referer">
        <img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" />
    </a>
    <a href="http://jigsaw.w3.org/css-validator/">
        <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" height="31" width="88" />
    </a>
   </div>
  </div>
 </body>
</html>