1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51//! [0]
52 QXmlNamePool namePool(query.namePool());
53 query.bindVariable(name: QXmlName(namePool, localName), value);
54//! [0]
55
56
57{
58//! [1]
59 QByteArray myDocument;
60 QBuffer buffer(&myDocument); // This is a QIODevice.
61 buffer.open(openMode: QIODevice::ReadOnly);
62 QXmlQuery query;
63 query.bindVariable(localName: "myDocument", &buffer);
64 query.setQuery(sourceCode: "doc($myDocument)");
65//! [1]
66}
67
68
69{
70 QIODevice *device = 0;
71//! [2]
72 QXmlNamePool namePool(query.namePool());
73 query.bindVariable(name: QXmlName(namePool, localName), device);
74//! [2]
75
76}
77
78{
79 QIODevice *myOutputDevice = 0;
80//! [3]
81 QFile xq("myquery.xq");
82
83 QXmlQuery query;
84 query.setQuery(sourceCode: &xq, documentURI: QUrl::fromLocalFile(localfile: xq.fileName()));
85
86 QXmlSerializer serializer(query, myOutputDevice);
87 query.evaluateTo(callback: &serializer);
88//! [3]
89}
90
91{
92 QIODevice *myOutputDevice = 0;
93//! [4]
94 QFile xq("myquery.xq");
95 QString fileName("the filename");
96 QString publisherName("the publisher");
97 qlonglong year = 1234;
98
99 QXmlQuery query;
100
101 query.bindVariable(localName: "file", value: QVariant(fileName));
102 query.bindVariable(localName: "publisher", value: QVariant(publisherName));
103 query.bindVariable(localName: "year", value: QVariant(year));
104
105 query.setQuery(sourceCode: &xq, documentURI: QUrl::fromLocalFile(localfile: xq.fileName()));
106
107 QXmlSerializer serializer(query, myOutputDevice);
108 query.evaluateTo(callback: &serializer);
109//! [4]
110}
111
112{
113//! [5]
114 QFile xq("myquery.xq");
115 QString fileName("the filename");
116 QString publisherName("the publisher");
117 qlonglong year = 1234;
118
119 QXmlQuery query;
120
121 query.bindVariable(localName: "file", value: QVariant(fileName));
122 query.bindVariable(localName: "publisher", value: QVariant(publisherName));
123 query.bindVariable(localName: "year", value: QVariant(year));
124
125 query.setQuery(sourceCode: &xq, documentURI: QUrl::fromLocalFile(localfile: xq.fileName()));
126
127 QXmlResultItems result;
128 query.evaluateTo(result: &result);
129 QXmlItem item(result.next());
130 while (!item.isNull()) {
131 if (item.isAtomicValue()) {
132 QVariant v = item.toAtomicValue();
133 switch (v.type()) {
134 case QVariant::LongLong:
135 // xs:integer
136 break;
137 case QVariant::String:
138 // xs:string
139 break;
140 default:
141 // error
142 break;
143 }
144 }
145 else if (item.isNode()) {
146#ifdef qdoc
147 QXmlNodeModelIndex i = item.toNodeModelIndex();
148 // process node
149#endif // qdoc
150 }
151 item = result.next();
152 }
153//! [5]
154}
155
156{
157//! [6]
158 QFile xq("myquery.xq");
159
160 QXmlQuery query;
161 query.setQuery(sourceCode: &xq, documentURI: QUrl::fromLocalFile(localfile: xq.fileName()));
162
163 QXmlResultItems result;
164 query.evaluateTo(result: &result);
165 QXmlItem item(result.next());
166 while (!item.isNull()) {
167 if (item.isAtomicValue()) {
168 QVariant v = item.toAtomicValue();
169 switch (v.type()) {
170 case QVariant::LongLong:
171 // xs:integer
172 break;
173 case QVariant::String:
174 // xs:string
175 break;
176 default:
177 if (v.userType() == qMetaTypeId<QXmlName>()) {
178#ifdef qdoc
179 QXmlName n = qvariant_cast<QXmlName>(v);
180 // process QXmlName n...
181#endif // qdoc
182 }
183 else {
184 // error
185 }
186 break;
187 }
188 }
189 else if (item.isNode()) {
190#ifdef qdoc
191 QXmlNodeModelIndex i = item.toNodeModelIndex();
192 // process node
193#endif // qdoc
194 }
195 item = result.next();
196 }
197//! [6]
198}
199
200{
201 QIODevice *out = 0;
202//! [7]
203 QXmlQuery query(QXmlQuery::XSLT20);
204 query.setFocus(QUrl("myInput.xml"));
205 query.setQuery(queryURI: QUrl("myStylesheet.xsl"));
206 query.evaluateTo(target: out);
207//! [7]
208}
209

source code of qtxmlpatterns/src/xmlpatterns/doc/snippets/code/src_xmlpatterns_api_qxmlquery.cpp