![]() |
Home · Examples |
[Previous: QtXml Module][Qt's Modules][Next: Phonon Module]
The QtXmlPatterns module supports using XQuery 1.0 and XPath 2.0 in Qt applications, for querying XML data and for querying non-XML data that can be modeled to look like XML. The QtXmlPatterns module is included in the Qt Desktop Edition, and the Qt Open Source Edition. Readers who are not familiar with the XQuery/XPath language can read A Short Path to XQuery for a brief introduction.Advantages of using QtXmlPatterns and XQuery
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:<bibliography>
{doc("library.xml")/bib/book[publisher="Addison-Wesley" and @year>1991]/<book year="{@year}">{title}</book>}
</bibliography>
First, the query opens a <bibliography> element in the output. The embedded path expression then loads the XML document describing the contents of the library (library.xml) and begins the search. For each <book> element it finds, where the publisher was Addison-Wesley and the publication year was after 1991, it creates a new <book> element in the output as a child of the open <bibliography> element. Each new <book> element gets the book's title as its contents and the book's publication year as an attribute. Finally, the <bibliography> element is closed.
The advantages of using QtXmlPatterns and XQuery in your Qt programs are summarized as follows:
#include <QtXmlPatterns>Link the compiled application with the QtXmlPatterns module by adding the following line to the QT line in your qmake .pro file:
QT += xmlpatternsIf we save the example XQuery shown above in a text file (e.g. myquery.xq), we can run it from a Qt application using a standard QtXmlPatterns code sequence:Error parsing snippet. First construct a QFile for the text file containing the XQuery (myquery.xq). Then create an instance of QXmlQuery and call its setQuery() function to load and parse the XQuery file. Then create an XML serializer to output the query's result set as unformatted XML. Finally, call the evaluateTo() function to evaluate the query and serialize the results as XML.
Note: If you compile Qt yourself, the QtXmlPatterns module will not be built if exceptions are disabled, or if you compile Qt with a compiler that doesn't support member templates, e.g., MSVC 6.
See the QXmlQuery documentation for more information about the QtXmlPatterns C++ API. 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. 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. Suppose you want to parameterize the bibliography XQuery in the example above. You could define variables for the catalog that contains the library ($file), the publisher name ($publisher), and the year of publication ($year):Running the query engine from the command line utility
xmlpatterns is a command line utility for running XQueries. It expects the name of a file containing the XQuery text.
xmlpatterns myQuery.xq
The XQuery in myQuery.xq will be evaluated and its output written to stdout. Pass the -help switch to get the list of input flags and their meanings. The XQuery Data Model
XQuery represents data items as atomic values or nodes. An atomic value is a value in the domain of one of the built-in datatypes defined in Part 2 of the W3C XML Schema. A node is normally an XML element or attribute, but when non-XML data is modeled to look like XML, a node can also represent a non-XML data items. Binding program variables to XQuery variables
When you want to run a parameterized XQuery from your Qt application, you will need to bind variables in your program to $name variables in your XQuery. Missing file: snippets/patternist/introExample2.xq Modify the QtXmlPatterns code to use one of the bindVariable() functions to bind a program variable to each XQuery $variable:Error parsing snippet.
Each program variable is passed to QtXmlPatterns as a QVariant of the type of the C++ variable or constant from which it is constructed. Note that QtXmlPatterns 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:
QVariant::LongLong | xs:integer |
QVariant::Int | xs:integer |
QVariant::UInt | xs:nonNegativeInteger |
QVariant::ULongLong | xs:unsignedLong |
QVariant::String | xs:string |
QVariant::Double | xs:double |
QVariant::Bool | xs:boolean |
QVariant::Double | xs:decimal |
QVariant::ByteArray | xs:base64Binary |
QVariant::StringList | xs:string* |
QVariant::Url | xs:string |
QVariant::Date | xs:date. |
QVariant::DateTime | xs:dateTime |
QVariant::Time. | xs:time. (see Binding To QVariant::Time below) |
QVariantList | (see Binding To QVariantList below) |
xs:QName | QXmlName (see Handling QXmlNames below) |
xs:integer | QVariant::LongLong |
xs:string | QVariant::String |
xs:string* | QVariant::StringList |
xs:double | QVariant::Double |
xs:float | QVariant::Double |
xs:boolean | QVariant::Bool |
xs:decimal | QVariant::Double |
xs:hexBinary | QVariant::ByteArray |
xs:base64Binary | QVariant::ByteArray |
xs:gYear | QVariant::DateTime |
xs:gYearMonth | QVariant::DateTime |
xs:gMonthDay | QVariant::DateTime |
xs:gDay | QVariant::DateTime |
xs:gMonth | QVariant::DateTime |
xs:anyURI | QVariant::Url |
xs:untypedAtomic | QVariant::String |
xs:ENTITY | QVariant::String |
xs:date | QVariant::DateTime |
xs:dateTime | QVariant::DateTime |
xs:time | (see No mapping for xs:time below) |
When QtXmlPatterns 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 myOrders.xml to find all the skin care product orders and output them ordered by shipping date.
<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>QtXmlPatterns can be used out of the box to perform this query, provided myOrders.xml actually contains well-formed XML. It can be loaded directly into the default XML node model and traversed. But suppose we want QtXmlPatterns to perform queries on the hierarchical structure of the local file system. The default XML node model in QtXmlPatterns 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:
<?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>The File System Example does exactly this.
There is no such file to load into the default XML node model, but one can write a subclass of QAbstractXmlNodeModel 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.
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.snippets/patternist/introNavigateFS.xq Without QtXmlPatterns, 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 QAbstractXmlNodeModel, 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.
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.
With QtXmlPatterns, subclassing QAbstractXmlNodeModel 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 QAbstractXmlNodeModel, 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.
Examples of other places where XQuery could be used in QtXmlPatterns to query non-XML data:
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 QtXmlPatterns 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.
The articles Avoid the dangers of XPath injection, Robi Sen and Blind XPath Injection, Amit Klein discuss the XQuery code injection problem in more detail.Denial of Service Attacks
Applications using QtXmlPatterns are subject to the same software limits as any other system. Generally, these can not be checked. This means QtXmlPatterns does not prevent rogue queries from consuming too many resources. For example, a query could take too much time to execute, or could attempt to transfer too much data. Or a query could cause an unreasonable amount of recursion, which could crash the system. XQueries can do these things accidentally, but they can also be meant as deliberate, denial of service attacks.Features and Conformance
Conformance
QtXmlPatterns aims at being a conformant XQuery processor. Apart from adhering to {http://www.w3.org/TR/xquery/#id-minimal-conformance} {Minimal Conformance}, QtXmlPatterns supports the Serialization Feature and the Full Axis Feature. QtXmlPatterns passes 97% of the tests in the XML Query Test Suite, and it is expected this will improve over time. Areas where conformance may be questionable and where behavior may be changed in future releases are:
Since XPath 2.0 is a subset of XQuery 1.0, it is supported.
The specifications discusses conformance further: XQuery 1.0: An XML Query Language. W3C's XQuery testing effort can be of interest as well, XML Query Test Suite.
Currently fn:collection() does not access any data set, and there is no API for providing data through the collection. As a result, evaluating fn:collection() returns the empty sequence. We intend to provide functionality for this in a future release of Qt.
Processing of XML files supports xml:id. In practice, this allows elements that have an attribute named xml:id to be looked up efficiently with the fn:id() function. See xml:id Version 1.0 for details.
Only queries encoded in UTF-8 are supported.Resource Loading
When QtXmlPatterns loads an XML resource, e.g., using fn:doc() function, the following schemes are supported:
file | Local files. |
data | The bytes are encoded in the URI itself. For instance, data:application/xml,%3Ce%2F%3E is <e/>. |
ftp | Resources retrieved via FTP. |
http | Resources retrieved via HTTP. |
https | Resources retrieved via HTTPS. This will succeed if no SSL errors are encountered. |
qrc | Qt Resource files. Expressing it as an empty scheme, :/..., is not supported. |
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.4.2_01 |