<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="extensions" page="functions" subpage="query"/>
      <!--
           Generated at 2011-12-09T20:47:22.916Z--><title>Saxonica: XSLT and XQuery Processing: saxon:query()</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title" content="Saxonica: XSLT and XQuery Processing: saxon:query()"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>saxon:query()</h1>
      <p><b>saxon:query($query as jt:net.sf.saxon.query.XQueryExpression?) ==&gt; item()*</b></p>
      <p><b>saxon:query($query as jt:net.sf.saxon.query.XQueryExpression?, $contextItem as item()?) ==&gt; item()*</b></p>
      <p><b>saxon:query($query as jt:net.sf.saxon.query.XQueryExpression?, $contextItem as item()?, $params as node()*) ==&gt; item()*</b></p>
      <p>This function takes as input a compiled XQuery query, and runs the query, returning the result of evaluating
the query. The first argument will generally be the result of calling the 
<a class="bodylink" href="../../extensions/functions/compile-query.xml">saxon:compile-query()</a> extension function.</p>
      <p>If the first argument is an empty sequence, the result is an empty sequence.</p>
      <p>If only one argument is supplied, the context item for evaluating the query will be the same as the
context item in the environment where the function is called, that is, the implicit second argument is ".".
If there is no context item, however, no failure occurs unless the query attempts to reference the context item.</p>
      <p>If the second argument is present it
can be any item, which is used as the context item for the query. It can also be the empty sequence, in which case
the query runs with no context item.</p>
      <p>If the optional third argument is present, it is used to supply parameters (external variables) to the query. The
value is a sequence of nodes. Each node must be an element node, attribute node, or document node; supplying a document
node is equivalent to supplying all its element children. The name of the node must match an external variable name
declared in the query prolog, and the atomized value of the node is used
as the value of the parameter. If this is <code>untypedAtomic</code> then it is converted to the required type declared
in the query.</p>
      <p>The function is available both in XQuery and in XSLT.</p>
      <p>The compiled stylesheet can be used repeatedly with different inputs.</p>
      <p>Here is an example of how to use the function from XQuery:</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>
declare namespace saxon = "http://saxon.sf.net/";

&lt;out&gt;{
let $q1 := "declare variable $x external; declare variable $y external; &lt;z&gt;{$x + $y + .}&lt;/z&gt;"
return saxon:query(saxon:compile-query($q1), 4, (&lt;x&gt;3&lt;/x&gt;, &lt;y&gt;2&lt;/y&gt;))
}&lt;/out&gt;
</code>
         </pre>
      </div>
      <p>The result of the query is a sequence containing the single integer 9.</p>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="result-document.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>