/usr/share/qt5/doc/qtxmlpatterns/xmlprocessing.html is in qtxmlpatterns5-doc-html 5.3.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- xml-patterns.qdoc -->
<title>XQuery | QtXmlPatterns </title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.3</li>
<li><a href="qtxmlpatterns-index.html">Qt XML Patterns</a></li>
<li>XQuery</li>
<li id="buildversion">
Qt 5.3.2 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<link rel="prev" href="Working with the DOM Tree" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="Working with the DOM Tree">Working with the DOM Tree</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#introduction">Introduction</a></li>
<li class="level1"><a href="#advantages-of-using-qt-xml-patterns-and-xquery">Advantages of using Qt XML Patterns and XQuery</a></li>
<li class="level1"><a href="#using-the-qt-xml-patterns-module">Using the Qt XML Patterns module</a></li>
<li class="level2"><a href="#running-the-query-engine-from-your-qt-application">Running the query engine from your Qt application</a></li>
<li class="level2"><a href="#running-the-query-engine-from-the-command-line-utility">Running the query engine from the command line utility</a></li>
<li class="level1"><a href="#the-xquery-data-model">The XQuery Data Model</a></li>
<li class="level2"><a href="#binding-program-variables-to-xquery-variables">Binding program variables to XQuery variables</a></li>
<li class="level3"><a href="#binding-to-qvariant-time">Binding To QVariant::Time</a></li>
<li class="level3"><a href="#binding-to-qvariantlist">Binding To QVariantList</a></li>
<li class="level2"><a href="#interpreting-xquery-results">Interpreting XQuery results</a></li>
<li class="level3"><a href="#handling-qxmlnames">Handling QXmlNames</a></li>
<li class="level3"><a href="#no-mapping-for-xs-time">No mapping for xs:time</a></li>
<li class="level1"><a href="#using-xquery-with-non-xml-data">Using XQuery with Non-XML Data</a></li>
<li class="level1"><a href="#more-on-using-qt-xml-patterns-with-non-xml-data">More on using Qt XML Patterns with non-XML Data</a></li>
<li class="level1"><a href="#security-considerations">Security Considerations</a></li>
<li class="level2"><a href="#code-injection">Code Injection</a></li>
<li class="level2"><a href="#denial-of-service-attacks">Denial of Service Attacks</a></li>
<li class="level1"><a href="#features-and-conformance">Features and Conformance</a></li>
<li class="level2"><a href="#xquery-1-0">XQuery 1.0</a></li>
<li class="level2"><a href="#xslt-2-0">XSLT 2.0</a></li>
<li class="level2"><a href="#xpath-2-0">XPath 2.0</a></li>
<li class="level2"><a href="#xml-id">xml:id</a></li>
<li class="level2"><a href="#xml-schema-1-0">XML Schema 1.0</a></li>
<li class="level2"><a href="#resource-loading">Resource Loading</a></li>
<li class="level2"><a href="#xml">XML</a></li>
</ul>
</div>
<h1 class="title">XQuery</h1>
<span class="subtitle"></span>
<!-- $$$xmlprocessing.html-description -->
<div class="descr"> <a name="details"></a>
<a name="patternist"></a><a name="introduction"></a>
<h2>Introduction</h2>
<p>XQuery is a language for traversing XML documents to select and aggregate items of interest and to transform them for output as XML or some other format. XPath is the <i>element selection</i> part of XQuery.</p>
<p>The Qt XML Patterns module supports using <a href="http://www.w3.org/TR/xquery">XQuery 1.0</a> and <a href="http://www.w3.org/TR/xpath20">XPath 2.0</a> in Qt applications, for querying XML data <i>and</i> for querying <a href="qabstractxmlnodemodel.html">non-XML data that can be modeled to look like XML</a>. Readers who are not familiar with the XQuery/XPath language can read <a href="xquery-introduction.html">A Short Path to XQuery</a> for a brief introduction.</p>
<a name="advantages-of-using-qt-xml-patterns-and-xquery"></a>
<h2>Advantages of using Qt XML Patterns and XQuery</h2>
<p>The XQuery/XPath language simplifies data searching and transformation tasks by eliminating the need for doing a lot of C++ or Java procedural programming for each new query task. Here is an XQuery that constructs a bibliography of the contents of a library:</p>
<a name="qtxmlpatterns-example-query"></a><pre class="cpp"><bibliography>
{doc("library.xml")/bib/book[publisher="Addison-Wesley" and @year>1991]/<book year="{@year}">{title}</book>}
</bibliography></pre>
<p>First, the query opens a <tt><bibliography></tt> element in the output. The <a href="xquery-introduction.html#using-path-expressions-to-match-and-select-items">embedded path expression</a> then loads the XML document describing the contents of the library (<tt>library.xml</tt>) and begins the search. For each <tt><book></tt> element it finds, where the publisher was Addison-Wesley and the publication year was after 1991, it creates a new <tt><book></tt> element in the output as a child of the open <tt><bibliography></tt> element. Each new <tt><book></tt> element gets the book's title as its contents and the book's publication year as an attribute. Finally, the <tt><bibliography></tt> element is closed.</p>
<p>The advantages of using Qt XML Patterns and XQuery in your Qt programs are summarized as follows:</p>
<ul>
<li><b>Ease of development</b>: All the C++ programming required to perform data query tasks can be replaced by a simple XQuery like the example above.</li>
<li><b>Comprehensive functionality</b>: The <a href="http://www.w3.org/TR/xquery/#id-expressions">expression syntax</a> and rich set of <a href="http://www.w3.org/TR/xpath-functions">functions and operators</a> provided by XQuery are sufficient for performing any data searching, selecting, and sorting tasks.</li>
<li><b>Conformance to standards</b>: Conformance to all applicable XML and XQuery standards ensures that Qt XML Patterns can always process XML documents generated by other conformant applications, and that XML documents created with Qt XML Patterns can be processed by other conformant applications.</li>
<li><b>Maximal flexibility</b> The Qt XML Patterns module can be used to query XML data <i>and</i> non-XML data that can be <a href="qabstractxmlnodemodel.html">modeled to look like XML</a>.</li>
</ul>
<a name="using-the-qt-xml-patterns-module"></a>
<h2>Using the Qt XML Patterns module</h2>
<p>There are two ways Qt XML Patterns can be used to evaluate queries. You can run the query engine in your Qt application using the Qt XML Patterns C++ API, or you can run the query engine from the command line using Qt's <tt>xmlpatterns</tt> command line utility.</p>
<a name="running-the-query-engine-from-your-qt-application"></a>
<h3>Running the query engine from your Qt application</h3>
<p>If we save the example XQuery shown above in a text file (e.g. <tt>myquery.xq</tt>), we can run it from a Qt application using a standard Qt XML Patterns code sequence:</p>
<pre class="cpp"> <span class="type">QFile</span> xq(<span class="string">"myquery.xq"</span>);
<span class="type"><a href="qxmlquery.html">QXmlQuery</a></span> query;
query<span class="operator">.</span>setQuery(<span class="operator">&</span>xq<span class="operator">,</span> <span class="type">QUrl</span><span class="operator">::</span>fromLocalFile(xq<span class="operator">.</span>fileName()));
<span class="type"><a href="qxmlserializer.html">QXmlSerializer</a></span> serializer(query<span class="operator">,</span> myOutputDevice);
query<span class="operator">.</span>evaluateTo(<span class="operator">&</span>serializer);</pre>
<p>First construct a QFile for the text file containing the XQuery (<tt>myquery.xq</tt>). Then create an instance of <a href="qxmlquery.html">QXmlQuery</a> and call its <a href="qxmlquery.html#setQuery">setQuery()</a> function to load and parse the XQuery file. Then create an <a href="qxmlserializer.html">XML serializer</a> to output the query's result set as unformatted XML. Finally, call the <a href="qxmlquery.html#evaluateTo">evaluateTo()</a> function to evaluate the query and serialize the results as XML.</p>
<p><b>Note: </b>If you compile Qt yourself, the Qt XML Patterns module will <i>not</i> be built if exceptions are disabled, or if you compile Qt with a compiler that doesn't support member templates, e.g., MSVC 6.</p><p>See the <a href="qxmlquery.html">QXmlQuery</a> documentation for more information about the Qt XML Patterns C++ API.</p>
<a name="running-the-query-engine-from-the-command-line-utility"></a>
<h3>Running the query engine from the command line utility</h3>
<p><i>xmlpatterns</i> is a command line utility for running XQueries. It expects the name of a file containing the XQuery text.</p>
<pre class="cpp">xmlpatterns myQuery<span class="operator">.</span>xq</pre>
<p>The XQuery in <tt>myQuery.xq</tt> will be evaluated and its output written to <tt>stdout</tt>. Pass the <tt>-help</tt> switch to get the list of input flags and their meanings.</p>
<p>xmlpatterns can be used in scripting. However, the descriptions and messages it outputs were not meant to be parsed and may be changed in future releases of Qt.</p>
<a name="qtxdm"></a><a name="the-xquery-data-model"></a>
<h2>The XQuery Data Model</h2>
<p>XQuery represents data items as <i>atomic values</i> or <i>nodes</i>. An atomic value is a value in the domain of one of the <a href="http://www.w3.org/TR/xmlschema-2/#built-in-datatypes">built-in datatypes</a> defined in <a href="http://www.w3.org/TR/xmlschema-2">Part 2</a> of the W3C XML Schema. A node is normally an XML element or attribute, but when non-XML data is <a href="qabstractxmlnodemodel.html">modeled to look like XML</a>, a node can also represent a non-XML data items.</p>
<p>When you run an XQuery using the C++ API in a Qt application, you will often want to bind program variables to $variables in the XQuery. After the query is evaluated, you will want to interpret the sequence of data items in the result set.</p>
<a name="binding-program-variables-to-xquery-variables"></a>
<h3>Binding program variables to XQuery variables</h3>
<p>When you want to run a parameterized XQuery from your Qt application, you will need to <a href="qxmlquery.html#bindVariable">bind variables</a> in your program to $name variables in your XQuery.</p>
<p>Suppose you want to parameterize the bibliography XQuery in the example above. You could define variables for the catalog that contains the library (<tt>$file</tt>), the publisher name (<tt>$publisher</tt>), and the year of publication (<tt>$year</tt>):</p>
<a name="qtxmlpatterns-example-query2"></a><pre class="cpp"><bibliography>
{
doc($file)/bib/book[publisher = $publisher and @year > $year]/<book year="{@year}">{title}</book>
}
</bibliography></pre>
<p>Modify the Qt XML Patterns code to use one of the <a href="qxmlquery.html#bindVariable">bindVariable()</a> functions to bind a program variable to each XQuery $variable:</p>
<pre class="cpp"> <span class="type">QFile</span> xq(<span class="string">"myquery.xq"</span>);
<span class="type">QString</span> fileName(<span class="string">"the filename"</span>);
<span class="type">QString</span> publisherName(<span class="string">"the publisher"</span>);
<span class="type">qlonglong</span> year <span class="operator">=</span> <span class="number">1234</span>;
<span class="type"><a href="qxmlquery.html">QXmlQuery</a></span> query;
query<span class="operator">.</span>bindVariable(<span class="string">"file"</span><span class="operator">,</span> <span class="type">QVariant</span>(fileName));
query<span class="operator">.</span>bindVariable(<span class="string">"publisher"</span><span class="operator">,</span> <span class="type">QVariant</span>(publisherName));
query<span class="operator">.</span>bindVariable(<span class="string">"year"</span><span class="operator">,</span> <span class="type">QVariant</span>(year));
query<span class="operator">.</span>setQuery(<span class="operator">&</span>xq<span class="operator">,</span> <span class="type">QUrl</span><span class="operator">::</span>fromLocalFile(xq<span class="operator">.</span>fileName()));
<span class="type"><a href="qxmlserializer.html">QXmlSerializer</a></span> serializer(query<span class="operator">,</span> myOutputDevice);
query<span class="operator">.</span>evaluateTo(<span class="operator">&</span>serializer);</pre>
<p>Each program variable is passed to Qt XML Patterns as a QVariant of the type of the C++ variable or constant from which it is constructed. Note that Qt XML Patterns assumes that the type of the QVariant in the bindVariable() call is the correct type, so the $variable it is bound to must be used in the XQuery accordingly. The following table shows how QVariant types are mapped to XQuery $variable types:</p>
<table class="generic">
<thead><tr class="qt-style"><th >QVariant type</th><th >XQuery $variable type</th></tr></thead>
<tr valign="top" class="odd"><td >QVariant::LongLong</td><td ><tt>xs:integer</tt></td></tr>
<tr valign="top" class="even"><td >QVariant::Int</td><td ><tt>xs:integer</tt></td></tr>
<tr valign="top" class="odd"><td >QVariant::UInt</td><td ><tt>xs:nonNegativeInteger</tt></td></tr>
<tr valign="top" class="even"><td >QVariant::ULongLong</td><td ><tt>xs:unsignedLong</tt></td></tr>
<tr valign="top" class="odd"><td >QVariant::String</td><td ><tt>xs:string</tt></td></tr>
<tr valign="top" class="even"><td >QVariant::Double</td><td ><tt>xs:double</tt></td></tr>
<tr valign="top" class="odd"><td >QVariant::Bool</td><td ><tt>xs:boolean</tt></td></tr>
<tr valign="top" class="even"><td >QVariant::Double</td><td ><tt>xs:decimal</tt></td></tr>
<tr valign="top" class="odd"><td >QVariant::ByteArray</td><td ><tt>xs:base64Binary</tt></td></tr>
<tr valign="top" class="even"><td >QVariant::StringList</td><td ><tt>xs:string*</tt></td></tr>
<tr valign="top" class="odd"><td >QVariant::Url</td><td ><tt>xs:string</tt></td></tr>
<tr valign="top" class="even"><td >QVariant::Date</td><td ><tt>xs:date</tt>.</td></tr>
<tr valign="top" class="odd"><td >QVariant::DateTime</td><td ><tt>xs:dateTime</tt></td></tr>
<tr valign="top" class="even"><td >QVariant::Time.</td><td ><tt>xs:time</tt>. (see <a href="#binding-to-time">Binding To QVariant::Time</a> below)</td></tr>
<tr valign="top" class="odd"><td >QVariantList</td><td >(see <a href="#binding-to-qvariantlist">Binding To QVariantList</a> below)</td></tr>
</table>
<p>A type not shown in the table is not supported and will cause undefined XQuery behavior or a $variable binding error, depending on the context in the XQuery where the variable is used.</p>
<a name="binding-to-time"></a><a name="binding-to-qvariant-time"></a>
<h4>Binding To QVariant::Time</h4>
<p>Because the instance of QTime used in QVariant::Time does not include a zone offset, an instance of QVariant::Time should not be bound to an XQuery variable of type <tt>xs:time</tt>, unless the QTime is UTC. When binding a non-UTC QTime to an XQuery variable, it should first be passed as a string, or converted to a QDateTime with an arbitrary date, and then bound to an XQuery variable of type <tt>xs:dateTime</tt>.</p>
<a name="binding-to-qvariantlist"></a><a name="binding-to-qvariantlist"></a>
<h4>Binding To QVariantList</h4>
<p>A QVariantList can be bound to an XQuery $variable. All the QVariants in the list must be of the same atomic type, and the $variable the variant list is bound to must be of that same atomic type. If the QVariants in the list are not all of the same atomic type, the XQuery behavior is undefined.</p>
<a name="interpreting-xquery-results"></a>
<h3>Interpreting XQuery results</h3>
<p>When the results of an XQuery are returned in a sequence of <a href="qxmlresultitems.html">result items</a>, atomic values in the sequence are treated as instances of QVariant. Suppose that instead of serializing the results of the XQuery as XML, we process the results programatically. Modify the standard Qt XML Patterns code sequence to call the overload of <a href="qxmlquery.html#evaluateTo">QXmlQuery::evaluateTo</a>() that populates a sequence of <a href="qxmlresultitems.html">result items</a> with the XQuery results:</p>
<pre class="cpp"> <span class="type">QFile</span> xq(<span class="string">"myquery.xq"</span>);
<span class="type">QString</span> fileName(<span class="string">"the filename"</span>);
<span class="type">QString</span> publisherName(<span class="string">"the publisher"</span>);
<span class="type">qlonglong</span> year <span class="operator">=</span> <span class="number">1234</span>;
<span class="type"><a href="qxmlquery.html">QXmlQuery</a></span> query;
query<span class="operator">.</span>bindVariable(<span class="string">"file"</span><span class="operator">,</span> <span class="type">QVariant</span>(fileName));
query<span class="operator">.</span>bindVariable(<span class="string">"publisher"</span><span class="operator">,</span> <span class="type">QVariant</span>(publisherName));
query<span class="operator">.</span>bindVariable(<span class="string">"year"</span><span class="operator">,</span> <span class="type">QVariant</span>(year));
query<span class="operator">.</span>setQuery(<span class="operator">&</span>xq<span class="operator">,</span> <span class="type">QUrl</span><span class="operator">::</span>fromLocalFile(xq<span class="operator">.</span>fileName()));
<span class="type"><a href="qxmlresultitems.html">QXmlResultItems</a></span> result;
query<span class="operator">.</span>evaluateTo(<span class="operator">&</span>result);
<span class="type"><a href="qxmlitem.html">QXmlItem</a></span> item(result<span class="operator">.</span>next());
<span class="keyword">while</span> (<span class="operator">!</span>item<span class="operator">.</span>isNull()) {
<span class="keyword">if</span> (item<span class="operator">.</span>isAtomicValue()) {
<span class="type">QVariant</span> v <span class="operator">=</span> item<span class="operator">.</span>toAtomicValue();
<span class="keyword">switch</span> (v<span class="operator">.</span>type()) {
<span class="keyword">case</span> <span class="type">QVariant</span><span class="operator">::</span>LongLong:
<span class="comment">// xs:integer</span>
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="type">QVariant</span><span class="operator">::</span>String:
<span class="comment">// xs:string</span>
<span class="keyword">break</span>;
<span class="keyword">default</span>:
<span class="comment">// error</span>
<span class="keyword">break</span>;
}
}
<span class="keyword">else</span> <span class="keyword">if</span> (item<span class="operator">.</span>isNode()) {
<span class="preprocessor">#ifdef qdoc</span>
<span class="type"><a href="qxmlnodemodelindex.html">QXmlNodeModelIndex</a></span> i <span class="operator">=</span> item<span class="operator">.</span>toNodeModelIndex();
<span class="comment">// process node</span>
<span class="preprocessor">#endif // qdoc</span>
}
item <span class="operator">=</span> result<span class="operator">.</span>next();
}</pre>
<p>Iterate through the <a href="qxmlresultitems.html">result items</a> and test each <a href="qxmlitem.html">QXmlItem</a> to see if it is an atomic value or a node. If it is an atomic value, convert it to a QVariant with <a href="qxmlitem.html#toAtomicValue">toAtomicValue()</a> and switch on its variant type to handle all the atomic values your XQuery might return. The following table shows the QVariant type to expect for each atomic value type (or <a href="qxmlname.html">QXmlName</a>):</p>
<table class="generic">
<thead><tr class="qt-style"><th >XQuery result item type</th><th >QVariant type returned</th></tr></thead>
<tr valign="top" class="odd"><td ><tt>xs:QName</tt></td><td ><a href="qxmlname.html">QXmlName</a> (see <a href="#handling-qxmlnames">Handling QXmlNames</a> below)</td></tr>
<tr valign="top" class="even"><td ><tt>xs:integer</tt></td><td >QVariant::LongLong</td></tr>
<tr valign="top" class="odd"><td ><tt>xs:string</tt></td><td >QVariant::String</td></tr>
<tr valign="top" class="even"><td ><tt>xs:string*</tt></td><td >QVariant::StringList</td></tr>
<tr valign="top" class="odd"><td ><tt>xs:double</tt></td><td >QVariant::Double</td></tr>
<tr valign="top" class="even"><td ><tt>xs:float</tt></td><td >QVariant::Double</td></tr>
<tr valign="top" class="odd"><td ><tt>xs:boolean</tt></td><td >QVariant::Bool</td></tr>
<tr valign="top" class="even"><td ><tt>xs:decimal</tt></td><td >QVariant::Double</td></tr>
<tr valign="top" class="odd"><td ><tt>xs:hexBinary</tt></td><td >QVariant::ByteArray</td></tr>
<tr valign="top" class="even"><td ><tt>xs:base64Binary</tt></td><td >QVariant::ByteArray</td></tr>
<tr valign="top" class="odd"><td ><tt>xs:gYear</tt></td><td >QVariant::DateTime</td></tr>
<tr valign="top" class="even"><td ><tt>xs:gYearMonth</tt></td><td >QVariant::DateTime</td></tr>
<tr valign="top" class="odd"><td ><tt>xs:gMonthDay</tt></td><td >QVariant::DateTime</td></tr>
<tr valign="top" class="even"><td ><tt>xs:gDay</tt></td><td >QVariant::DateTime</td></tr>
<tr valign="top" class="odd"><td ><tt>xs:gMonth</tt></td><td >QVariant::DateTime</td></tr>
<tr valign="top" class="even"><td ><tt>xs:anyURI</tt></td><td >QVariant::Url</td></tr>
<tr valign="top" class="odd"><td ><tt>xs:untypedAtomic</tt></td><td >QVariant::String</td></tr>
<tr valign="top" class="even"><td ><tt>xs:ENTITY</tt></td><td >QVariant::String</td></tr>
<tr valign="top" class="odd"><td ><tt>xs:date</tt></td><td >QVariant::DateTime</td></tr>
<tr valign="top" class="even"><td ><tt>xs:dateTime</tt></td><td >QVariant::DateTime</td></tr>
<tr valign="top" class="odd"><td ><tt>xs:time</tt></td><td >(see <a href="#xstime-not-mapped">No mapping for xs:time</a> below)</td></tr>
</table>
<a name="handling-qxmlnames"></a><a name="handling-qxmlnames"></a>
<h4>Handling QXmlNames</h4>
<p>If your XQuery can return atomic value items of type <tt>xs:QName</tt>, they will appear in your <a href="qxmlresultitems.html">QXmlResultItems</a> as instances of <a href="qxmlname.html">QXmlName</a>. Since the QVariant class does not support the <a href="qxmlname.html">QXmlName</a> class directly, extracting them from <a href="qxmlresultitems.html">QXmlResultItems</a> requires a bit of slight-of-hand using the Qt metatype system. We must modify our example to use a couple of template functions, a friend of QMetaType (qMetaTypeId<T>()) and a friend of QVariant (qvariant_cast<T>()):</p>
<pre class="cpp"> <span class="type">QFile</span> xq(<span class="string">"myquery.xq"</span>);
<span class="type"><a href="qxmlquery.html">QXmlQuery</a></span> query;
query<span class="operator">.</span>setQuery(<span class="operator">&</span>xq<span class="operator">,</span> <span class="type">QUrl</span><span class="operator">::</span>fromLocalFile(xq<span class="operator">.</span>fileName()));
<span class="type"><a href="qxmlresultitems.html">QXmlResultItems</a></span> result;
query<span class="operator">.</span>evaluateTo(<span class="operator">&</span>result);
<span class="type"><a href="qxmlitem.html">QXmlItem</a></span> item(result<span class="operator">.</span>next());
<span class="keyword">while</span> (<span class="operator">!</span>item<span class="operator">.</span>isNull()) {
<span class="keyword">if</span> (item<span class="operator">.</span>isAtomicValue()) {
<span class="type">QVariant</span> v <span class="operator">=</span> item<span class="operator">.</span>toAtomicValue();
<span class="keyword">switch</span> (v<span class="operator">.</span>type()) {
<span class="keyword">case</span> <span class="type">QVariant</span><span class="operator">::</span>LongLong:
<span class="comment">// xs:integer</span>
<span class="keyword">break</span>;
<span class="keyword">case</span> <span class="type">QVariant</span><span class="operator">::</span>String:
<span class="comment">// xs:string</span>
<span class="keyword">break</span>;
<span class="keyword">default</span>:
<span class="keyword">if</span> (v<span class="operator">.</span>userType() <span class="operator">=</span><span class="operator">=</span> qMetaTypeId<span class="operator"><</span><span class="type"><a href="qxmlname.html">QXmlName</a></span><span class="operator">></span>()) {
<span class="preprocessor">#ifdef qdoc</span>
<span class="type"><a href="qxmlname.html">QXmlName</a></span> n <span class="operator">=</span> qvariant_cast<span class="operator"><</span><span class="type"><a href="qxmlname.html">QXmlName</a></span><span class="operator">></span>(v);
<span class="comment">// process QXmlName n...</span>
<span class="preprocessor">#endif // qdoc</span>
}
<span class="keyword">else</span> {
<span class="comment">// error</span>
}
<span class="keyword">break</span>;
}
}
<span class="keyword">else</span> <span class="keyword">if</span> (item<span class="operator">.</span>isNode()) {
<span class="preprocessor">#ifdef qdoc</span>
<span class="type"><a href="qxmlnodemodelindex.html">QXmlNodeModelIndex</a></span> i <span class="operator">=</span> item<span class="operator">.</span>toNodeModelIndex();
<span class="comment">// process node</span>
<span class="preprocessor">#endif // qdoc</span>
}
item <span class="operator">=</span> result<span class="operator">.</span>next();
}</pre>
<p>To access the strings in a <a href="qxmlname.html">QXmlName</a> returned by an <a href="qxmlquery.html#evaluateTo">XQuery evaluation</a>, the <a href="qxmlname.html">QXmlName</a> must be accessed with the <a href="qxmlnamepool.html">name pool</a> from the instance of <a href="qxmlquery.html">QXmlQuery</a> that was used for the evaluation.</p>
<a name="xstime-not-mapped"></a><a name="no-mapping-for-xs-time"></a>
<h4>No mapping for xs:time</h4>
<p>An instance of <tt>xs:time</tt> can't be represented correctly as an instance of QVariant::Time, unless the <tt>xs:time</tt> is a UTC time. This is because xs:time has a zone offset (0 for UTC) in addition to the time value, which the QTime in QVariant::Time does not have. This means that if an XQuery tries to return an atomic value of type <tt>xs:time</tt>, an invalid QVariant will be returned. A query can return an atomic value of type xs:time by either converting it to an <tt>xs:dateTime</tt> with an arbitrary date, or to an <tt>xs:string</tt>.</p>
<a name="using-xquery-with-non-xml-data"></a>
<h2>Using XQuery with Non-XML Data</h2>
<p>Although the XQuery language was designed for querying XML, with Qt XML Patterns one can use XQuery for querying any data that can be modeled to look like XML. Non-XML data is modeled to look like XML by loading it into a custom subclass of <a href="qabstractxmlnodemodel.html">QAbstractXmlNodeModel</a>, where it is then presented to the Qt XML Patterns XQuery engine via the same API the XQuery engine uses for querying XML.</p>
<p>When Qt XML Patterns loads and queries XML files and produces XML output, it can always load the XML data into its default XML node model, where it can be traversed efficiently. The XQuery below traverses the product orders found in the XML file <i>myOrders.xml</i> to find all the skin care product orders and output them ordered by shipping date.</p>
<pre class="cpp"><result>
<para>The following skin care products have shipped, ordered by shipping date(oldest first):</para>
{
for $i in doc("myOrders.xml")/orders/order[@product = "Acme Skin Care"]
order by xs:date($i/@shippingDate) descending
return $i
}
</result></pre>
<p>Qt XML Patterns can be used out of the box to perform this query, provided <i>myOrders.xml</i> actually contains well-formed XML. It can be loaded directly into the default XML node model and traversed. But suppose we want Qt XML Patterns to perform queries on the hierarchical structure of the local file system. The default XML node model in Qt XML Patterns is not suitable for navigating the file system, because there is no XML file to load that contains a description of it. Such an XML file, if it existed, might look something like this:</p>
<pre class="cpp"><?xml version="1.0" encoding="UTF-8"?>
<directory name="home">
<file name="myNote.txt" mimetype="text/plain" size="8" extension="txt" uri="file:///home/frans/myNote.txt">
<content asBase64Binary="TXkgTm90ZSE=" asStringFromUTF-8="My Note!"/>
</file>
<directory name="src">
...
</directory>
...
</directory></pre>
<p>The <a href="qtxmlpatterns-xmlpatterns-filetree-example.html">File System Example</a> does exactly this.</p>
<p>There is no such file to load into the default XML node model, but one can write a subclass of <a href="qabstractxmlnodemodel.html">QAbstractXmlNodeModel</a> to represent the file system. This custom XML node model, once populated with all the directory and file descriptors obtained directly from the system, presents the complete file system hierarchy to the query engine via the same API used by the default XML node model to present the contents of an XML file. In other words, once the custom XML node model is populated, it presents the file system to the query engine as if a description of it had been loaded into the default XML node model from an XML file like the one shown above.</p>
<p>Now we can write an XQuery to find all the XML files and parse them to find the ones that don't contain well-formed XML.</p>
<pre class="qml"><html>
<body>
{
$myRoot//file[@mimetype = 'text/xml' or @mimetype = 'application/xml']
/
(if(doc-available(@uri))
then ()
else <p>Failed to parse file {@uri}.</p>)
}
</body>
</html></pre>
<p>Without Qt XML Patterns, there is no simple way to solve this kind of problem. You might do it by writing a C++ program to traverse the file system, sniff out all the XML files, and submit each one to an XML parser to test that it contains valid XML. The C++ code required to write that program will probably be more complex than the C++ code required to subclass <a href="qabstractxmlnodemodel.html">QAbstractXmlNodeModel</a>, but even if the two are comparable, your custom C++ program can be used only for that one task, while your custom XML node model can be used by any XQuery that must navigate the file system.</p>
<p>The general approach to using XQuery to perform queries on non-XML data has been a three step process. In the first step, the data is loaded into a non-XML data model. In the second step, the non-XML data model is serialized as XML and output to XML (text) files. In the final step, an XML tool loads the XML files into a second, XML data model, where the XQueries can be performed. The development cost of implementing this process is often high, and the three step system that results is inefficient because the two data models must be built and maintained separately.</p>
<p>With Qt XML Patterns, subclassing <a href="qabstractxmlnodemodel.html">QAbstractXmlNodeModel</a> eliminates the transformation required to convert the non-XML data model to the XML data model, because there is only ever one data model required. The non-XML data model presents the non-XML data to the query engine via the XML data model API. Also, since the query engine uses the API to access the <a href="qabstractxmlnodemodel.html">QAbstractXmlNodeModel</a>, the data model subclass can construct the elements, attributes and other data on demand, responding to the query's specific requests. This can greatly improve efficiency, because it means the entire model might not have to be built. For example, in the file system model above, it is not necessary to build an instance for a whole XML file representing the whole file system. Instead nodes are created on demand, which also likely is a small subset of the file system.</p>
<p>Examples of other places where XQuery could be used in Qt XML Patterns to query non-XML data:</p>
<ul>
<li>The internal representation for word processor documents</li>
<li>The set of dependencies for a software build system</li>
<li>The hierarchy (or graph) that links a set of HTML documents from a web crawler</li>
<li>The images and meta-data in an image collection</li>
<li>The set of D-Bus interfaces available in a system</li>
<li>A QObject hierarchy, as seen in the QObject XML Model example.</li>
</ul>
<p>See the <a href="qabstractxmlnodemodel.html">QAbstractXmlNodeModel</a> documentation for information about how to implement custom XML node models.</p>
<a name="more-on-using-qt-xml-patterns-with-non-xml-data"></a>
<h2>More on using Qt XML Patterns with non-XML Data</h2>
<p>Subclassing <a href="qabstractxmlnodemodel.html">QAbstractXmlNodeModel</a> to let the query engine access non-XML data by the same API it uses for XML is the feature that enables Qt XML Patterns to query non-XML data with XQuery. It allows XQuery to be used as a mapping layer between different non-XML node models or between a non-XML node model and the built-in XML node model. Once the subclass(es) of <a href="qabstractxmlnodemodel.html">QAbstractXmlNodeModel</a> have been written, XQuery can be used to select a set of elements from one node model, transform the selected elements, and then write them out, either as XML using <a href="qxmlquery.html#evaluateTo">QXmlQuery::evaluateTo</a>() and <a href="qxmlserializer.html">QXmlSerializer</a>, or as some other format using a subclass of <a href="qabstractxmlreceiver.html">QAbstractXmlReceiver</a>.</p>
<p>Consider a word processor application that must import and export data in several different formats. Rather than writing a lot of C++ code to convert each input format to an intermediate form, and more C++ code to convert the intermediate form back to each output format, one can implement a solution based on Qt XML Patterns that uses simple XQueries to transform each XML or non-XML format (e.g. MathFormula.xml below) to the intermediate form (e.g. the DocumentRepresentation node model class below), and more simple XQueries to transform the intermediate form back to each XML or non-XML format.</p>
<p class="centerAlign"><img src="images/patternist-wordProcessor.png" alt="" /></p><p>Because CSV files are not XML, a subclass of <a href="qabstractxmlnodemodel.html">QAbstractXmlNodeModel</a> is used to present the CSV data to the XQuery engine as if it were XML. What are not shown are the subclasses of <a href="qabstractxmlreceiver.html">QAbstractXmlReceiver</a> that would then send the selected elements into the DocumentRepresentation node model, and the subclasses of <a href="qabstractxmlnodemodel.html">QAbstractXmlNodeModel</a> that would ultimately write the output files in each format.</p>
<a name="security-considerations"></a>
<h2>Security Considerations</h2>
<a name="code-injection"></a>
<h3>Code Injection</h3>
<p>XQuery is vulnerable to <a href="http://en.wikipedia.org/wiki/Code_injection">code injection attacks</a> in the same way as the SQL language. If an XQuery is constructed by concatenating strings, and the strings come from user input, the constructed XQuery could be malevolent. The best way to prevent code injection attacks is to not construct XQueries from user-written strings, but only accept user data input using QVariant and variable bindings. See <a href="qxmlquery.html#bindVariable">QXmlQuery::bindVariable</a>().</p>
<p>The articles <a href="http://www.ibm.com/developerworks/xml/library/x-xpathinjection.html">Avoid the dangers of XPath injection</a>, by Robi Sen and <a href="http://www.packetstormsecurity.org/papers/bypass/Blind_XPath_Injection_20040518.pdf">Blind XPath Injection</a>, by Amit Klein, discuss the XQuery code injection problem in more detail.</p>
<a name="denial-of-service-attacks"></a>
<h3>Denial of Service Attacks</h3>
<p>Applications using Qt XML Patterns are subject to the same limitations of software as other systems. Generally, these can not be checked. This means Qt XML Patterns does not prevent rogue queries from consuming too many resources. For example, a query could take too much time to execute or try to transfer too much data. A query could also do too much recursion, which could crash the system. XQueries can do these things accidentally, but they can also be done as deliberate denial of service attacks.</p>
<a name="features-and-conformance"></a>
<h2>Features and Conformance</h2>
<a name="xquery-1-0"></a>
<h3>XQuery 1.0</h3>
<p>Qt XML Patterns aims at being a <a href="http://www.w3.org/TR/xquery/#id-xquery-conformance">conformant XQuery processor</a>. It adheres to <a href="http://www.w3.org/TR/xquery/#id-minimal-conformance">Minimal Conformance</a> and supports the <a href="http://www.w3.org/TR/xquery/#id-serialization-feature">Serialization Feature</a> and the <a href="http://www.w3.org/TR/xquery/#id-full-axis-feature">Full Axis Feature</a>. Qt XML Patterns currently passes 97% of the tests in the <a href="http://www.w3.org/XML/Query/test-suite">XML Query Test Suite</a>. Areas where conformance may be questionable and where behavior may be changed in future releases include:</p>
<ul>
<li>Some corner cases involving namespaces and element constructors are incorrect.</li>
<li>XPath is a subset of XQuery and the implementation of Qt XML Patterns uses XPath 2.0 with XQuery 1.0.</li>
</ul>
<p>The specifications discusses conformance further: <a href="http://www.w3.org/TR/xquery/">XQuery 1.0: An XML Query Language</a>. W3C's XQuery testing effort can be of interest as well, <a href="http://www.w3.org/XML/Query/test-suite/">XML Query Test Suite</a>.</p>
<p>Currently <tt>fn:collection()</tt> does not access any data set, and there is no API for providing data through the collection. As a result, evaluating <tt>fn:collection()</tt> returns the empty sequence. We intend to provide functionality for this in a future release of Qt.</p>
<p>Only queries encoded in UTF-8 are supported.</p>
<a name="xslt-2-0"></a>
<h3>XSLT 2.0</h3>
<p>Partial support for XSLT was introduced in Qt 4.5. Future releases of Qt XML Patterns will aim to support these XSLT features:</p>
<ul>
<li>Basic XSLT 2.0 processor</li>
<li>Serialization feature</li>
<li>Backwards Compatibility feature</li>
</ul>
<p>For details, see <a href="http://www.w3.org/TR/xslt20/#conformance">XSL Transformations (XSLT) Version 2.0, 21 Conformance</a>.</p>
<p><b>Note: </b>In this release, XSLT support is considered experimental.</p><p>Unsupported or partially supported XSLT features are documented in the following table. The implementation of XSLT in Qt 4.5 can be seen as XSLT 1.0 but with the data model of XPath 2.0 and XSLT 2.0, and using the using the functionality of XPath 2.0 and its accompanying function library. When Qt XML Patterns encounters an unsupported or partially support feature, it will either report a syntax error or silently continue, unless otherwise noted in the table.</p>
<p>The implementation currently passes 42% of W3C's XSLT test suite, which focus on features introduced in XSLT 2.0.</p>
<table class="generic">
<thead><tr class="qt-style"><th >XSL Feature</th><th >Support Status</th></tr></thead>
<tr valign="top" class="odd"><td ><tt>xsl:key</tt> and <tt>fn:key()</tt></td><td >not supported</td></tr>
<tr valign="top" class="even"><td ><tt>xsl:include</tt></td><td >not supported</td></tr>
<tr valign="top" class="odd"><td ><tt>xsl:import</tt></td><td >not supported</td></tr>
<tr valign="top" class="even"><td ><tt>xsl:copy</tt></td><td >The <tt>copy-namespaces</tt> and <tt>inherit-namespaces</tt> attributes have no effect. For copied comments, attributes and processing instructions, the copy has the same node identity as the original.</td></tr>
<tr valign="top" class="odd"><td ><tt>xsl:copy-of</tt></td><td >The <tt>copy-namespaces</tt> attribute has no effect.</td></tr>
<tr valign="top" class="even"><td ><tt>fn:format-number()</tt></td><td >not supported</td></tr>
<tr valign="top" class="odd"><td ><tt>xsl:message</tt></td><td >not supported</td></tr>
<tr valign="top" class="even"><td ><tt>xsl:use-when</tt></td><td >not supported</td></tr>
<tr valign="top" class="odd"><td ><tt>Tunnel</tt> Parameters</td><td >not supported</td></tr>
<tr valign="top" class="even"><td ><tt>xsl:attribute-set</tt></td><td >not supported</td></tr>
<tr valign="top" class="odd"><td ><tt>xsl:decimal-format</tt></td><td >not supported</td></tr>
<tr valign="top" class="even"><td ><tt>xsl:fallback</tt></td><td >not supported</td></tr>
<tr valign="top" class="odd"><td ><tt>xsl:apply-imports</tt></td><td >not supported</td></tr>
<tr valign="top" class="even"><td ><tt>xsl:character-map</tt></td><td >not supported</td></tr>
<tr valign="top" class="odd"><td ><tt>xsl:number</tt></td><td >not supported</td></tr>
<tr valign="top" class="even"><td ><tt>xsl:namespace-alias</tt></td><td >not supported</td></tr>
<tr valign="top" class="odd"><td ><tt>xsl:output</tt></td><td >not supported</td></tr>
<tr valign="top" class="even"><td ><tt>xsl:output-character</tt></td><td >not supported</td></tr>
<tr valign="top" class="odd"><td ><tt>xsl:preserve-space</tt></td><td >not supported</td></tr>
<tr valign="top" class="even"><td ><tt>xsl:result-document</tt></td><td >not supported</td></tr>
<tr valign="top" class="odd"><td >Patterns</td><td >Complex patterns or patterns with predicates have issues.</td></tr>
<tr valign="top" class="even"><td ><tt>2.0</tt> Compatibility Mode</td><td >Stylesheets are interpreted as XSLT 2.0 stylesheets, even if the <tt>version</tt> attribute is in the XSLT source is 1.0. In other words, the version attribute is ignored.</td></tr>
<tr valign="top" class="odd"><td >Grouping</td><td ><tt>fn:current-group()</tt>, <tt>fn:grouping-key()</tt> and <tt>xsl:for-each-group</tt>.</td></tr>
<tr valign="top" class="even"><td >Regexp elements</td><td ><tt>xsl:analyze-string</tt>, <tt>xsl:matching-substring</tt>, <tt>xsl:non-matching-substring</tt>, and <tt>fn:regex-group()</tt></td></tr>
<tr valign="top" class="odd"><td >Date & Time formatting</td><td ><tt>fn:format-dateTime()</tt>, <tt>fn:format-date()</tt> and fn:format-time().</td></tr>
<tr valign="top" class="even"><td >XPath Conformance</td><td >Since XPath is a subset of XSLT, its issues are in affect too.</td></tr>
</table>
<p>The Qt XML Patterns implementation of the XPath Data Model does not include entities (due to QXmlStreamReader not reporting them). This means that functions <tt>unparsed-entity-uri()</tt> and <tt>unparsed-entity-public-id()</tt> always return negatively.</p>
<a name="xpath-2-0"></a>
<h3>XPath 2.0</h3>
<p>Since XPath 2.0 is a subset of XQuery 1.0, XPath 2.0 is supported. Areas where conformance may be questionable and, consequently, where behavior may be changed in future releases include:</p>
<ul>
<li>Regular expression support is currently not conformant but follows Qt's QRegExp standard syntax.</li>
<li>Operators for <tt>xs:time</tt>, <tt>xs:date</tt>, and <tt>xs:dateTime</tt> are incomplete.</li>
<li>Formatting of very large or very small <tt>xs:double</tt>, <tt>xs:float</tt>, and <tt>xs:decimal</tt> values may be incorrect.</li>
</ul>
<a name="xml-id"></a>
<h3>xml:id</h3>
<p>Processing of XML files supports <tt>xml:id</tt>. This allows elements that have an attribute named <tt>xml:id</tt> to be looked up efficiently with the <tt>fn:id()</tt> function. See <a href="http://www.w3.org/TR/xml-id/">xml:id Version 1.0</a> for details.</p>
<a name="xml-schema-1-0"></a>
<h3>XML Schema 1.0</h3>
<p>There are two ways Qt XML Patterns can be used to validate schemas: You can use the C++ API in your Qt application using the classes <a href="qxmlschema.html">QXmlSchema</a> and <a href="qxmlschemavalidator.html">QXmlSchemaValidator</a>, or you can use the command line utility named xmlpatternsvalidator (located in the "bin" directory of your Qt build).</p>
<p>The Qt XML Patterns implementation of XML Schema validation supports the schema specification version 1.0 in large parts. Known problems of the implementation and areas where conformancy may be questionable are:</p>
<ul>
<li>Large <tt>minOccurs</tt> or <tt>maxOccurs</tt> values or deeply nested ones require huge amount of memory which might cause the system to freeze. Such a schema should be rewritten to use <tt>unbounded</tt> as value instead of large numbers. This restriction will hopefully be fixed in a later release.</li>
<li>Comparison of really small or large floating point values might lead to wrong results in some cases. However such numbers should not be relevant for day-to-day usage.</li>
<li>Regular expression support is currently not conformant but follows Qt's QRegExp standard syntax.</li>
<li>Identity constraint checks can not use the values of default or fixed attribute definitions.</li>
</ul>
<a name="resource-loading"></a>
<h3>Resource Loading</h3>
<p>When Qt XML Patterns loads an XML resource, e.g., using the <tt>fn:doc()</tt> function, the following schemes are supported:</p>
<table class="generic">
<thead><tr class="qt-style"><th >Scheme Name</th><th >Description</th></tr></thead>
<tr valign="top" class="odd"><td ><tt>file</tt></td><td >Local files.</td></tr>
<tr valign="top" class="even"><td ><tt>data</tt></td><td >The bytes are encoded in the URI itself. e.g., <tt>data:application/xml,%3Ce%2F%3E</tt> is <tt><e/></tt>.</td></tr>
<tr valign="top" class="odd"><td ><tt>ftp</tt></td><td >Resources retrieved via FTP.</td></tr>
<tr valign="top" class="even"><td ><tt>http</tt></td><td >Resources retrieved via HTTP.</td></tr>
<tr valign="top" class="odd"><td ><tt>https</tt></td><td >Resources retrieved via HTTPS. This will succeed if no SSL errors are encountered.</td></tr>
<tr valign="top" class="even"><td ><tt>qrc</tt></td><td >Qt Resource files. Expressing it as an empty scheme, :/..., is not supported.</td></tr>
</table>
<a name="xml"></a>
<h3>XML</h3>
<p>XML 1.0 and XML Namespaces 1.0 are supported, as opposed to the 1.1 versions. When a strings is passed to a query as a QString, the characters must be XML 1.0 characters. Otherwise, the behavior is undefined. This is not checked.</p>
<p>URIs are first passed to <a href="qabstracturiresolver.html">QAbstractUriResolver</a>. Check <a href="qxmlquery.html#setUriResolver">QXmlQuery::setUriResolver</a>() for possible rewrites.</p>
</div>
<!-- @@@xmlprocessing.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="Working with the DOM Tree">Working with the DOM Tree</a>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2014 Digia Plc and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|