XQuery Exercises
Jump to navigation
Jump to search
Good practices
(: GOLDEN RULES
-1. KYDB - Know Your Database
0. Divide issue into smaller parts
1. Do one part at a time, step by step
2. KISS (Keep It Simple Stupid)
3. Choose proper source(s) from the context
4. If something can be solved with Xpath only, then don't use FLOWR (or even no XQuery at all)
5. One change at a time, then test it
6. When you're stuck - use documentation (main original source first, then stackoverflow, etc)
7. Do not invent the wheel - custom function as the last resort, first look for the built-in or ready solution
:)
XQuery Exercises
- Paths, Constructors
- exerciseBooks.xq
- exercisePredicates1.xq
- Constructors, sub-expressions, sequences, quantified
- exercise7.3.xq
- exercise7.3_1.xq
- exercise7.5.xq (some ... satisfies)
- exercise7.6.xq (every ... satisfies)
- exercise7.7.xq (nested loop)
- exercise7.8.xq (order by, union)
- exercise7.9.xq
- Joins, Constructors, Html
- 3wJoinExercise.xq
- exercise3.xq
- exercise3html.xq
- exercise4.xq
- exercise7.4.xq
- Average
- exercise7.10.xq
- exercise7.10a.xq
- exercise7.10b.xq
- Functions
- exercise5_1.xq
- exercise5_2.xq
- exercise6.xq
- exercise7.11.xq (some ... satisfies)
- exercise7.9b.xq
- empty and absent (exercises, library)
- Group by (Xquery 1.0)
(: Example :)
(: <!-- Order input document -->
<order num="00299432" date="2006-09-15" cust="0221A">
<item dept="WMN" num="557" quantity="1" color="navy"/>
<item dept="ACC" num="563" quantity="1"/>
<item dept="ACC" num="443" quantity="2"/>
<item dept="MEN" num="784" quantity="1" color="white"/>
<item dept="MEN" num="784" quantity="1" color="gray"/>
<item dept="WMN" num="557" quantity="1" color="black"/>
</order> :)
(: Group by 'dept' :)
(: for $d in distinct-values(doc("order.xml")//item/@dept)
let $items := doc("order.xml")//item[@dept = $d]
order by $d
return <department code="{$d}">{
for $i in $items
order by $i/@num
return $i
}</department> :)
(: Exercise :)
(: Src file - 'exercises.xml' :)
(: Group by 'difficulty' :)
(: Expecting result
<difficulty kind="advanced">
<answer>Me (-;</answer>
</difficulty>
<difficulty kind="guru">
<answer>Everything</answer>
</difficulty>
<difficulty kind="medium">
<answer>Alan Turing</answer>
<answer/>
</difficulty>
<difficulty kind="standard">
<answer>30</answer>
<answer>7</answer>
</difficulty> :)
More exercises
- Regexp
- Date and time
Regexp
(: Example :)
(: let $input := 'Hello World'
return
<result>{
(matches($input, 'Hello World') = true(),
matches($input, 'Hi') = false(),
matches($input, 'H.*') = true(),
matches($input, 'H.*o W.*d') = true(),
matches($input, 'Hel+o? W.+d') = true(),
matches($input, 'Hel?o+') = false(),
matches($input, 'hello', "i") = true(),
matches($input, 'he l lo', "ix") = true() ,
matches($input, '^Hello$') = false(),
matches($input, '^Hello') = true()
)}
</result> :)
(: EXERCISE - file 'svnlist.xml' :)
(: Find all entries, which have no 'logo' in their names :)
(: Result
<result>
<entry kind="file">
<name>jquery-1.9.1.js</name>
<size>268381</size>
<commit revision="1">
<author>lsokolowski</author>
<date>2015-07-01T23:50:07.421903Z</date>
</commit>
</entry>
<entry kind="file">
<name>jquery.cycle.all.js</name>
<size>51734</size>
<commit revision="1">
<author>lsokolowski</author>
<date>2015-07-01T23:50:07.421903Z</date>
</commit>
</entry>
</result>
:)
Dates and time
(: Example :)
(: xs:dateTime("2015-04-11T09:23:30.5") - xs:dateTime("2015-04-04T02:15:10.2") :)
(: Exercise :)
(: Get dates from 'svnlist.xml' and find out how old are the entries. :)
(: Result
P2023DT14H32M29.416097S
P2023DT14H32M29.416097S
P2023DT14H32M29.416097S
(...)
:)
Miscelaneous
- exerciseUSplcodes.xq
- oraclExercise2.xq
- exercisesElkomtech.xq