1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \page xmlprocessing.html
|
---|
30 | \title XQuery
|
---|
31 |
|
---|
32 | \previouspage Working with the DOM Tree
|
---|
33 | \contentspage XML Processing
|
---|
34 |
|
---|
35 | \keyword Patternist
|
---|
36 |
|
---|
37 | \brief An overview of Qt's support for using XML technologies in
|
---|
38 | Qt programs.
|
---|
39 |
|
---|
40 | \tableofcontents
|
---|
41 |
|
---|
42 | \section1 Introduction
|
---|
43 |
|
---|
44 | XQuery is a language for traversing XML documents to select and
|
---|
45 | aggregate items of interest and to transform them for output as
|
---|
46 | XML or some other format. XPath is the \e{element selection} part
|
---|
47 | of XQuery.
|
---|
48 |
|
---|
49 | The QtXmlPatterns module supports using
|
---|
50 | \l{http://www.w3.org/TR/xquery} {XQuery 1.0} and
|
---|
51 | \l{http://www.w3.org/TR/xpath20} {XPath 2.0} in Qt applications,
|
---|
52 | for querying XML data \e{and} for querying
|
---|
53 | \l{QAbstractXmlNodeModel} {non-XML data that can be modeled to
|
---|
54 | look like XML}.
|
---|
55 | Readers who are not familiar with the XQuery/XPath language can read
|
---|
56 | \l {A Short Path to XQuery} for a brief introduction.
|
---|
57 |
|
---|
58 | \section1 Advantages of using QtXmlPatterns and XQuery
|
---|
59 |
|
---|
60 | The XQuery/XPath language simplifies data searching and
|
---|
61 | transformation tasks by eliminating the need for doing a lot of
|
---|
62 | C++ or Java procedural programming for each new query task. Here
|
---|
63 | is an XQuery that constructs a bibliography of the contents of a
|
---|
64 | library:
|
---|
65 |
|
---|
66 | \target qtxmlpatterns_example_query
|
---|
67 | \quotefile snippets/patternist/introductionExample.xq
|
---|
68 |
|
---|
69 | First, the query opens a \c{<bibliography>} element in the
|
---|
70 | output. The
|
---|
71 | \l{xquery-introduction.html#using-path-expressions-to-match-and-select-items}
|
---|
72 | {embedded path expression} then loads the XML document describing
|
---|
73 | the contents of the library (\c{library.xml}) and begins the
|
---|
74 | search. For each \c{<book>} element it finds, where the publisher
|
---|
75 | was Addison-Wesley and the publication year was after 1991, it
|
---|
76 | creates a new \c{<book>} element in the output as a child of the
|
---|
77 | open \c{<bibliography>} element. Each new \c{<book>} element gets
|
---|
78 | the book's title as its contents and the book's publication year
|
---|
79 | as an attribute. Finally, the \c{<bibliography>} element is
|
---|
80 | closed.
|
---|
81 |
|
---|
82 | The advantages of using QtXmlPatterns and XQuery in your Qt
|
---|
83 | programs are summarized as follows:
|
---|
84 |
|
---|
85 | \list
|
---|
86 |
|
---|
87 | \o \bold{Ease of development}: All the C++ programming required to
|
---|
88 | perform data query tasks can be replaced by a simple XQuery
|
---|
89 | like the example above.
|
---|
90 |
|
---|
91 | \o \bold{Comprehensive functionality}: The
|
---|
92 | \l{http://www.w3.org/TR/xquery/#id-expressions} {expression
|
---|
93 | syntax} and rich set of
|
---|
94 | \l{http://www.w3.org/TR/xpath-functions} {functions and
|
---|
95 | operators} provided by XQuery are sufficient for performing any
|
---|
96 | data searching, selecting, and sorting tasks.
|
---|
97 |
|
---|
98 | \o \bold{Conformance to standards}: Conformance to all applicable
|
---|
99 | XML and XQuery standards ensures that QtXmlPatterns can always
|
---|
100 | process XML documents generated by other conformant
|
---|
101 | applications, and that XML documents created with QtXmlPatterns
|
---|
102 | can be processed by other conformant applications.
|
---|
103 |
|
---|
104 | \o \bold{Maximal flexibility} The QtXmlPatterns module can be used
|
---|
105 | to query XML data \e{and} non-XML data that can be
|
---|
106 | \l{QAbstractXmlNodeModel} {modeled to look like XML}.
|
---|
107 |
|
---|
108 | \endlist
|
---|
109 |
|
---|
110 | \section1 Using the QtXmlPatterns module
|
---|
111 |
|
---|
112 | There are two ways QtXmlPatterns can be used to evaluate queries.
|
---|
113 | You can run the query engine in your Qt application using the
|
---|
114 | QtXmlPatterns C++ API, or you can run the query engine from the
|
---|
115 | command line using Qt's \c{xmlpatterns} command line utility.
|
---|
116 |
|
---|
117 | \section2 Running the query engine from your Qt application
|
---|
118 |
|
---|
119 | If we save the example XQuery shown above in a text file (e.g.
|
---|
120 | \c{myquery.xq}), we can run it from a Qt application using a
|
---|
121 | standard QtXmlPatterns code sequence:
|
---|
122 |
|
---|
123 | \snippet doc/src/snippets/code/src_xmlpatterns_api_qxmlquery.cpp 3
|
---|
124 |
|
---|
125 | First construct a QFile for the text file containing the XQuery
|
---|
126 | (\c{myquery.xq}). Then create an instance of QXmlQuery and call
|
---|
127 | its \l{QXmlQuery::}{setQuery()} function to load and parse the
|
---|
128 | XQuery file. Then create an \l{QXmlSerializer} {XML serializer} to
|
---|
129 | output the query's result set as unformatted XML. Finally, call
|
---|
130 | the \l{QXmlQuery::}{evaluateTo()} function to evaluate the query
|
---|
131 | and serialize the results as XML.
|
---|
132 |
|
---|
133 | \note If you compile Qt yourself, the QtXmlPatterns module will
|
---|
134 | \e{not} be built if exceptions are disabled, or if you compile Qt
|
---|
135 | with a compiler that doesn't support member templates, e.g., MSVC
|
---|
136 | 6.
|
---|
137 |
|
---|
138 | See the QXmlQuery documentation for more information about the
|
---|
139 | QtXmlPatterns C++ API.
|
---|
140 |
|
---|
141 | \section2 Running the query engine from the command line utility
|
---|
142 |
|
---|
143 | \e xmlpatterns is a command line utility for running XQueries. It
|
---|
144 | expects the name of a file containing the XQuery text.
|
---|
145 |
|
---|
146 | \snippet doc/src/snippets/code/doc_src_qtxmlpatterns.qdoc 2
|
---|
147 |
|
---|
148 | The XQuery in \c{myQuery.xq} will be evaluated and its output
|
---|
149 | written to \c stdout. Pass the \c -help switch to get the list of
|
---|
150 | input flags and their meanings.
|
---|
151 |
|
---|
152 | xmlpatterns can be used in scripting. However, the descriptions
|
---|
153 | and messages it outputs were not meant to be parsed and may be
|
---|
154 | changed in future releases of Qt.
|
---|
155 |
|
---|
156 | \target QtXDM
|
---|
157 | \section1 The XQuery Data Model
|
---|
158 |
|
---|
159 | XQuery represents data items as \e{atomic values} or \e{nodes}. An
|
---|
160 | atomic value is a value in the domain of one of the
|
---|
161 | \l{http://www.w3.org/TR/xmlschema-2/#built-in-datatypes} {built-in
|
---|
162 | datatypes} defined in \l{http://www.w3.org/TR/xmlschema-2} {Part
|
---|
163 | 2} of the W3C XML Schema. A node is normally an XML element or
|
---|
164 | attribute, but when non-XML data is \l{QAbstractXmlNodeModel}
|
---|
165 | {modeled to look like XML}, a node can also represent a non-XML
|
---|
166 | data items.
|
---|
167 |
|
---|
168 | When you run an XQuery using the C++ API in a Qt application, you
|
---|
169 | will often want to bind program variables to $variables in the
|
---|
170 | XQuery. After the query is evaluated, you will want to interpret
|
---|
171 | the sequence of data items in the result set.
|
---|
172 |
|
---|
173 | \section2 Binding program variables to XQuery variables
|
---|
174 |
|
---|
175 | When you want to run a parameterized XQuery from your Qt
|
---|
176 | application, you will need to \l{QXmlQuery::bindVariable()} {bind
|
---|
177 | variables} in your program to $name variables in your XQuery.
|
---|
178 |
|
---|
179 | Suppose you want to parameterize the bibliography XQuery in the
|
---|
180 | example above. You could define variables for the catalog that
|
---|
181 | contains the library (\c{$file}), the publisher name
|
---|
182 | (\c{$publisher}), and the year of publication (\c{$year}):
|
---|
183 |
|
---|
184 | \target qtxmlpatterns_example_query2
|
---|
185 | \quotefile snippets/patternist/introExample2.xq
|
---|
186 |
|
---|
187 | Modify the QtXmlPatterns code to use one of the \l{QXmlQuery::}
|
---|
188 | {bindVariable()} functions to bind a program variable to each
|
---|
189 | XQuery $variable:
|
---|
190 |
|
---|
191 | \snippet doc/src/snippets/code/src_xmlpatterns_api_qxmlquery.cpp 4
|
---|
192 |
|
---|
193 | Each program variable is passed to QtXmlPatterns as a QVariant of
|
---|
194 | the type of the C++ variable or constant from which it is
|
---|
195 | constructed. Note that QtXmlPatterns assumes that the type of the
|
---|
196 | QVariant in the bindVariable() call is the correct type, so the
|
---|
197 | $variable it is bound to must be used in the XQuery accordingly.
|
---|
198 | The following table shows how QVariant types are mapped to XQuery
|
---|
199 | $variable types:
|
---|
200 |
|
---|
201 | \table
|
---|
202 |
|
---|
203 | \header
|
---|
204 | \o QVariant type
|
---|
205 | \o XQuery $variable type
|
---|
206 |
|
---|
207 | \row
|
---|
208 | \o QVariant::LongLong
|
---|
209 | \o \c xs:integer
|
---|
210 |
|
---|
211 | \row
|
---|
212 | \o QVariant::Int
|
---|
213 | \o \c xs:integer
|
---|
214 |
|
---|
215 | \row
|
---|
216 | \o QVariant::UInt
|
---|
217 | \o \c xs:nonNegativeInteger
|
---|
218 |
|
---|
219 | \row
|
---|
220 | \o QVariant::ULongLong
|
---|
221 | \o \c xs:unsignedLong
|
---|
222 |
|
---|
223 | \row
|
---|
224 | \o QVariant::String
|
---|
225 | \o \c xs:string
|
---|
226 |
|
---|
227 | \row
|
---|
228 | \o QVariant::Double
|
---|
229 | \o \c xs:double
|
---|
230 |
|
---|
231 | \row
|
---|
232 | \o QVariant::Bool
|
---|
233 | \o \c xs:boolean
|
---|
234 |
|
---|
235 | \row
|
---|
236 | \o QVariant::Double
|
---|
237 | \o \c xs:decimal
|
---|
238 |
|
---|
239 | \row
|
---|
240 | \o QVariant::ByteArray
|
---|
241 | \o \c xs:base64Binary
|
---|
242 |
|
---|
243 | \row
|
---|
244 | \o QVariant::StringList
|
---|
245 | \o \c xs:string*
|
---|
246 |
|
---|
247 | \row
|
---|
248 | \o QVariant::Url
|
---|
249 | \o \c xs:string
|
---|
250 |
|
---|
251 | \row
|
---|
252 | \o QVariant::Date
|
---|
253 | \o \c xs:date.
|
---|
254 |
|
---|
255 | \row
|
---|
256 | \o QVariant::DateTime
|
---|
257 | \o \c xs:dateTime
|
---|
258 |
|
---|
259 | \row
|
---|
260 | \o QVariant::Time.
|
---|
261 | \o \c xs:time. (see \l{Binding To Time}{Binding To
|
---|
262 | QVariant::Time} below)
|
---|
263 |
|
---|
264 | \row
|
---|
265 | \o QVariantList
|
---|
266 | \o (see \l{Binding To QVariantList}{Binding To QVariantList}
|
---|
267 | below)
|
---|
268 |
|
---|
269 | \endtable
|
---|
270 |
|
---|
271 | A type not shown in the table is not supported and will cause
|
---|
272 | undefined XQuery behavior or a $variable binding error, depending
|
---|
273 | on the context in the XQuery where the variable is used.
|
---|
274 |
|
---|
275 | \target Binding To Time
|
---|
276 | \section3 Binding To QVariant::Time
|
---|
277 |
|
---|
278 | Because the instance of QTime used in QVariant::Time does not
|
---|
279 | include a zone offset, an instance of QVariant::Time should not be
|
---|
280 | bound to an XQuery variable of type \c xs:time, unless the QTime is
|
---|
281 | UTC. When binding a non-UTC QTime to an XQuery variable, it should
|
---|
282 | first be passed as a string, or converted to a QDateTime with an arbitrary
|
---|
283 | date, and then bound to an XQuery variable of type \c xs:dateTime.
|
---|
284 |
|
---|
285 | \target Binding To QVariantList
|
---|
286 | \section3 Binding To QVariantList
|
---|
287 |
|
---|
288 | A QVariantList can be bound to an XQuery $variable. All the
|
---|
289 | \l{QVariant}s in the list must be of the same atomic type, and the
|
---|
290 | $variable the variant list is bound to must be of that same atomic
|
---|
291 | type. If the QVariants in the list are not all of the same atomic
|
---|
292 | type, the XQuery behavior is undefined.
|
---|
293 |
|
---|
294 | \section2 Interpreting XQuery results
|
---|
295 |
|
---|
296 | When the results of an XQuery are returned in a sequence of \l
|
---|
297 | {QXmlResultItems} {result items}, atomic values in the sequence
|
---|
298 | are treated as instances of QVariant. Suppose that instead of
|
---|
299 | serializing the results of the XQuery as XML, we process the
|
---|
300 | results programatically. Modify the standard QtXmlPatterns code
|
---|
301 | sequence to call the overload of QXmlQuery::evaluateTo() that
|
---|
302 | populates a sequence of \l {QXmlResultItems} {result items} with
|
---|
303 | the XQuery results:
|
---|
304 |
|
---|
305 | \snippet doc/src/snippets/code/src_xmlpatterns_api_qxmlquery.cpp 5
|
---|
306 |
|
---|
307 | Iterate through the \l {QXmlResultItems} {result items} and test
|
---|
308 | each QXmlItem to see if it is an atomic value or a node. If it is
|
---|
309 | an atomic value, convert it to a QVariant with \l {QXmlItem::}
|
---|
310 | {toAtomicValue()} and switch on its \l {QVariant::type()} {variant
|
---|
311 | type} to handle all the atomic values your XQuery might return.
|
---|
312 | The following table shows the QVariant type to expect for each
|
---|
313 | atomic value type (or QXmlName):
|
---|
314 |
|
---|
315 | \table
|
---|
316 |
|
---|
317 | \header
|
---|
318 | \o XQuery result item type
|
---|
319 | \o QVariant type returned
|
---|
320 |
|
---|
321 | \row
|
---|
322 | \o \c xs:QName
|
---|
323 | \o QXmlName (see \l{Handling QXmlNames}{Handling QXmlNames}
|
---|
324 | below)
|
---|
325 |
|
---|
326 | \row
|
---|
327 | \o \c xs:integer
|
---|
328 | \o QVariant::LongLong
|
---|
329 |
|
---|
330 | \row
|
---|
331 | \o \c xs:string
|
---|
332 | \o QVariant::String
|
---|
333 |
|
---|
334 | \row
|
---|
335 | \o \c xs:string*
|
---|
336 | \o QVariant::StringList
|
---|
337 |
|
---|
338 | \row
|
---|
339 | \o \c xs:double
|
---|
340 | \o QVariant::Double
|
---|
341 |
|
---|
342 | \row
|
---|
343 | \o \c xs:float
|
---|
344 | \o QVariant::Double
|
---|
345 |
|
---|
346 | \row
|
---|
347 | \o \c xs:boolean
|
---|
348 | \o QVariant::Bool
|
---|
349 |
|
---|
350 | \row
|
---|
351 | \o \c xs:decimal
|
---|
352 | \o QVariant::Double
|
---|
353 |
|
---|
354 | \row
|
---|
355 | \o \c xs:hexBinary
|
---|
356 | \o QVariant::ByteArray
|
---|
357 |
|
---|
358 | \row
|
---|
359 | \o \c xs:base64Binary
|
---|
360 | \o QVariant::ByteArray
|
---|
361 |
|
---|
362 | \row
|
---|
363 | \o \c xs:gYear
|
---|
364 | \o QVariant::DateTime
|
---|
365 |
|
---|
366 | \row
|
---|
367 | \o \c xs:gYearMonth
|
---|
368 | \o QVariant::DateTime
|
---|
369 |
|
---|
370 | \row
|
---|
371 | \o \c xs:gMonthDay
|
---|
372 | \o QVariant::DateTime
|
---|
373 |
|
---|
374 | \row
|
---|
375 | \o \c xs:gDay
|
---|
376 | \o QVariant::DateTime
|
---|
377 |
|
---|
378 | \row
|
---|
379 | \o \c xs:gMonth
|
---|
380 | \o QVariant::DateTime
|
---|
381 |
|
---|
382 | \row
|
---|
383 | \o \c xs:anyURI
|
---|
384 | \o QVariant::Url
|
---|
385 |
|
---|
386 | \row
|
---|
387 | \o \c xs:untypedAtomic
|
---|
388 | \o QVariant::String
|
---|
389 |
|
---|
390 | \row
|
---|
391 | \o \c xs:ENTITY
|
---|
392 | \o QVariant::String
|
---|
393 |
|
---|
394 | \row
|
---|
395 | \o \c xs:date
|
---|
396 | \o QVariant::DateTime
|
---|
397 |
|
---|
398 | \row
|
---|
399 | \o \c xs:dateTime
|
---|
400 | \o QVariant::DateTime
|
---|
401 |
|
---|
402 | \row
|
---|
403 | \o \c xs:time
|
---|
404 | \o (see \l{xstime-not-mapped}{No mapping for xs:time} below)
|
---|
405 |
|
---|
406 | \endtable
|
---|
407 |
|
---|
408 | \target Handling QXmlNames
|
---|
409 | \section3 Handling QXmlNames
|
---|
410 |
|
---|
411 | If your XQuery can return atomic value items of type \c{xs:QName},
|
---|
412 | they will appear in your QXmlResultItems as instances of QXmlName.
|
---|
413 | Since the QVariant class does not support the QXmlName class
|
---|
414 | directly, extracting them from QXmlResultItems requires a bit of
|
---|
415 | slight-of-hand using the \l{QMetaType} {Qt metatype system}. We
|
---|
416 | must modify our example to use a couple of template functions, a
|
---|
417 | friend of QMetaType (qMetaTypeId<T>()) and a friend of QVariant
|
---|
418 | (qVariantValue<T>()):
|
---|
419 |
|
---|
420 | \snippet doc/src/snippets/code/src_xmlpatterns_api_qxmlquery.cpp 6
|
---|
421 |
|
---|
422 | To access the strings in a QXmlName returned by an
|
---|
423 | \l{QXmlQuery::evaluateTo()} {XQuery evaluation}, the QXmlName must
|
---|
424 | be accessed with the \l{QXmlNamePool} {name pool} from the
|
---|
425 | instance of QXmlQuery that was used for the evaluation.
|
---|
426 |
|
---|
427 | \target xstime-not-mapped
|
---|
428 | \section3 No mapping for xs:time
|
---|
429 |
|
---|
430 | An instance of \c xs:time can't be represented correctly as an
|
---|
431 | instance of QVariant::Time, unless the \c xs:time is a UTC time.
|
---|
432 | This is because xs:time has a zone offset (0 for UTC) in addition
|
---|
433 | to the time value, which the QTime in QVariant::Time does not
|
---|
434 | have. This means that if an XQuery tries to return an atomic value
|
---|
435 | of type \c xs:time, an invalid QVariant will be returned. A query
|
---|
436 | can return an atomic value of type xs:time by either converting it
|
---|
437 | to an \c xs:dateTime with an arbitrary date, or to an \c xs:string.
|
---|
438 |
|
---|
439 | \section1 Using XQuery with Non-XML Data
|
---|
440 |
|
---|
441 | Although the XQuery language was designed for querying XML, with
|
---|
442 | QtXmlPatterns one can use XQuery for querying any data that can
|
---|
443 | be modeled to look like XML. Non-XML data is modeled to look like
|
---|
444 | XML by loading it into a custom subclass of QAbstractXmlNodeModel,
|
---|
445 | where it is then presented to the QtXmlPatterns XQuery engine via
|
---|
446 | the same API the XQuery engine uses for querying XML.
|
---|
447 |
|
---|
448 | When QtXmlPatterns loads and queries XML files and produces XML
|
---|
449 | output, it can always load the XML data into its default XML node
|
---|
450 | model, where it can be traversed efficiently. The XQuery below
|
---|
451 | traverses the product orders found in the XML file \e myOrders.xml
|
---|
452 | to find all the skin care product orders and output them ordered
|
---|
453 | by shipping date.
|
---|
454 |
|
---|
455 | \quotefile snippets/patternist/introAcneRemover.xq
|
---|
456 |
|
---|
457 | QtXmlPatterns can be used out of the box to perform this
|
---|
458 | query, provided \e myOrders.xml actually contains well-formed XML. It
|
---|
459 | can be loaded directly into the default XML node model and
|
---|
460 | traversed. But suppose we want QtXmlPatterns to perform queries on
|
---|
461 | the hierarchical structure of the local file system. The default
|
---|
462 | XML node model in QtXmlPatterns is not suitable for navigating the
|
---|
463 | file system, because there is no XML file to load that contains a
|
---|
464 | description of it. Such an XML file, if it existed, might look
|
---|
465 | something like this:
|
---|
466 |
|
---|
467 | \quotefile snippets/patternist/introFileHierarchy.xml
|
---|
468 |
|
---|
469 | The \l{File System Example}{File System Example} does exactly this.
|
---|
470 |
|
---|
471 | There is no such file to load into the default XML node model, but
|
---|
472 | one can write a subclass of QAbstractXmlNodeModel to represent the
|
---|
473 | file system. This custom XML node model, once populated with all
|
---|
474 | the directory and file descriptors obtained directly from the
|
---|
475 | system, presents the complete file system hierarchy to the query
|
---|
476 | engine via the same API used by the default XML node model to
|
---|
477 | present the contents of an XML file. In other words, once the
|
---|
478 | custom XML node model is populated, it presents the file system to
|
---|
479 | the query engine as if a description of it had been loaded into
|
---|
480 | the default XML node model from an XML file like the one shown
|
---|
481 | above.
|
---|
482 |
|
---|
483 | Now we can write an XQuery to find all the XML files and parse
|
---|
484 | them to find the ones that don't contain well-formed XML.
|
---|
485 |
|
---|
486 | \quotefromfile snippets/patternist/introNavigateFS.xq
|
---|
487 | \skipto <html>
|
---|
488 | \printuntil
|
---|
489 |
|
---|
490 | Without QtXmlPatterns, there is no simple way to solve this kind
|
---|
491 | of problem. You might do it by writing a C++ program to traverse
|
---|
492 | the file system, sniff out all the XML files, and submit each one
|
---|
493 | to an XML parser to test that it contains valid XML. The C++ code
|
---|
494 | required to write that program will probably be more complex than
|
---|
495 | the C++ code required to subclass QAbstractXmlNodeModel, but even
|
---|
496 | if the two are comparable, your custom C++ program can be used
|
---|
497 | only for that one task, while your custom XML node model can be
|
---|
498 | used by any XQuery that must navigate the file system.
|
---|
499 |
|
---|
500 | The general approach to using XQuery to perform queries on non-XML
|
---|
501 | data has been a three step process. In the first step, the data is
|
---|
502 | loaded into a non-XML data model. In the second step, the non-XML
|
---|
503 | data model is serialized as XML and output to XML (text) files. In
|
---|
504 | the final step, an XML tool loads the XML files into a second, XML
|
---|
505 | data model, where the XQueries can be performed. The development
|
---|
506 | cost of implementing this process is often high, and the three
|
---|
507 | step system that results is inefficient because the two data
|
---|
508 | models must be built and maintained separately.
|
---|
509 |
|
---|
510 | With QtXmlPatterns, subclassing QAbstractXmlNodeModel eliminates
|
---|
511 | the transformation required to convert the non-XML data model to
|
---|
512 | the XML data model, because there is only ever one data model
|
---|
513 | required. The non-XML data model presents the non-XML data to the
|
---|
514 | query engine via the XML data model API. Also, since the query
|
---|
515 | engine uses the API to access the QAbstractXmlNodeModel, the data
|
---|
516 | model subclass can construct the elements, attributes and other
|
---|
517 | data on demand, responding to the query's specific requests. This
|
---|
518 | can greatly improve efficiency, because it means the entire model
|
---|
519 | might not have to be built. For example, in the file system model
|
---|
520 | above, it is not necessary to build an instance for a whole
|
---|
521 | XML file representing the whole file system. Instead nodes are
|
---|
522 | created on demand, which also likely is a small subset of the file
|
---|
523 | system.
|
---|
524 |
|
---|
525 | Examples of other places where XQuery could be used in
|
---|
526 | QtXmlPatterns to query non-XML data:
|
---|
527 |
|
---|
528 | \list
|
---|
529 |
|
---|
530 | \o The internal representation for word processor documents
|
---|
531 |
|
---|
532 | \o The set of dependencies for a software build system
|
---|
533 |
|
---|
534 | \o The hierarchy (or graph) that links a set of HTML documents
|
---|
535 | from a web crawler
|
---|
536 |
|
---|
537 | \o The images and meta-data in an image collection
|
---|
538 |
|
---|
539 | \o The set of D-Bus interfaces available in a system
|
---|
540 |
|
---|
541 | \o A QObject hierarchy, as seen in the \l{QObject XML Model
|
---|
542 | Example} {QObject XML Model example}.
|
---|
543 |
|
---|
544 | \endlist
|
---|
545 |
|
---|
546 | See the QAbstractXmlNodeModel documentation for information about
|
---|
547 | how to implement custom XML node models.
|
---|
548 |
|
---|
549 | \section1 More on using QtXmlPatterns with non-XML Data
|
---|
550 |
|
---|
551 | Subclassing QAbstractXmlNodeModel to let the query engine access
|
---|
552 | non-XML data by the same API it uses for XML is the feature that
|
---|
553 | enables QtXmlPatterns to query non-XML data with XQuery. It allows
|
---|
554 | XQuery to be used as a mapping layer between different non-XML
|
---|
555 | node models or between a non-XML node model and the built-in XML
|
---|
556 | node model. Once the subclass(es) of QAbstractXmlNodeModel have
|
---|
557 | been written, XQuery can be used to select a set of elements from
|
---|
558 | one node model, transform the selected elements, and then write
|
---|
559 | them out, either as XML using QXmlQuery::evaluateTo() and QXmlSerializer,
|
---|
560 | or as some other format using a subclass of QAbstractXmlReceiver.
|
---|
561 |
|
---|
562 | Consider a word processor application that must import and export
|
---|
563 | data in several different formats. Rather than writing a lot of
|
---|
564 | C++ code to convert each input format to an intermediate form, and
|
---|
565 | more C++ code to convert the intermediate form back to each
|
---|
566 | output format, one can implement a solution based on QtXmlPatterns
|
---|
567 | that uses simple XQueries to transform each XML or non-XML format
|
---|
568 | (e.g. MathFormula.xml below) to the intermediate form (e.g. the
|
---|
569 | DocumentRepresentation node model class below), and more simple
|
---|
570 | XQueries to transform the intermediate form back to each XML or
|
---|
571 | non-XML format.
|
---|
572 |
|
---|
573 | \image patternist-wordProcessor.png
|
---|
574 |
|
---|
575 | Because CSV files are not XML, a subclass of QAbstractXmlNodeModel
|
---|
576 | is used to present the CSV data to the XQuery engine as if it were
|
---|
577 | XML. What are not shown are the subclasses of QAbstractXmlReceiver
|
---|
578 | that would then send the selected elements into the
|
---|
579 | DocumentRepresentation node model, and the subclasses of
|
---|
580 | QAbstractXmlNodeModel that would ultimately write the output files
|
---|
581 | in each format.
|
---|
582 |
|
---|
583 | \section1 Security Considerations
|
---|
584 |
|
---|
585 | \section2 Code Injection
|
---|
586 |
|
---|
587 | XQuery is vulnerable to
|
---|
588 | \l{http://en.wikipedia.org/wiki/Code_injection} {code injection
|
---|
589 | attacks} in the same way as the SQL language. If an XQuery is
|
---|
590 | constructed by concatenating strings, and the strings come from
|
---|
591 | user input, the constructed XQuery could be malevolent. The best
|
---|
592 | way to prevent code injection attacks is to not construct XQueries
|
---|
593 | from user-written strings, but only accept user data input using
|
---|
594 | QVariant and variable bindings. See QXmlQuery::bindVariable().
|
---|
595 |
|
---|
596 | The articles
|
---|
597 | \l{http://www.ibm.com/developerworks/xml/library/x-xpathinjection.html}
|
---|
598 | {Avoid the dangers of XPath injection}, by Robi Sen and
|
---|
599 | \l{http://www.packetstormsecurity.org/papers/bypass/Blind_XPath_Injection_20040518.pdf}
|
---|
600 | {Blind XPath Injection}, by Amit Klein, discuss the XQuery code
|
---|
601 | injection problem in more detail.
|
---|
602 |
|
---|
603 | \section2 Denial of Service Attacks
|
---|
604 |
|
---|
605 | Applications using QtXmlPatterns are subject to the same
|
---|
606 | limitations of software as other systems. Generally, these can not
|
---|
607 | be checked. This means QtXmlPatterns does not prevent rogue
|
---|
608 | queries from consuming too many resources. For example, a query
|
---|
609 | could take too much time to execute or try to transfer too much
|
---|
610 | data. A query could also do too much recursion, which could crash
|
---|
611 | the system. XQueries can do these things accidentally, but they
|
---|
612 | can also be done as deliberate denial of service attacks.
|
---|
613 |
|
---|
614 | \section1 Features and Conformance
|
---|
615 |
|
---|
616 | \section2 XQuery 1.0
|
---|
617 |
|
---|
618 | QtXmlPatterns aims at being a
|
---|
619 | \l{http://www.w3.org/TR/xquery/#id-xquery-conformance} {conformant
|
---|
620 | XQuery processor}. It adheres to
|
---|
621 | \l{http://www.w3.org/TR/xquery/#id-minimal-conformance} {Minimal
|
---|
622 | Conformance} and supports the
|
---|
623 | \l{http://www.w3.org/TR/xquery/#id-serialization-feature}
|
---|
624 | {Serialization Feature} and the
|
---|
625 | \l{http://www.w3.org/TR/xquery/#id-full-axis-feature} {Full Axis
|
---|
626 | Feature}. QtXmlPatterns currently passes 97% of the tests in the
|
---|
627 | \l{http://www.w3.org/XML/Query/test-suite} {XML Query Test Suite}.
|
---|
628 | Areas where conformance may be questionable and where behavior may
|
---|
629 | be changed in future releases include:
|
---|
630 |
|
---|
631 | \list
|
---|
632 |
|
---|
633 | \o Some corner cases involving namespaces and element constructors
|
---|
634 | are incorrect.
|
---|
635 |
|
---|
636 | \o XPath is a subset of XQuery and the implementation of
|
---|
637 | QtXmlPatterns uses XPath 2.0 with XQuery 1.0.
|
---|
638 |
|
---|
639 | \endlist
|
---|
640 |
|
---|
641 | The specifications discusses conformance further:
|
---|
642 | \l{http://www.w3.org/TR/xquery/}{XQuery 1.0: An XML Query
|
---|
643 | Language}. W3C's XQuery testing effort can be of interest as
|
---|
644 | well, \l{http://www.w3.org/XML/Query/test-suite/}{XML Query Test
|
---|
645 | Suite}.
|
---|
646 |
|
---|
647 | Currently \c fn:collection() does not access any data set, and
|
---|
648 | there is no API for providing data through the collection. As a
|
---|
649 | result, evaluating \c fn:collection() returns the empty
|
---|
650 | sequence. We intend to provide functionality for this in a future
|
---|
651 | release of Qt.
|
---|
652 |
|
---|
653 | Only queries encoded in UTF-8 are supported.
|
---|
654 |
|
---|
655 | \section2 XSLT 2.0
|
---|
656 |
|
---|
657 | Partial support for XSLT was introduced in Qt 4.5. Future
|
---|
658 | releases of QtXmlPatterns will aim to support these XSLT
|
---|
659 | features:
|
---|
660 |
|
---|
661 | \list
|
---|
662 | \o Basic XSLT 2.0 processor
|
---|
663 | \o Serialization feature
|
---|
664 | \o Backwards Compatibility feature
|
---|
665 | \endlist
|
---|
666 |
|
---|
667 | For details, see \l{http://www.w3.org/TR/xslt20/#conformance}{XSL
|
---|
668 | Transformations (XSLT) Version 2.0, 21 Conformance}.
|
---|
669 |
|
---|
670 | \note In this release, XSLT support is considered experimental.
|
---|
671 |
|
---|
672 | Unsupported or partially supported XSLT features are documented
|
---|
673 | in the following table. The implementation of XSLT in Qt 4.5 can
|
---|
674 | be seen as XSLT 1.0 but with the data model of XPath 2.0 and
|
---|
675 | XSLT 2.0, and using the using the functionality of XPath 2.0 and
|
---|
676 | its accompanying function library. When QtXmlPatterns encounters
|
---|
677 | an unsupported or partially support feature, it will either report
|
---|
678 | a syntax error or silently continue, unless otherwise noted in the
|
---|
679 | table.
|
---|
680 |
|
---|
681 | The implementation currently passes 42% of W3C's XSLT test suite,
|
---|
682 | which focus on features introduced in XSLT 2.0.
|
---|
683 |
|
---|
684 | \table
|
---|
685 | \header
|
---|
686 | \o XSL Feature
|
---|
687 | \o Support Status
|
---|
688 | \row
|
---|
689 | \o \c xsl:key and \c fn:key()
|
---|
690 | \o not supported
|
---|
691 | \row
|
---|
692 | \o \c xsl:include
|
---|
693 | \o not supported
|
---|
694 | \row
|
---|
695 | \o \c xsl:import
|
---|
696 | \o not supported
|
---|
697 | \row
|
---|
698 | \o \c xsl:copy
|
---|
699 |
|
---|
700 | \o The \c copy-namespaces and \c inherit-namespaces attributes
|
---|
701 | have no effect. For copied comments, attributes and
|
---|
702 | processing instructions, the copy has the same node
|
---|
703 | identity as the original.
|
---|
704 |
|
---|
705 | \row
|
---|
706 | \o \c xsl:copy-of
|
---|
707 | \o The \c copy-namespaces attribute has no effect.
|
---|
708 | \row
|
---|
709 | \o \c fn:format-number()
|
---|
710 | \o not supported
|
---|
711 | \row
|
---|
712 | \o \c xsl:message
|
---|
713 | \o not supported
|
---|
714 | \row
|
---|
715 | \o \c xsl:use-when
|
---|
716 | \o not supported
|
---|
717 | \row
|
---|
718 | \o \c Tunnel Parameters
|
---|
719 | \o not supported
|
---|
720 | \row
|
---|
721 | \o \c xsl:attribute-set
|
---|
722 | \o not supported
|
---|
723 | \row
|
---|
724 | \o \c xsl:decimal-format
|
---|
725 | \o not supported
|
---|
726 | \row
|
---|
727 | \o \c xsl:fallback
|
---|
728 | \o not supported
|
---|
729 | \row
|
---|
730 | \o \c xsl:apply-imports
|
---|
731 | \o not supported
|
---|
732 | \row
|
---|
733 | \o \c xsl:character-map
|
---|
734 | \o not supported
|
---|
735 | \row
|
---|
736 | \o \c xsl:number
|
---|
737 | \o not supported
|
---|
738 | \row
|
---|
739 | \o \c xsl:namespace-alias
|
---|
740 | \o not supported
|
---|
741 | \row
|
---|
742 | \o \c xsl:output
|
---|
743 | \o not supported
|
---|
744 | \row
|
---|
745 | \o \c xsl:output-character
|
---|
746 | \o not supported
|
---|
747 | \row
|
---|
748 | \o \c xsl:preserve-space
|
---|
749 | \o not supported
|
---|
750 | \row
|
---|
751 | \o \c xsl:result-document
|
---|
752 | \o not supported
|
---|
753 | \row
|
---|
754 | \o Patterns
|
---|
755 | \o Complex patterns or patterns with predicates have issues.
|
---|
756 | \row
|
---|
757 | \o \c 2.0 Compatibility Mode
|
---|
758 |
|
---|
759 | \o Stylesheets are interpreted as XSLT 2.0 stylesheets, even
|
---|
760 | if the \c version attribute is in the XSLT source is
|
---|
761 | 1.0. In other words, the version attribute is ignored.
|
---|
762 |
|
---|
763 | \row
|
---|
764 | \o Grouping
|
---|
765 |
|
---|
766 | \o \c fn:current-group(), \c fn:grouping-key() and \c
|
---|
767 | xsl:for-each-group.
|
---|
768 |
|
---|
769 | \row
|
---|
770 | \o Regexp elements
|
---|
771 | \o \c xsl:analyze-string, \c xsl:matching-substring,
|
---|
772 | \c xsl:non-matching-substring, and \c fn:regex-group()
|
---|
773 | \row
|
---|
774 | \o Date & Time formatting
|
---|
775 | \o \c fn:format-dateTime(), \c fn:format-date() and fn:format-time().
|
---|
776 |
|
---|
777 | \row
|
---|
778 | \o XPath Conformance
|
---|
779 | \o Since XPath is a subset of XSLT, its issues are in affect too.
|
---|
780 | \endtable
|
---|
781 |
|
---|
782 | The QtXmlPatterns implementation of the XPath Data Model does not
|
---|
783 | include entities (due to QXmlStreamReader not reporting them).
|
---|
784 | This means that functions \c unparsed-entity-uri() and \c
|
---|
785 | unparsed-entity-public-id() always return negatively.
|
---|
786 |
|
---|
787 | \section2 XPath 2.0
|
---|
788 |
|
---|
789 | Since XPath 2.0 is a subset of XQuery 1.0, XPath 2.0 is
|
---|
790 | supported. Areas where conformance may be questionable and,
|
---|
791 | consequently, where behavior may be changed in future releases
|
---|
792 | include:
|
---|
793 |
|
---|
794 | \list
|
---|
795 | \o Regular expression support is currently not conformant
|
---|
796 | but follows Qt's QRegExp standard syntax.
|
---|
797 |
|
---|
798 | \o Operators for \c xs:time, \c xs:date, and \c xs:dateTime
|
---|
799 | are incomplete.
|
---|
800 |
|
---|
801 | \o Formatting of very large or very small \c xs:double, \c
|
---|
802 | xs:float, and \c xs:decimal values may be incorrect.
|
---|
803 | \endlist
|
---|
804 |
|
---|
805 | \section2 xml:id
|
---|
806 |
|
---|
807 | Processing of XML files supports \c xml:id. This allows elements
|
---|
808 | that have an attribute named \c xml:id to be looked up efficiently
|
---|
809 | with the \c fn:id() function. See
|
---|
810 | \l{http://www.w3.org/TR/xml-id/}{xml:id Version 1.0} for details.
|
---|
811 |
|
---|
812 | \section2 XML Schema 1.0
|
---|
813 |
|
---|
814 | There are two ways QtXmlPatterns can be used to validate schemas:
|
---|
815 | You can use the C++ API in your Qt application using the classes
|
---|
816 | QXmlSchema and QXmlSchemaValidator, or you can use the command line
|
---|
817 | utility named xmlpatternsvalidator (located in the "bin" directory
|
---|
818 | of your Qt build).
|
---|
819 |
|
---|
820 | The QtXmlPatterns implementation of XML Schema validation supports
|
---|
821 | the schema specification version 1.0 in large parts. Known problems
|
---|
822 | of the implementation and areas where conformancy may be questionable
|
---|
823 | are:
|
---|
824 |
|
---|
825 | \list
|
---|
826 | \o Large \c minOccurs or \c maxOccurs values or deeply nested ones
|
---|
827 | require huge amount of memory which might cause the system to freeze.
|
---|
828 | Such a schema should be rewritten to use \c unbounded as value instead
|
---|
829 | of large numbers. This restriction will hopefully be fixed in a later release.
|
---|
830 | \o Comparison of really small or large floating point values might lead to
|
---|
831 | wrong results in some cases. However such numbers should not be relevant
|
---|
832 | for day-to-day usage.
|
---|
833 | \o Regular expression support is currently not conformant but follows
|
---|
834 | Qt's QRegExp standard syntax.
|
---|
835 | \o Identity constraint checks can not use the values of default or fixed
|
---|
836 | attribute definitions.
|
---|
837 | \endlist
|
---|
838 |
|
---|
839 | \section2 Resource Loading
|
---|
840 |
|
---|
841 | When QtXmlPatterns loads an XML resource, e.g., using the
|
---|
842 | \c fn:doc() function, the following schemes are supported:
|
---|
843 |
|
---|
844 | \table
|
---|
845 | \header
|
---|
846 | \o Scheme Name
|
---|
847 | \o Description
|
---|
848 | \row
|
---|
849 | \o \c file
|
---|
850 | \o Local files.
|
---|
851 | \row
|
---|
852 | \o \c data
|
---|
853 |
|
---|
854 | \o The bytes are encoded in the URI itself. e.g., \c
|
---|
855 | data:application/xml,%3Ce%2F%3E is \c <e/>.
|
---|
856 |
|
---|
857 | \row
|
---|
858 | \o \c ftp
|
---|
859 | \o Resources retrieved via FTP.
|
---|
860 | \row
|
---|
861 | \o \c http
|
---|
862 | \o Resources retrieved via HTTP.
|
---|
863 | \row
|
---|
864 | \o \c https
|
---|
865 | \o Resources retrieved via HTTPS. This will succeed if no SSL
|
---|
866 | errors are encountered.
|
---|
867 | \row
|
---|
868 | \o \c qrc
|
---|
869 | \o Qt Resource files. Expressing it as an empty scheme, :/...,
|
---|
870 | is not supported.
|
---|
871 |
|
---|
872 | \endtable
|
---|
873 |
|
---|
874 | \section2 XML
|
---|
875 |
|
---|
876 | XML 1.0 and XML Namespaces 1.0 are supported, as opposed to the
|
---|
877 | 1.1 versions. When a strings is passed to a query as a QString,
|
---|
878 | the characters must be XML 1.0 characters. Otherwise, the behavior
|
---|
879 | is undefined. This is not checked.
|
---|
880 |
|
---|
881 | URIs are first passed to QAbstractUriResolver. Check
|
---|
882 | QXmlQuery::setUriResolver() for possible rewrites.
|
---|
883 | */
|
---|
884 |
|
---|
885 | /*!
|
---|
886 | \namespace QPatternist
|
---|
887 | \brief The QPatternist namespace contains classes and functions required by the QtXmlPatterns module.
|
---|
888 | \internal
|
---|
889 | */
|
---|