<!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 ; " /> <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="xmlschema.html" title="XML DTD and XML Schemas" /> <link rel="next" href="xslttrans.html" title="XSLT Transformation" /> <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 ; " /> <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="xq" /> <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="xmlschema.html" title="XML DTD and XML Schemas">Prev</a> | <a class="link" href="xslttrans.html" title="XSLT Transformation">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> <a href="xmltemplates.html">XML Templates</a> </div> <div> <a href="xmlschema.html">XML DTD and XML Schemas</a> </div> <div class="selected"> <a href="xq.html">XQuery 1.0 Support</a> <div> <a href="#xq_supported_expns" title="Types of XQuery Expressions">Types of XQuery Expressions</a> <a href="#xq_supported_syntax" title="Details of XQuery Syntax">Details of XQuery Syntax</a> <a href="#xq_precompilation" title="Pre-compilation of XPath and XQuery Expressions">Pre-compilation of XPath and XQuery Expressions</a> </div> </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="xq" /> <h2>13.8. XQuery 1.0 Support</h2> <p> The Virtuoso Server provides support for the <a href="http://www.w3.org/TR/xquery/">XQuery 1.0 XML Query Language</a> specification. This specification is currently in the working draft stage at the W3C XML Query Working Group working in collaboration with the W3C XSL Working Group. Both the syntax and semantics of XQuery will probably vary from version to version. </p> <p> In addition to the XQuery 1.0 standard, which describes the language, the <a href="http://www.w3.org/TR/xquery-operators/">XQuery 1.0 and XPath 2.0 Functions and Operators Version 1.0</a> specification describes a set of built-in functions. As with all W3C in-progress efforts, there is a list of open issues detailing problems and unresolved areas; where these affect Virtuoso's implementation, they are noted below. </p> <p> This chapter is not an XQuery textbook and does not replace XQuery-related specifications of W3C. Only Virtuoso-specific extensions and differences are described here. </p> <p> The most important deviation from the standard is that Virtuoso does not provide full type information about data values. As a consequence, "typeswitch" and automatic type conversions are not implemented. </p> <a name="xq_supported_expns" /> <h3>13.8.1. Types of XQuery Expressions</h3> <p> The current draft of XQuery lists 10 groups of XQuery expressions:</p> <ul> <li> <p>Primary expressions, including literals, variable references and function calls.</p> </li> <li> <p>Path expressions, including all XPATH 1.0 expressions and a "pointer operator".</p> </li> <li> <p>Sequence expressions.</p> </li> <li> <p>Arithmetic, comparison and logical operators.</p> </li> <li> <p>Element constructors, which allow you to create new element nodes with specified names, lists of attributes and lists of children.</p> </li> <li> <p>FLWR (FOR-LET-WHERE-RETURN) expressions, which allow you to create variables for intermediate results and to transform sequences of items on a per-item basis.</p> </li> <li> <p>Ordered and unordered expressions to force some sorting of intermediate results or to prevent the XQuery processor from making redundant sorting.</p> </li> <li> <p>Control expressions, such as IF.</p> </li> <li> <p>Quantified expressions: SOME and EVERY.</p> </li> <li> <p>Expressions that test or modify data types.</p> </li> </ul> <p> Not all groups of expressions are implemented. In some groups, not all kinds of clauses are implemented. </p> <p> In addition to the standard, Virtuoso supports special cases of FLWR expressions to deal with XML views: </p> <ul> <li> <p> <a href="xq.html#xq_supported_xmlview">FOR Clause expressions with xmlview() function.</a> </p> </li> </ul> <a name="xq_supported_primary" /> <h4>13.8.1.1. Primary Expressions</h4> <p> XQuery processor uses 32-bit integers on 32-bit platforms and 64-bit integers on 64-bit platforms. Similarly, the scale and precision of floating-point operations may vary from platform to platform. </p> <p> Note that string literals are handled differently in XPath 1.0 and XQuery. "Ben "amp; Jerry"apos;s" denotes the string "Ben " Jerry's" in XQuery and the string "Ben "amp; Jerry"apos;s" in XPath. </p> <br /> <a name="xq_supported_path" /> <h4>13.8.1.2. Path Expressions</h4> <p> Any XPath 1.0 expression is a valid XQuery 1.0 path expression, which the Virtuoso XQuery processor supports. When invoked from the XQuery context, the XPath Processor works in accordance with XSLT rules. There are two major differences between standalone and XQuery/XSLT path expressions. First, the meaning of non-qualified name used as NameTest criterion, as described below. Second, the data type used for attributes varies. In XPath or XQuery mode, if a value is calculated by an <span class="computeroutput">attribute::</span> axis, it is of type <span class="computeroutput">attribute entity</span>; in standalone XPath, the string value of the attribute is used instead. </p> <p> As specified in the XQuery 1.0 standard, a node-set returned by an XPath expression may be used as a sequence of items, where every node of that node-set becomes an item of the sequence. The opposite is not true, however. Not every sequence may be converted into a node-set, even if it is a sequence of nodes. If XPath starts from a function which returns a sequence, an error message "Context node is not an entity" is returned. Fortunately, a variable of type <span class="computeroutput">sequence</span> may be used as a node-set if all items of the sequence are nodes. </p> <p> Obsolete drafts of W3C specification contains description of "pointer operator". Virtuoso continues to support this operator to provide backward compatibility. XQuery processor needs DTD data associated with the XML document in question to distinguish ID attributes from other sorts of attributes and to bookmark elements that have ID locations. For more details, see the description of <a href="xpf_id.html">id()</a> XPATH function. This function uses same DTD data for same purposes, so for any given document, either both <span class="computeroutput">id()</span> and "pointer operator" are applicable or both does not work. </p> <p> Sometimes the "Context node is not an entity" error is signalled if the beginning of the XPath expression is surrounded by parenthesis, even if the expression works fine without these parenthesis. This happens because "(...)" is an "append" operator in XQuery, not just a way to group subexpressions. "append" converts a node-set into a sequence even when it is called with a single argument - that is, without commas inside "(...)". This sequence cannot be used as input for the rest of the XPath expression. </p> <p> As an syntax extension, special notation of QNames is added and can be used, e.g., in NameTest. An expanded name can be surrounded by delimiters (! and !), like (!http://www.example.com:MyTag!) and this syntax allows names that contain otherwise prohibited characters. This syntax is also useful when the text of the query is generated by software. </p> <p> Note that the NameTest that consists of an unqualified name has different meanings in Virtuoso XPath and in XQuery. In XPath, NameTest "sample-tag" means "any element whose local-name is equal to sample-tag". In XQuery, the same test means "any element without namespace-uri whose local-name is equal to sample-tag". </p> <br /> <a name="xq_supported_seq" /> <h4>13.8.1.3. Sequence Expressions</h4> <p>XQuery sequences are supported not only in XQuery but can also be handled in XPath and XSLT. When the XQuery processor is invoked from SQL and a sequence is returned to the caller, the sequence is automatically converted into a vector of its elements.</p> <p>Virtuoso supports all sequence operations listed in current W3C paper plus deprecated operations BEFORE and AFTER.</p> <p> The <span class="computeroutput">sequence concatenation</span> operator is available in XPath and XSLT as the <a href="xpf_append.html">append()</a> function. In addition, the <a href="xpf_tuple.html">tuple()</a> function is available to get the first items of every given argument sequence and return the sequence of these items. </p> <p> XQuery operators UNION, INTERSECT, EXCEPT are available in XPath and XSLT as functions <a href="xpf_union.html">union()</a>, <a href="xpf_intersect.html">intersect()</a> and <a href="xpf_except.html">except()</a>. </p> <br /> <a name="xq_supported_opers" /> <h4>13.8.1.4. Arithmetic, Comparison and Logical operations</h4> <p>Virtuoso shares the implementation of basic arithmetic and comparison operations between XPath, XQuery, XSLT, SQL and Virtuoso/PL processors, so type casting, scale and precision of calculated values are identical across the system. All operators are available in XQuery, in addition, << and >> operators are available in XPath and XSLT as <a href="xpf_is_before.html">is_before()</a> and <a href="xpf_is_after.html">is_after()</a> built-in functions. </p> <br /> <a name="xq_supported_el_ctors" /> <h4>13.8.1.5. Element Constructors</h4> <p> Virtuoso XQuery supports all XQuery 1.0 direct constructors. Previous versions of W3C draft contained the syntax for placing calculated content into the opening tag of direct element constructor, such as <span class="computeroutput"><{concat("calculated-", "element-name")} {concat("calculated-", "attribute-name")}={concat("calculated-", "attribute-value")}>...</> </span>. Thus name of element or attribute, or a value of an attribute can be calculated dynamically. This syntax is still supported. The <a href="xpf_create_element.html"> create-element() </a> XPath function is implemented to make this functionality available in XPath. Additionally, a special function <a href="xpf_create_attribute.html"> create-attribute() </a> may be used to create a new dynamic attribute entity with value and name calculated, this works similarly to xsl:attribute XSLT instruction. </p> <p> Similarly, <a href="xpf_create_comment.html">create-comment()</a>, <a href="xpf_create_element.html">create-element()</a> and <a href="xpf_create_pi.html">create-pi()</a> mimics other XQuery direct constructors in XPath and XSLT. </p> <p> The XQuery specification states that when sequence of atomic values is converted into content of an element constructor, whitespace character is inserted between adjacent values. </p> <p> Unlike previous versions of Virtuoso, current XQuery syntax allows you to use "pure XML notation" inside element constructors. Thus there is no strict need to write 'constant' expression <span class="computeroutput"> <emp empid="12345"><name>John Smith</name><job>Bubble sorter</job></emp> </span> as it is dynamically calculated text, like <span class="computeroutput"> <emp empid="12345"><name>{'John Smith'}</name><job>{'Bubble sorter'}</job></emp> </span> It is still may be useful to write 'constant' expression in the old way. This artificial restriction simplifies finding syntax errors, because there are syntactically wrong expressions that are still correct "pure XML notation." Alternatively, CDATA sections also may be used to make it obvious that the string is a constant, not an expression with forgotten braces around it: <span class="computeroutput"> <emp empid="12345"><name><![CDATA[John Smith]]></name><job><![CDATA[Bubble sorter]]></job></emp> </span> </p> <p> The current version of Virtuoso does not support the new XQuery syntax for dynamic constructors. </p> <br /> <a name="xq_supported_flowr" /> <h4>13.8.1.6. FLWR Expressions</h4> <p> FLWR expressions are fully supported by Virtuoso XQuery. Moreover, <a href="xpf_for.html">for()</a> and <a href="xpf_let.html">let() </a> XPath functions are implemented to make this functionality available in XPath and XSLT. In addition, <a href="xpf_assign.html"> assign() </a> and <a href="xpf_progn.html"> progn() </a> functions are available to deal with extension functions, especially when extension functions are called for their side effects. </p> <p> A special <a href="xpf_xmlview.html">xmlview()</a> function allows very efficient access to SQL data from XML views. </p> <p> Previous XQuery specifications used "sort by" instead of "order by". The difference is that "sort by" was applicable to the final results of the FLWR statement made by RETURN clause whereas "order by" reorders input data for RETURN. Thus, "order by" can sort outputs using data that do not appear in the final result. E.g., an expression can collect items, "order" them by category and title but output only title and price. This was much harder in previous versions of XQuery because it was necessary to prepare an intermediate result that contained title and price and category, then do "sort" by category and title then use one more FLWR expression to form a result that is free from redundant data about category. </p> <p> Nevertheless, Virtuoso supports both "sort by" and "order by", to keep backward compatibility. Moreover, "sort by" operator can be freely used with no relation to any FLWR subexpression. Typical use of such a simplified notation is <span class="computeroutput"> <hit-list>{//track[@rating] sort by (@rating descending)}</hit-list> </span> instead of portable <span class="computeroutput"> <hit-list>{for $t in //track[@rating] order by $t/@rating descending return $t}</hit-list> </span> </p> <br /> <a name="xq_supported_ordered" /> <h4>13.8.1.7. Ordered and Unordered Expressions</h4> <p>The current version of Virtuoso does not use ordered/unordered hints. Everything is calculated ordered. This will change in the future but it is not advisable to place "unordered" hints for future use because there's no way to validate these hints. It is better to place appropriate comments but not hints. </p> <br /> <a name="xq_supported_ctrl" /> <h4>13.8.1.8. Control Expressions</h4> <p> The <a href="xpf_if.html">if()</a> special function mimics the XQuery operator for use in XPath and XSLT. Functions <a href="xpf_and.html">and()</a> and <a href="xpf_or.html">or()</a> are also control expressions because they calculate arguments in strict left-to-right order and may omit the calculation of some results. </p> <br /> <a name="xq_supported_quan" /> <h4>13.8.1.9. Quantified Expressions</h4> <p> Both the <span class="computeroutput">SOME</span> and <span class="computeroutput">EVERY</span> operators are implemented. The <a href="xpf_some.html">some()</a> and <a href="xpf_every.html">every()</a> XPath functions are implemented to make this functionality available in XPath and XSLT. </p> <br /> <a name="xq_supported_metadatatype" /> <h4>13.8.1.10. Expressions That Test or Modify Data types</h4> <p> The operators <span class="computeroutput">IS</span>, <span class="computeroutput">CASTABLE</span>, <span class="computeroutput">CAST</span>, <span class="computeroutput">TREAT</span>, <span class="computeroutput">TYPESWITCH</span> and <span class="computeroutput">VALIDATE</span> are not implemented. </p> <br /> <a name="xq_supported_xmlview" /> <h4>13.8.1.11. FOR Clause Expressions With xmlview() Function</h4> <p> XML views can be queried using FOR Clause from FLWR expressions. The <a href="xpf_xmlview.html"> xmlview() </a> function allows XML views to be accessed as if they were XML documents. XPath expressions beginning with the <span class="computeroutput">xmlview()</span> function will be translated into SQL statements to avoid redundant data access and to avoid creating a whole XML tree.</p> <br /> <br /> <a name="xq_supported_syntax" /> <h3>13.8.2. Details of XQuery Syntax</h3> <p> Virtuoso XQuery uses some syntax extensions. Most visible is an additional notation for qualified names as described above (name is surrounded by "(!...!)" delimiters. An earlier implementation allowed single-line comments started with "#" or "--" continuing to the end of line, this syntax is now obsolete. </p> <p> The "default namespace declaration" clause is not currently supported, to make the text of XQuery unambiguous. If used, default namespaces must extend element names but not attribute names. Extension function names must be extended as they have non-default namespace prefixes but the names of basic functions should not be extended by the default namespace. Finally, Virtuoso will not preserve any information about used namespace prefixes, so default namespaces will be converted into non-default when the resulting XML entity is printed. </p> <br /> <a name="xq_precompilation" /> <h3>13.8.3. Pre-compilation of XPath and XQuery Expressions</h3> <p>Virtuoso compiles XPath and XQuery expressions as early as it is possible. E.g. if the first argument of <a href="fn_xquery_eval.html"> xquery_eval() </a> is a string constant then the SQL compiler will invoke the XQuery compiler to avoid on-demand compilation(s) of this text. </p> <p> This feature significantly enhances performance of XQuery expressions embedded in SQL. For a simple search on XML document of average size the compilation time can be three times greater than execution time. In addition, the use of <a href="xpf__sql__column.html"> sql:column() </a> special XQuery function is possible only when pre-compilation can be done by SQL compiler. </p> <p> Pre-compilation is impossible if the text of the expression is not a constant. The typical case is passing an XQuery expression as parameter to a function. In this case the expression is compiled during the call of xquery_eval() and stored for future use. If the same string is passed again to the same invocation of xquery_eval() then a stored compiled expression is used. </p> <p> Only partial pre-compilation is possible if XQuery expression refers to not-yet defined extension functions or to external resources. Partial pre-compilation gives little gain in speed, but it allows the use of <a href="xpf__sql__column.html"> sql:column() </a> </p> <p> The most important fact about pre-compilation is that passing parameters into XQuery statement is much more efficient than printing then into the text of the query. This is similar to SQL queries. </p> <a name="ex_xq_precompilation" /> <div class="example"> <div class="exampletitle">Good and Poor Coding Practices</div> <p> <strong>GOOD</strong> The expression is compiled once when SQL query is compiled:</p> <div> <pre class="programlisting"> select xquery_eval('count(//abstract)', SOURCE_XML) from LIB..ARTICLES; </pre> </div> <p> <strong>GOOD</strong> The expression is compiled once when SQL query is compiled:</p> <div> <pre class="programlisting"> select xquery_eval('count(//article[@id=$main_id]/abstract)', SOURCE_XML, 1, vector('main_id', MAIN_ID)) from LIB..ARTICLES; </pre> </div> <p> <strong>POOR</strong> The expression is compiled once per data row. In addition, a hard-to-find error will occur if a value of MAIN_ID may contain double quote or a backslash character. </p> <div> <pre class="programlisting"> select xquery_eval(sprintf('count(//article[@id="%s"]/abstract)', MAIN_ID), SOURCE_XML) from LIB..ARTICLES; </pre> </div> <p> <strong>GOOD</strong> The XQuery expression is compiled once per execution of the SQL query. The SQL compiler pays special attention to queries that import external resources, because the content and availability of these resources may differ from call to call. In addition, importing an external resource is usually not possible during SQL compilation due to deadlock danger, so the compilation is postponed until run time, but this is not too bad anyway. Even in this sophisticated case, XQuery can contain calls of <a href="xpf__sql__column.html"> sql:column() </a> </p>. <div> <pre class="programlisting"> select xquery_eval(' namespace tools="http://www.example.com/lib/tools/" import define "http://www.example.com/lib/tools/common.xqr" tools:extract-keywords(//abstract)', SOURCE_XML) from LIB..ARTICLES; </pre> </div> <p> <strong>GOOD</strong> Two XQuery expressions are compiled during SQL compilation.</p> <div> <pre class="programlisting"> select case when SOURCE_IS_DOCBOOK then xquery_eval ('//formalpara[title="See Also"]/para', SOURCE_XML) else xquery_eval ('//p[@style="seealso"]', SOURCE_XML) end from LIB..ARTICLES; </pre> </div> <p> <strong>POOR</strong> Virtuoso can not pre-compile XQquery expressions. Moreover, only one precompiled expression is cached per occurrence of xquery_eval() in the SQL statement so it is possible that an XQuery compiler will start once per data row.</p> <div> <pre class="programlisting"> select xquery_eval ( case when SOURCE_IS_DOCBOOK then '//formalpara[title="See Also"]/para' else '//p[@style="seealso"]' end, SOURCE_XML) from LIB..ARTICLES; </pre> </div> </div> <br /> <table border="0" width="90%" id="navbarbottom"> <tr> <td align="left" width="33%"> <a href="xmlschema.html" title="XML DTD and XML Schemas">Previous</a> <br />XML DTD and XML Schemas</td> <td align="center" width="34%"> <a href="webandxml.html">Chapter Contents</a> </td> <td align="right" width="33%"> <a href="xslttrans.html" title="XSLT Transformation">Next</a> <br />XSLT Transformation</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>