(: 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
:)
(: 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> :)
(: 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>
:)
(: 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
(...)
:)