1. XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
XQuery Tutorial
XQuery Type
System
Basics
Tricks Vyacheslav Zholudev
Pitfalls
XQuery in Jacobs University Bremen
TNTBase
KWARC – Knowledge Adaptation and Reasoning for Content
Conclusion
September 17, 2010
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 1
2. What is XQuery?
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
XQuery Type XQuery is an XML-database query language
System
Basics Like SQL for Relational databases
Tricks Not only for XML files
Pitfalls
W3C Recommendation
XQuery in
TNTBase
Side-effect free
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 2
3. For those who know XSLT
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction XQuery and XSLT both:
XQuery Type
System
Take XML as their input and produce XML as their
Basics output
Tricks Have the same data model and type system
Pitfalls
Are declarative and meant to be free from side effects
XQuery in
TNTBase
Share XPath, having the same library of built-in
Conclusion
functions
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 3
4. Basic Rules
XQuery
Tutorial
Vyacheslav
Zholudev
XQuery is case-sensitive
Introduction
XQuery Type
XQuery elements, attributes, and variables must be
System valid XML names
Basics
An XQuery string value can be in single or double
Tricks
quotes (’foo’ or “foo”)
Pitfalls
XQuery in An XQuery variable is defined with a $ followed by a
TNTBase
name, e.g. $theory
Conclusion
XQuery comments are delimited by (: and :), e.g.
( : This XQuery generates an omdoc document. :)
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 4
5. XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
XQuery Type
System
Basics
Tricks
Pitfalls
Type System
XQuery in
TNTBase
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 5
6. XQuery types
XQuery
Tutorial
Taken from XML Schema
Vyacheslav
Zholudev
Introduction
XQuery Type
System
Basics
Tricks
Pitfalls
XQuery in
TNTBase
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 6
7. Sequences in XQuery
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction Ordered list of objects
XQuery Type
System
List of sequences is one sequence
Basics One element is a one-element sequence
Tricks
Pitfalls
Examples:
XQuery in (“a”, “b”, “c”)
TNTBase
Conclusion
((“a”, “b”), “c”)
((“a”, “b”), (“c”, ()))
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 7
8. XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
XQuery Type
System
Basics
Tricks
Pitfalls
XQuery in “5 minutes”
XQuery in
TNTBase
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 8
10. XPath Expressions
XQuery
Tutorial
Selecting nodes using XPath:
Vyacheslav doc(’ http: // tntbase.org/sets.omdoc’)/omdoc/theory/notation
Zholudev
OR
Introduction doc(’ http: // tntbase.org/sets.omdoc’)/omdoc//notation
XQuery Type <notation xmlns="http: // omdoc.org/ns" cd="sets−operations" name="eset">
System
<prototype>
Basics <om:OMS cd="sets−operations" name="eset"/>
Tricks </prototype>
Pitfalls
<rendering>
<m:mi mathvariant="normal">X</m:mi>
XQuery in
TNTBase
</rendering>
</notation>
Conclusion <notation cd="sets−operations" name="powerset">
<prototype>
<om:OMS cd="sets−operations" name="powerset"/>
</prototype>
...
</notation>
... <!−− More results −−>
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 10
11. Predicates
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
XQuery Type doc(’ http: // tntbase.org/sets.omdoc’)//notation [3]
System
Basics doc(’ http: // tntbase.org/sets.omdoc’)//notation[ position () <4]
Tricks doc(’ http: // tntbase.org/sets.omdoc’)//notation[@cd=’sets−operations’]
Pitfalls
doc(’ http: // tntbase.org/sets.omdoc’)//notation[prototype]
XQuery in
TNTBase doc (...)// notation [prototype/om:OMS/@name="eset"]
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 11
12. FLOWR Expressions
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
FOR
XQuery Type
System
LET
Basics
Tricks
WHERE
Pitfalls ORDER BY
XQuery in
TNTBase
RETURN
Conclusion
Note: there is no GROUP BY in XQuery
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 12
13. LET
XQuery
Tutorial
Vyacheslav
Variables are prefixed with $
Zholudev
Query:
Introduction
let $a := 3
XQuery Type
System
let $b := 4
return
Basics
$a + $b
Tricks
Pitfalls Result: 7
XQuery in
TNTBase Query:
Conclusion 1 let $x := (<foo><bar/><bla/></foo>, <foo><baz/></foo>)
return
$x/∗ ( : all children elements :)
Result: (<bar/>, <bla/>, <baz/>)
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 13
14. FOR loop
XQuery
Tutorial Syntax:
Vyacheslav
Zholudev for $var in $sequence
where <condition>
Introduction return <result>
XQuery Type
System
Examples:
Basics
Tricks for $notation in doc(’ http: // tntbase.org/sets.omdoc’)//notation
2 where $notation//∗[@name="eset"]
Pitfalls
return $notation/rendering
XQuery in
TNTBase
Conclusion let $numbers := (2, 23, 1, 32, 8, 13, −1)
2 return
for $num in $numbers
where $num > 5
order by $num
return −($num + 100)
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 14
15. Wrapping results
XQuery
Tutorial let $ntns := tnt:doc ( ’ / slides /dmath/en/sets−operations.omdoc’)//notation
return
Vyacheslav
Zholudev <notations>
4 <amount>{count($ntns)}</amount>
Introduction <renderings>
{
XQuery Type
System for $ntn in $ntns return
<my−rend>{$ntn/rendering}</my−rend>
Basics
9 }
Tricks </renderings>
Pitfalls </notations>
XQuery in
TNTBase Result:
Conclusion <notations>
<amount>38</amount>
<renderings>
4 <my−rend><rendering"><m:mi mathvariant="normal">X</m:mi></rendering>
</my−rend>
.... <!−− more results −−>
</renderings>
</notations>
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 15
16. IF Expression
XQuery
Tutorial
Vyacheslav
Zholudev
Note: "else" clause is mandatory
Introduction Syntax:
XQuery Type
System if <condition> then $seq1 else $seq2
Basics
Tricks Example:
Pitfalls for $i in (1, " str " , 3, <foo/>) return
XQuery in if ($ i instance of element()) then "element"
TNTBase else if ($ i instance of xs:string ) then " string "
Conclusion 4 else "other"
Result: ( "other" , " string " , "other" , "element")
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 16
17. Comparisons
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
General comparisons: =, !=, <, <=, >, >=
XQuery Type
System Value comparisons: eq, ne, lt, le, gt, ge
Basics
Tricks
Value comparisons operate only on single elements
Pitfalls (otherwise, error)
XQuery in
TNTBase (1, 2, 4) = (10, 2, 8) returns true
Conclusion (1, 2, 4) eq (10, 2, 8) throws an error
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 17
18. Axes
XQuery
Tutorial
Forward Axes:
Vyacheslav child (/*)
Zholudev
descendant (//*)
Introduction
attribute (/@*)
XQuery Type
System self (/.)
Basics descendant-or-self
Tricks
following-sibling
Pitfalls
XQuery in
following
TNTBase
Reverse Axes
Conclusion
parent (/..)
ancestor
preceding-sibling
preceding
ancestor-or-self
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 18
19. Functions
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
Built-in Functions + User-defined functions
XQuery Type
System Built-in functions examples:
Basics fn:count($seq) as xs:double
Tricks
fn:empty($seq) as xs:boolean
Pitfalls
XQuery in fn:uppercase($str as xs:string ) as xs:string
TNTBase
fn:substring ($ str , $pos) as xs:string
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 19
20. User-defined functions
XQuery
Tutorial declare function prefix:function_name($parameter AS datatype)
Vyacheslav
AS returnDatatype
Zholudev {
... function code here...
Introduction 5 }
XQuery Type
System Example:
Basics
declare namespace omdoc = "http://omdoc.org";
Tricks
( : This function returns amounts of notations in OMDoc documents :)
Pitfalls declare function omdoc:notations−amount($docs as document−node()∗)
XQuery in as xs:double∗
TNTBase 5 {
Conclusion
for $d in $docs return count($d//notation)
}
Invocation:
omdoc:notations−amount((doc("/doc1.omdoc"), doc("doc2.omdoc")))
Note: Types are optional, but highly recommended
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 20
21. XQuery modules
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
An XQuery module is a set of user-defined functions
XQuery Type
System
Every function should be in the module namespace
Basics
Tricks
Module declaration: module namespace tnt="http://tntbase.org/ns";
Pitfalls Modules can’t contain expressions
XQuery in
TNTBase
Modules can import other modules, the latter will not be
Conclusion visible for those who import the former module
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 21
22. Importing a module
XQuery
Tutorial
Vyacheslav Syntax:
Zholudev
import module namespace <prefix>= "<ns_uri>"(at "<hint_for_a_processor";)
Introduction Example:
XQuery Type
System import module namespace omdoc="http://omdoc.org/ns" at
tntbase: /modules/omdoc−module.xq;
Basics
let $doc−names := (..., ..., ...)
Tricks for $doc in $doc−names return
Pitfalls 5 omdoc:get−notations($doc)
XQuery in
TNTBase
The influence of the “at” keyword is
Conclusion
implementation-dependent
Note: Having multiple modules with the same
namespace is a bad practice and have
non-deterministic behaviour
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 22
23. XQuery Prolog
XQuery
Tutorial
is a series of declarations and imports that define the
Vyacheslav
Zholudev processing environment (separated by a semicolon)
Useful examples:
Introduction
XQuery Type Module imports (described before)
System
Basics
Namespace declarations:
Tricks declare namespace tnt="http://tntbase.org/ns";
Pitfalls declare default element namespace "http://omdoc.org/ns"
XQuery in
TNTBase
Variable declarations:
Conclusion
declare variable $NUM := 10;
Function declarations (described before)
declare function local:test () as xs:string external ;
Module declaration (described before)
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 23
24. XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
XQuery Type
System
Basics
Tricks
Pitfalls
XQuery Tricks
XQuery in
TNTBase
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 24
25. Eliminating FOR
XQuery
Tutorial
Vyacheslav
Zholudev
declare function omdoc:notations−amount($docs as document−node()∗)
Introduction
as xs:double
XQuery Type {
System
for $d in $docs return count($d//notation)
Basics 5 }
Tricks
Pitfalls declare function omdoc:notations−amount($docs as document−node()∗)
XQuery in as xs:double
TNTBase {
Conclusion $docs/count(.// notation)
5 }
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 25
26. Default namespace
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
We can define default namespace:
XQuery Type
System declare default element namespace "http://omdoc.org/ns"
Basics How to address nodes in no namespace?
Tricks
declare default element namespace "http://omdoc.org/ns";
Pitfalls declare namespace no−ns = "";
XQuery in
TNTBase tnt:doc ( ’ /sets−operations.omdoc’)/omdoc/theory[1]/no−ns:extra−element
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 26
27. Preserving XML-formatting
XQuery
Tutorial
Query:
Vyacheslav 1 <foo>
Zholudev <bar/>
</foo>
Introduction
XQuery Type Result by default:
System
Basics <foo><bar/></foo>
Tricks
New query:
Pitfalls
declare boundary−space preserve;
XQuery in
TNTBase <foo>
<bar/>
Conclusion
4 </foo>
Result:
1 <foo>
<bar/>
</foo>
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 27
28. XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
XQuery Type
System
Basics
Tricks
Pitfalls
Typical Pitfalls
XQuery in
TNTBase
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 28
29. Pitfalls
XQuery
Tutorial
Vyacheslav
Zholudev
$seq1 != $seq2, but meant: not($seq1 == $seq2)
Introduction
XQuery Type foo[bar is null ] instead of foo[empty(bar)]
System
Basics
// foo[bar < 5 and bar > 1] vs. // foo[bar [. < 5 and . > 1]]
Tricks In IF clause you always need ELSE
Pitfalls
There are no reserved words “true” and “false”. Use
XQuery in
TNTBase built-in functions fn:true() and fn:false().
Conclusion One can’t return <foo/> <bar/>.
Should return a sequence: (<foo/>, <bar/>)
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 29
30. Document collections
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
XQuery Type
Document from HEAD:
System
tnt:doc ( ’ / slides /dmath/en/sets−operations.omdoc’)
Basics
Tricks
Collection from HEAD:
Pitfalls
tnt:collection ( ’ / slides // en/∗sets∗.omdoc’)
XQuery in Document from previous revisions:
TNTBase
tnt:doc ( ’ / slides /dmath/en/sets−operations.omdoc’, 1397)
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 30
31. Repository information
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction The last revision number:
XQuery Type tnt:last −revnum() as xs:integer
System
Basics
Revisions in which the file has been changed:
Tricks
tnt:file −revs($path as xs:string) as xs:integer∗
Pitfalls Get a document in the uncommitted revision:
XQuery in tnt:txn −doc($path as xs:string, $svntxn as xs:string ) as document−node()?
TNTBase
Conclusion Get a file path to which an element belongs:
tnt:get −doc−paths($docs as node()∗) as xs:string∗
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 31
32. VDoc functions
XQuery
Tutorial
Vyacheslav
Zholudev
Test a VDoc Spec:
tnt:vd −skel($path as xs:string)
Introduction
XQuery Type
Get an underlying VDoc Spec path of the VDoc:
System
tnt:skel −path($path as xs:string) as xs:string ?
Basics
Tricks
Get a VDoc:
Pitfalls
tnt:vdoc($path as xs:string ) as item()
XQuery in Insert a parameter for the VDoc:
TNTBase
tnt:ins −vd−param($path as xs:string, $par as xs:string, $vals as xs:string +)
Conclusion
Delete a parameter of the VDoc:
tnt:delete −vd−param($path as xs:string, $name as xs:string)
... some others
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 32
33. Dynamic evaluation
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
XQuery Type
System Query:
Basics
tnt:execute($xquery as xs:string )
Tricks
Pitfalls
Example:
XQuery in
count(tnt:execute( " tnt:collection (//∗. omdoc)"))
TNTBase
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 33
34. Some other functions
XQuery
Tutorial
Vyacheslav Commit documents (dangerous):
Zholudev
tnt:commit($paths as xs:string ∗, $docs as element()∗,
Introduction $msg as xs:string, $revnum as xs:integer) as xs:string
XQuery Type
System
Basics
XML-diff:
Tricks tnt:xml −diff($ differ −name as xs:string, $l as element(), $r as element())
Pitfalls Get an auxiliary document produced by a plugin:
XQuery in
TNTBase
tnt:typed −doc($path as xs:string, $type as xs:string +)
Conclusion Substring before last occurrence of some string:
tnt:substring −before−last($arg as xs:string?, $delim as xs:string )
Delete a parameter of the VDoc:
tnt:delete −vd−param($path as xs:string, $name as xs:string)
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 34
35. XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
XQuery Type
System
Basics
Tricks
Pitfalls
Wrapping Up
XQuery in
TNTBase
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 35
36. Non-covered topics
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction Type casting in XQuery
XQuery Type
System
Stronger typing using XML Schema
Basics Node construction
Tricks
XQuery Update facility
Pitfalls
XQuery in
EXQuery
TNTBase
What’s new in XQuery 1.1?
Conclusion
XQuery Scripting and side-effects
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 36
37. Advantages of XQuery
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction In context of XML DBs when benefiting from indexes
XQuery Type
System Processing big documents that do not fit into memory
Basics (XSLT streaming doesn’t count here)
Tricks
Querying is better, then XSLT for formatting and
Pitfalls
presentation
XQuery in
TNTBase XQuery is easier to explain
Conclusion
Less verbose
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 37
38. Advantages of XSLT
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction Recurring over trees is much easier
XQuery Type
System
Better in formatting numbers
Basics More mature technology
Tricks
Well-standardized library of extensions (EXSLT)
Pitfalls
XQuery in
XML-language
TNTBase
XQuery is a subset of XSLT in terms of functionality
Conclusion
Has a form of polymorphism to overwrite templates
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 38
39. Conclusion
XQuery
Tutorial
Vyacheslav
Zholudev
Introduction
XQuery Type
System
Basics
You should pick out the tool
Tricks
Pitfalls that suits your task best!
XQuery in
TNTBase
Conclusion
Vyacheslav Zholudev XQuery Tutorial September 17, 2010 39