source: trunk/doc/src/snippets/code/doc_src_qtxmlpatterns.qdoc@ 865

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

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

File size: 6.9 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:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41
42void wrapInFunction()
43{
44
45//! [0]
46#include <QtXmlPatterns>
47//! [0]
48
49
50//! [1]
51QT += xmlpatterns
52//! [1]
53
54//! [2]
55xmlpatterns myQuery.xq
56//! [2]
57
58//! [3]
59declare namespace c = "http://cookbook/namespace";
60doc('cookbook.xml')//c:recipe/c:title
61//! [3]
62
63//! [4]
64declare default element namespace "http://cookbook/namespace";
65doc('cookbook.xml')//recipe/title
66//! [4]
67
68//! [5]
69<title xmlns="http://cookbook/namespace">Quick and Easy Mushroom Soup</title>
70<title xmlns="http://cookbook/namespace">Cheese on Toast</title>
71<title xmlns="http://cookbook/namespace">Hard-Boiled Eggs</title>
72//! [5]
73
74//! [6]
75xmlpatterns file.xq
76//! [6]
77
78//! [7]
79doc('cookbook.xml')//@xml:*
80//! [7]
81
82//! [8]
83doc('cookbook.xml')//@*:name
84//! [8]
85
86//! [9]
87declare default element namespace "http://cookbook/namespace";
88doc('cookbook.xml')/cookbook/@*
89//! [9]
90
91//! [10]
92declare default element namespace "http://cookbook/namespace";
93doc("cookbook.xml")/cookbook/recipe[title = "Hard-Boiled Eggs"]
94//! [10]
95
96//! [11]
97declare default element namespace "http://cookbook/namespace";
98doc('cookbook.xml')//method[string-length(.) = 0]
99//! [11]
100
101//! [12]
102declare default element namespace "http://cookbook/namespace";
103doc('cookbook.xml')//method[string-length() = 0]
104//! [12]
105
106//! [13]
107declare default element namespace "http://cookbook/namespace";
108doc('cookbook.xml')/cookbook/recipe[2]
109//! [13]
110
111//! [14]
112declare default element namespace "http://cookbook/namespace";
113doc('cookbook.xml')/cookbook/recipe[position() = 2]
114//! [14]
115
116//! [15]
117declare default element namespace "http://cookbook/namespace";
118doc('cookbook.xml')/cookbook/recipe[position() > 1]
119//! [15]
120
121//! [16]
122declare default element namespace "http://cookbook/namespace";
123doc('cookbook.xml')/cookbook/recipe[last()]
124//! [16]
125
126//! [17]
127declare default element namespace "http://cookbook/namespace";
128doc('cookbook.xml')/cookbook/recipe[last() - 1]
129//! [17]
130
131//! [18]
132doc('cookbook.xml')//recipe
133//! [18]
134
135//! [19]
136doc('cookbook.xml')//recipe/title
137//! [19]
138
139//! [20]
140<recipe/>
141//! [20]
142
143//! [21]
144 <html xmlns="http://www.w3.org/1999/xhtml/"
145 xml:id="{doc("other.html")/html/@xml:id}"/>
146//! [21]
147
148//! [22]
149doc('cookbook.xml')/descendant-or-self::element(recipe)/child::element(title)
150//! [22]
151
152//! [23]
153<cookbook>
154//! [23]
155
156//! [24]
157<cookbook xmlns="http://cookbook/namespace">
158//! [24]
159
160//! [25]
161for $i in doc("cookbook.xml")//@xml:*
162return <p>{$i}</p>
163//! [25]
164
165//! [26]
166for $i in doc("cookbook.xml")//@*:name
167return <p>{$i}</p>
168//! [26]
169
170//! [27]
171declare default element namespace "http://cookbook/namespace";
172for $i in doc("cookbook.xml")/cookbook/@*
173return <p>{$i}</p>
174//! [27]
175
176//! [28]
177<p xml:id="MushroomSoup"/>
178<p xml:id="CheeseOnToast"/>
179<p xml:id="HardBoiledEggs"/>
180//! [28]
181
182//! [29]
183<p name="Fresh mushrooms"/>
184<p name="Garlic"/>
185<p name="Olive oil"/>
186<p name="Milk"/>
187<p name="Water"/>
188<p name="Cream"/>
189<p name="Vegetable soup cube"/>
190<p name="Ground black pepper"/>
191<p name="Dried parsley"/>
192<p name="Bread"/>
193<p name="Cheese"/>
194<p name="Eggs"/>
195//! [29]
196
197//! [30]
198<p xmlns="http://cookbook/namespace" count="3"/>
199//! [30]
200
201//! [31]
202<method xmlns="http://cookbook/namespace"/>
203//! [31]
204
205//! [32]
206declare default element namespace "http://cookbook/namespace";
207doc('cookbook.xml')//recipe[string-length(method) = 0]
208//! [32]
209
210//! [33]
211<recipe xmlns="http://cookbook/namespace" xml:id="HardBoiledEggs">
212 <title>Hard-Boiled Eggs</title>
213 <ingredient name="Eggs" quantity="3" unit="eggs"/>
214 <time quantity="3" unit="minutes"/>
215 <method/>
216</recipe>
217//! [33]
218
219//! [34]
220declare default element namespace "http://cookbook/namespace";
221doc('cookbook.xml')/cookbook/recipe[method[empty(step)]]
222//! [34]
223
224//! [35]
225declare default element namespace "http://cookbook/namespace";
226doc('cookbook.xml')/cookbook/recipe[not(normalize-space(method))]
227//! [35]
228
229//! [36]
230<e>{sum((1, 2, 3))}</e>
231//! [36]
232
233//! [37]
234<e>6</e>
235//! [37]
236
237//![38]
238declare variable $insertion := "example";
239<p class="important {$insertion} obsolete"/>
240//![38]
241
242//! [39]
243<p class="important example obsolete"/>
244//! [39]
245
246//! [40]
247declare default element namespace "http://cookbook/namespace";
248let $docURI := 'cookbook.xml'
249return if(doc-available($docURI))
250 then doc($docURI)//recipe/<oppskrift>{./node()}</oppskrift>
251 else <oppskrift>Failed to load {$docURI}</oppskrift>
252//! [40]
253
254//! [41]
255<span>1</span>
256<span>3</span>
257<span>5</span>
258<span>7</span>
259<span>9</span>
260<span>b</span>
261<span>d</span>
262<span>f</span>
263//! [41]
264
265//! [42]
266<span>1</span>
267//! [42]
268
269//! [43]
270let $doc := doc('doc.txt')
271return $doc/doc/p[1]/span[1]
272//! [43]
273
274//! [44]
275for $a in doc('doc.txt')/doc/p/span[1]
276return $a
277//! [44]
278
279//! [45]
280doc('doc.txt')/doc/p/span[1]
281//! [45]
282
283//! [46]
284doc('doc.txt')//p/<p>{span/node()}</p>
285//! [46]
286
287//! [47]
288<p>78</p>
289<p>9a</p>
290<p>12</p>
291<p>bc</p>
292<p>de</p>
293<p>34</p>
294<p>56</p>
295<p>f0</p>
296//! [47]
297
298//! [48]
299for $a in doc('doc.txt')//p
300 return <p>{$a/span/node()}</p>
301//! [48]
302
303//! [49]
304<p>12</p>
305<p>34</p>
306<p>56</p>
307<p>78</p>
308<p>9a</p>
309<p>bc</p>
310<p>de</p>
311<p>f0</p>
312//! [49]
313
314}
315
Note: See TracBrowser for help on using the repository browser.