source: trunk/doc/src/xml-processing/xml-patterns.qdoc@ 1115

Last change on this file since 1115 was 846, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 31.7 KB
Line 
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