CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML...

71
CBU 2007 XQuery and XPath 1 W3C XML Query W3C XML Query How to access various XML data sources? How to access various XML data sources? XQuery XQuery , XML Query Lang, W3C Rec, Jan '07 , XML Query Lang, W3C Rec, Jan '07 joint work by XML Query and XSL WGs joint work by XML Query and XSL WGs » with XPath 2.0 and XSLT 2.0 with XPath 2.0 and XSLT 2.0 influenced by many research groups and query influenced by many research groups and query languages languages » Quilt Quilt , , XPath XPath , XQL, XML-QL, SQL, OQL, Lorel, , XQL, XML-QL, SQL, OQL, Lorel, ... ... A query language for any XML-represented data: A query language for any XML-represented data: both documents and databases both documents and databases

Transcript of CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML...

Page 1: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 1

W3C XML QueryW3C XML Query

How to access various XML data sources?How to access various XML data sources? XQueryXQuery, XML Query Lang, W3C Rec, Jan '07, XML Query Lang, W3C Rec, Jan '07

– joint work by XML Query and XSL WGsjoint work by XML Query and XSL WGs» with XPath 2.0 and XSLT 2.0with XPath 2.0 and XSLT 2.0

– influenced by many research groups and query influenced by many research groups and query languageslanguages» QuiltQuilt, , XPathXPath, XQL, XML-QL, SQL, OQL, Lorel, ..., XQL, XML-QL, SQL, OQL, Lorel, ...

– A query language for any XML-represented data: A query language for any XML-represented data: both documents and databasesboth documents and databases

Page 2: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 2

Functional Requirements Functional Requirements (1)(1)

Support operations (selection, projection, aggregation, Support operations (selection, projection, aggregation, sorting, etc.) on all data types: sorting, etc.) on all data types: – Choose data based on content or structureChoose data based on content or structure– Operate on document hierarchy and orderOperate on document hierarchy and order

Structural preservation and transformation:Structural preservation and transformation: – Preserve relative hierarchy and sequence of input structuresPreserve relative hierarchy and sequence of input structures– Transform XML structures, and create newTransform XML structures, and create new

Combining and joining:Combining and joining:– Combine data from different parts of a document, or from multiple Combine data from different parts of a document, or from multiple

documentsdocuments

Page 3: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 3

Functional Requirements Functional Requirements (2)(2)

Closure property:Closure property: – Results of XML queries are also XML Results of XML queries are also XML

(well-formed document (well-formed document fragmentsfragments))– > queries can be combined without limit> queries can be combined without limit

Extensibility:Extensibility:– should support externally defined functions on all data should support externally defined functions on all data

types of the data modeltypes of the data model

Page 4: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 4

XQuery in a NutshellXQuery in a Nutshell

Functional expression languageFunctional expression language Strongly-typedStrongly-typed: : (optional) (optional) type-checkingtype-checking of expressions, and of expressions, and

validationvalidation of results (We’ll concentrate to of results (We’ll concentrate to processingprocessing))– predeclared prefixpredeclared prefix for type names: for type names: xsxs==http://www.w3.org/2001/http://www.w3.org/2001/XMLSchemaXMLSchema

Extends XPath 2.0Extends XPath 2.0– XQuery 1.0 and XPath 2.0XQuery 1.0 and XPath 2.0 Functions and OperatorsFunctions and Operators, Rec. Jan. , Rec. Jan.

20072007 XQuery XQuery XPath 2.0 + XSLT' + SQL' (roughly) XPath 2.0 + XSLT' + SQL' (roughly)

Page 5: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 5

Example QueryExample Query

xquery version "1.0";xquery version "1.0";

<cheapBooks><cheapBooks> <Title>Cheap Books</Title> <Title>Cheap Books</Title> { { forfor $b $b inin fn:doc("bib.xml")//book[@price < 50] fn:doc("bib.xml")//book[@price < 50] order byorder by $b/title $b/title returnreturn $b } $b } </cheapBooks> </cheapBooks>

XML-based syntax (XQueryX) also specifiedXML-based syntax (XQueryX) also specified

Syntax "concise and easily understood"Syntax "concise and easily understood"

Page 6: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 6

A possible resultA possible result

<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><cheapBooks><cheapBooks> <Title>Cheap Books</Title> <Title>Cheap Books</Title> <book price="26.50"> <book price="26.50"> <title>Computing with Logic</title> <title>Computing with Logic</title> <author>David Maier</author> <author>David Maier</author> <publisher>Benjamin Cummings</publisher> <publisher>Benjamin Cummings</publisher> <year>1999</year> <year>1999</year> </book> </book> <book price="40.00"> <book price="40.00"> <title>Designing Internet applications</title> <title>Designing Internet applications</title> <author>Michael Leventhal</author> <author>Michael Leventhal</author> <publisher>Prentice Hall</publisher> <publisher>Prentice Hall</publisher> <year>1998</year> <year>1998</year> </book> </book></cheapBooks></cheapBooks>

Page 7: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 7

XQuery and XPathXQuery and XPath

XQuery (1.0) is an extension of XPath (2.0)XQuery (1.0) is an extension of XPath (2.0)– Common data model, functions and operatorsCommon data model, functions and operators– > study some XPath first> study some XPath first

XPath used in several other contexts, too:XPath used in several other contexts, too:– For uniqueness constraints in XML SchemaFor uniqueness constraints in XML Schema– For in validation rules of SchematronFor in validation rules of Schematron– For pattern matching and selection in XSLTFor pattern matching and selection in XSLT– For addressing in XLink and XPointerFor addressing in XLink and XPointer

Page 8: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 8

XPath in a NutshellXPath in a Nutshell

XPath 1.0 (W3C Rec. 11/99)XPath 1.0 (W3C Rec. 11/99)– a compact non-XML syntax for a compact non-XML syntax for addressing parts of addressing parts of

XML documents XML documents (as (as node-setsnode-sets in XPath 1.0)in XPath 1.0)– also typical operations on also typical operations on stringsstrings, , numbersnumbers and and truth truth

valuesvalues

XPath 2.0 (2.0 Rec. 1/07) extends and XPath 2.0 (2.0 Rec. 1/07) extends and generalizes: generalizes: – data manipulated as data manipulated as sequences sequences ofof items items

» Item = a node or an atomic value of a simple XML Item = a node or an atomic value of a simple XML Schema datatype Schema datatype

Page 9: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 9

XPath 1.0 vs 2.0 XPath 1.0 vs 2.0

XPath 2.0 more elegant and complete than 1.0XPath 2.0 more elegant and complete than 1.0 Also more complex (Length of specs as pages):Also more complex (Length of specs as pages):

Data ModelData Model ~ 80 ~ 80

XPath 2.0XPath 2.0 ~100~100

Funcs & opersFuncs & opers ~160~160--------------------------------------------------------------------

~340~340

XPath 1.0 ~ 30XPath 1.0 ~ 30

------------------------------------------------TotalTotal ~ 30 ~ 30

Page 10: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 10

XQuery/XPath/XSLT Data ModelXQuery/XPath/XSLT Data Model

Documents are viewed as treesDocuments are viewed as treesmade of six types of nodes:made of six types of nodes:– rootroot (additional parent of document element) (additional parent of document element)– elementelement nodes nodes– attributeattribute nodes nodes– texttext nodes nodes– CommentsComments and and processing instructionsprocessing instructions

Obs 1: No entity nodesObs 1: No entity nodes Obs 2: No namespace nodes Obs 2: No namespace nodes

(XPath/XSLT 1.0 contains them) (XPath/XSLT 1.0 contains them)

Page 11: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 11

Document treesDocument trees

Defined in Sect. 5 of XPath 1.0 specDefined in Sect. 5 of XPath 1.0 spec– for XSLT/XPath 2.0 & XQuery in their joint Data Model for XSLT/XPath 2.0 & XQuery in their joint Data Model

Element nodes have elements, text nodes, Element nodes have elements, text nodes, comments and processing instructions of their comments and processing instructions of their (direct) (direct) contentcontent as their children as their children– NBNB: attribute nodes are : attribute nodes are notnot children (but have a parent) children (but have a parent)– > they have no siblings either> they have no siblings either– the string the string valuevalue of an document/element is the of an document/element is the

concatenation of its all text-node descendantsconcatenation of its all text-node descendants

Page 12: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 12

Document OrderDocument Order

Document orderDocument order of nodes: of nodes:– = the depth-first traversal order= the depth-first traversal order– Root firstRoot first– Other nodes in the order of the first character of their Other nodes in the order of the first character of their

XML markup in the document textXML markup in the document text– > an element precedes it's attribute nodes, which > an element precedes it's attribute nodes, which

precede any content nodes of the elementprecede any content nodes of the element– Implementation dependent btw nodes belonging to Implementation dependent btw nodes belonging to

different treesdifferent trees

Page 13: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 13

"W ritte n b y the le c ture r."

"W ritte n b y the le c ture r."

root

""

elem ent

"artic le "

text

""

"W ritte n b y "

elem ent

"f ig "

""

text

""

" the le c ture r."

attribute attribute

"cap tio n" "f ile "

"The Lec turer" "p e kka.jp g "

XPath trees: ExampleXPath trees: Example

Legend: type

value

nam e

<article>Written by <article>Written by <fig <fig file="pekka.jpg" file="pekka.jpg"

caption="The caption="The Lecturer" /> Lecturer" />

the lecturer.the lecturer.</article></article>

33rdrd

55thth or 6 or 6thth

11stst

55thth or 6 or 6thth

77thth

44thth

22ndnd

Page 14: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 14

XQuery/XPath SequencesXQuery/XPath Sequences

Expressions operate on, and return Expressions operate on, and return sequencessequences of of– atomic valuesatomic values (of XML Schema simple types) and (of XML Schema simple types) and– nodesnodes– an an itemitem a a singleton sequencesingleton sequence– sequences are sequences are flatflat: no sequences as items: no sequences as items

» (1(1,, (2 (2,, 3) 3),, () (),, 1) = (1 1) = (1,, 2 2,, 3 3,, 1) 1)

– sequences are sequences are orderedordered, and can contain duplicates , and can contain duplicates Unlimited combination of expressions, often with automatic Unlimited combination of expressions, often with automatic

type conversions (e.g. for arithmetics)type conversions (e.g. for arithmetics)

Page 15: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 15

Sequence ExpressionsSequence Expressions

Constant sequences constructed by Constant sequences constructed by listing valueslisting values– comma (comma (,,) is a catenation operator) is a catenation operator

» (1(1,, (2 (2,, 3) 3),, () (),, 1) = (1 1) = (1,, 2 2,, 3 3,, 1) 1)

Shorthands for numeric sequences:Shorthands for numeric sequences:– 1 1 toto 4 4– 4 4 toto 1 1– fn:reverse(1 fn:reverse(1 toto 4) 4)

(1, 2, 3, 4)(1, 2, 3, 4)

()()

(4, 3, 2, 1)(4, 3, 2, 1)

Page 16: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 16

Location PathsLocation Paths

XPath can select any parts of a document XPath can select any parts of a document tree using … tree using …

Location pathsLocation paths– evaluated with respect to a evaluated with respect to a context itemcontext item

» in XQuery typically starting from in XQuery typically starting from $x$x or or doc(…)doc(…)

– ResultResult: a sequence of nodes in document order, : a sequence of nodes in document order, without duplicateswithout duplicates

Page 17: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 17

Location pathsLocation paths

Consist of Consist of location stepslocation steps separated by ' separated by '//''– each step produces a sequence of itemseach step produces a sequence of items– steps evaluated left-to-right, steps evaluated left-to-right,

each item in turn as the context itemeach item in turn as the context item Complete location step: Complete location step:

AxisNameAxisName:::: NodeTestNodeTest ( ([[PredicateExprPredicateExpr]])*)*– axisaxis specifies the tree relationship between the context specifies the tree relationship between the context

node and the selected nodes node and the selected nodes – node testnode test restricts the type and and name of nodes restricts the type and and name of nodes– filtered further by 0 or more filtered further by 0 or more predicatespredicates

Page 18: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 18

Location steps: AxesLocation steps: Axes

In total 12 axes (~ directions in tree)In total 12 axes (~ directions in tree)– for staying at the context node: for staying at the context node: selfself– for going downwards:for going downwards:

» childchild, , descendantdescendant, , descendant-or-selfdescendant-or-self– for going upwards:for going upwards:

» parentparent, , ancestorancestor, , ancestor-or-selfancestor-or-self– for moving towards start/end of the document:for moving towards start/end of the document:

» preceding-siblingpreceding-sibling, , following-siblingfollowing-sibling, , precedingpreceding, , followingfollowing

– ““Special” axesSpecial” axes» attribute;attribute; (+ (+ namespace namespace in Path 1.0) in Path 1.0)

– OnlyOnly child child,, descendant descendant,, attribute attribute,, self self,, descendant-or- descendant-or-selfself, and, and parent parent mandatory in XQuery mandatory in XQuery

Page 19: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 19

Path Axes and Their OrientationPath Axes and Their Orientation

Ordinary axes oriented away from context nodeOrdinary axes oriented away from context node((attributeattribute and and namespacenamespace axes are unordered) axes are unordered)– the the position()position() for the closest node for the closest node = 1= 1– for the most remote node, for the most remote node, position() = last()position() = last()

1

C o nte xtno de :

The simplest axis,The simplest axis, self::self::

Page 20: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 20

XPath Axes and Their OrientationXPath Axes and Their Orientation

parent::parent:: (exists for every node except the root)(exists for every node except the root)

1

Context node:

Page 21: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 21

XPath Axes and Their OrientationXPath Axes and Their Orientation

ancestor::ancestor:: 2

1

ancestor-or-self::ancestor-or-self::3

2

1

Page 22: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 22

XPath Axes and Their OrientationXPath Axes and Their Orientation

child::child::

431 2

C ontextnode :

Page 23: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 23

XPath Axes and Their OrientationXPath Axes and Their Orientation

descendant::descendant::

871 4

2 3 65 9

descendant-or-self::descendant-or-self:: 1

982 5

3 4 76 1 0

Page 24: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 24

XPath Axes and Their OrientationXPath Axes and Their Orientation

preceding-sibling::preceding-sibling::

2 1

following-sibling::following-sibling::

21

Page 25: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 25

XPath Axes and Their OrientationXPath Axes and Their Orientation

following::following::

21

3

4

5

preceding::preceding::

3

2 1

Page 26: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 26

Location paths: Node testsLocation paths: Node tests

Node tests (slightly simplified)Node tests (slightly simplified)– NameName: any : any elementelement node with name node with name NameName

(on an attribute axis, any attribute node with name (on an attribute axis, any attribute node with name NameName))

– **: any : any elementelement (any (any attributeattribute node on an attribute axis) node on an attribute axis)

– text()text(): any text node: any text node» comment()comment(): any comment node: any comment node» processing-instruction()processing-instruction(): any processing instruction: any processing instruction

– node()node(): any node of any type: any node of any type

Page 27: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 27

Location paths: AbbreviationsLocation paths: Abbreviations

Abbreviations in location stepsAbbreviations in location steps– ''child::child::'' can be omitted can be omitted– ''attribute::attribute::'' can be shortened to can be shortened to ''@@''– ''parent::node()parent::node()'' can be shortened to can be shortened to ''....''

– Predicate 'Predicate '[position()=[position()=nn]]' for testing ' for testing occurrence position occurrence position nn can be shortened to ' can be shortened to '[[nn]]''

– ''/descendant-or-self::node()//descendant-or-self::node()/'' shortened to shortened to ''////''

Page 28: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 28

Notes on Location Paths (1)Notes on Location Paths (1)

Path 2.0 allows unrestricted expressions as stepsPath 2.0 allows unrestricted expressions as steps– but steps except the last must produce nodes onlybut steps except the last must produce nodes only

Numeric predicates support array-style access: Numeric predicates support array-style access: $rows[$i]$rows[$i]

Predicates evaluated step at a time. This often Predicates evaluated step at a time. This often causes confusion with shorthand notations:causes confusion with shorthand notations:– doc("doc.xml")doc("doc.xml")////title[3] title[3]

third third titletitle child of each parent (likely none!). Why? child of each parent (likely none!). Why?– = = doc("doc.xml")doc("doc.xml")//

descendant-or-self::node()/descendant-or-self::node()/child::title[3]child::title[3]– To get the third title in the doc useTo get the third title in the doc use

((doc("doc.xml")//titledoc("doc.xml")//title))[3][3]

Page 29: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 29

Notes on Location Paths (2)Notes on Location Paths (2)

References to attributes and References to attributes and subelements easy to use as predicatessubelements easy to use as predicates– Get divisions that are of class Get divisions that are of class CC or have a or have a headhead::

doc("doc.xml")//div[@class="C" or head]doc("doc.xml")//div[@class="C" or head]

– Values are coerced to Booleans on Values are coerced to Booleans on demanddemand» string/sequence string/sequence true iff non-empty true iff non-empty» number number true iff not zero true iff not zero

(but a single number as predicate tests for (but a single number as predicate tests for equality with equality with position()position()))

Page 30: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 30

Semantics of Location Paths (example)Semantics of Location Paths (example)

{2, 5, 7}{2, 5, 7}

AA

BB

context nodecontext node

CC"txt""txt"AA BB

BBCC55

884433

22

11

66

77

*/node()/parent::B[child::A]*/node()/parent::B[child::A]

{3, 4, 6, 8}{3, 4, 6, 8} {2, 5, 7}{2, 5, 7}

final value: {2}final value: {2}

value after value after each step:each step:

Page 31: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 31

Filter ExpressionsFilter Expressions

Location steps can be filtered by predicates:Location steps can be filtered by predicates: ./(chap | app)[fn:last()]/title ./(chap | app)[fn:last()]/title

the title of the last chapter of appendix, whichever is lastthe title of the last chapter of appendix, whichever is last

Other sequences, too:Other sequences, too: (1 to 20)[. mod 5 eq 0](1 to 20)[. mod 5 eq 0] →→ (5, 10, 15, 20) (5, 10, 15, 20) – ('('..' generalized from XPath 1.0 shorthand for ' generalized from XPath 1.0 shorthand for self::node()self::node() into the into the context itemcontext item))

XPath 2.0 extended stepXPath 2.0 extended step

Page 32: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 32

Further XPath ExpressionsFurther XPath Expressions

Double-precision floating-point Arithmetics Double-precision floating-point Arithmetics ++, , --, , **, , divdiv, , modmod (same as (same as %% in Java)in Java)

»e.g. e.g. 2.3 mod 1.12.3 mod 1.1 ≈ 0.1≈ 0.1 Functions for rounding and truncating:Functions for rounding and truncating: floor(x)floor(x), , ceiling(x)ceiling(x),,round(x)round(x)

Page 33: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 33

Set Operations on Node (!) SequencesSet Operations on Node (!) Sequences

Assume variable bindings:Assume variable bindings:

$s2$s2

Then:Then:

$s1$s1

$s1 $s1 unionunion $s2 = $s2 =

$s1 $s1 intersectintersect $s2 = $s2 =

$s1 $s1 exceptexcept $s2 = $s2 =

based on based on node indentitynode indentity((nodenode11 is is nodenode22))

aa bb cc dd ee

aa bb cc dd ee

cc

aa bb

Page 34: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 34

Node ComparisonsNode Comparisons

To compare single nodes, To compare single nodes, – for identity: for identity: isis

$book//chap[@id="ch1"] $book//chap[@id="ch1"] isis ($book//chap)[1] ($book//chap)[1] true iff the chapter with true iff the chapter with id="ch1"id="ch1" is indeed the first is indeed the first

– for document order: for document order: <<<< andand >>>>$book//chap[@id="ch2"] $book//chap[@id="ch2"] >>>>

$book//title[. eq "Intro"] $book//title[. eq "Intro"] true iff the chapter with true iff the chapter with id="ch2"id="ch2" appears appears afterafter <title>Intro</title><title>Intro</title>

Page 35: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 35

Comparing values of sequences and itemsComparing values of sequences and items

General comparisonsGeneral comparisons btw sequences: btw sequences: – ==, , !=!=, , <<, , <=<=, , >>, , >=>=– existential semantics: true iff existential semantics: true iff somesome pair of values pair of values

from operand sequences satisfy the conditionfrom operand sequences satisfy the condition » (1,2) = (2,3); (2,3) = (3,4); (1,2) != (3,4)(1,2) = (2,3); (2,3) = (3,4); (1,2) != (3,4)» Same as in XPath 1.0:Same as in XPath 1.0:

//book[author = "Aho"]//book[author = "Aho"]→→ books where books where somesome author is Aho author is Aho

Value comparisonsValue comparisons btw single values: btw single values:– eqeq, , nene, , ltlt, , lele, , gtgt, , gege

» 1 eq 3 - 2; 10 lt 20; $books[@price le 100]1 eq 3 - 2; 10 lt 20; $books[@price le 100]

Page 36: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 36

Accessing DocumentsAccessing Documents

XQuery operates on nodes accessible by input XQuery operates on nodes accessible by input functionsfunctions– fn:doc(fn:doc(""URIURI""))

» document-nodedocument-node of the XML document available at of the XML document available at URIURI

» same as same as document(document(""URIURI"")) in XSLT 1.0 in XSLT 1.0

– fn:collection(fn:collection(""URIURI""))» sequence of nodes from sequence of nodes from URIURI

– predeclared prefix for the default function namespace: predeclared prefix for the default function namespace: fnfn=http://www.w3.org/2005/04/xpath-functions=http://www.w3.org/2005/04/xpath-functions

Page 37: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 37

XQuery over XPathXQuery over XPath

A query is an expressionA query is an expression XQuery adds to XPath expressionsXQuery adds to XPath expressions

– Element constructors (Element constructors ( XSLT templates) XSLT templates)– FLWOR expressions FLWOR expressions

(”flower”; (”flower”; fforor--lletet--wwherehere--oorder byrder by--rreturneturn))

Page 38: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 38

Central XQuery ExpressionsCentral XQuery Expressions

Path expressionsPath expressions Sequence expressions Sequence expressions Comparison operatorsComparison operators Quantified expressions Quantified expressions

((somesome//everyevery $var $var in … satisfiesin … satisfies …) …) Element constructors (Element constructors ( XSLT templates) XSLT templates) FLWOR expressions FLWOR expressions

(”flower”; (”flower”; fforor--lletet--wwherehere--oorder byrder by--rreturneturn)) and others, in examples ... and others, in examples ...

also in XPath 2.0also in XPath 2.0

Page 39: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 39

Element ConstructorsElement Constructors

Similar to XSLT templates:Similar to XSLT templates:– start and end tag enclosing the contentstart and end tag enclosing the content– literal fragments written directly, literal fragments written directly,

expressions enclosed in braces expressions enclosed in braces {{ and and }}≈≈ XSLT 1.0 attribute value templates XSLT 1.0 attribute value templates

often used inside another expression that binds often used inside another expression that binds variables used in the element constructorvariables used in the element constructor– (There is no 'current node' in XQuery)(There is no 'current node' in XQuery)– See nextSee next

Page 40: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 40

ExampleExample

An An empemp element with an element with an empidempid attribute and child attribute and child elements elements namename and and jobjob, from values in variables , from values in variables $id$id, , $n$n, and , and $j$j::

<emp empid="<emp empid="{$id}{$id}">">

<name><name>{$n}{$n}</name> </name>

<job><job>{$j}{$j}</job></job>

</emp></emp>

Also Also computed constructorscomputed constructors::

element {"emp"} { element {"emp"} { attribute {"empid"} attribute {"empid"}{$id}{$id},, <name> <name> {$n}{$n} </name>, </name>, <job> <job> {$j}{$j} </job> } </job> }

Page 41: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 41

FLWOR ("flower") ExpressionsFLWOR ("flower") Expressions

Constructed from Constructed from forfor, , letlet, , wherewhere, , order byorder by and and returnreturn clauses (~SQL clauses (~SQL selectselect--fromfrom--wherewhere))

Syntax: Syntax: (ForClause | LetClause)+ (ForClause | LetClause)+ WhereClause? WhereClause? OrderByClause?OrderByClause?""returnreturn" Expr" Expr

FLWOR binds variables to values, and uses FLWOR binds variables to values, and uses these bindings to construct a result these bindings to construct a result (an ordered sequence of items)(an ordered sequence of items)

XPath 2.0 has a XPath 2.0 has a

simpler "for-return"simpler "for-return"

Page 42: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 42

Flow of data in a FLWOR expressionFlow of data in a FLWOR expression

Page 43: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 43

forfor clauses clauses

forfor $V $V11 inin ExpExp11 ( (, ,

$V$V22 inin ExpExp2,2, …) …)

– associates each variable associates each variable VVii with expression with expression ExpExpii

(e.g. a path expression) (e.g. a path expression)

Result: list of tuples, each containing a binding for Result: list of tuples, each containing a binding for each of the variableseach of the variables

can be though of as loops iterating over the items can be though of as loops iterating over the items returned by respective expressionsreturned by respective expressions

Page 44: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 44

Example:Example: for for clause clause

forfor $i $i inin (1,2), (1,2), $j $j inin (1 (1 toto $i) $i)returnreturn <tuple> <tuple>

<i>{<i>{$i$i}</i> <j>{}</i> <j>{$j$j}</j></tuple>}</j></tuple>

Result:Result:

<tuple><i>1</i><j>1</j></tuple><tuple><i>1</i><j>1</j></tuple>

<tuple><i>2</i><j>1</j></tuple><tuple><i>2</i><j>1</j></tuple>

<tuple><i>2</i><j>2</j></tuple><tuple><i>2</i><j>2</j></tuple>

Page 45: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 45

letlet clauses clauses

letlet also binds variables to expressions also binds variables to expressions– each variable gets the entire sequence as its value each variable gets the entire sequence as its value

(without iterating over the items of the sequence)(without iterating over the items of the sequence)– results in binding a single sequence for each variableresults in binding a single sequence for each variable

Compare:Compare:– forfor $b $b inin doc("bib.xml")//book doc("bib.xml")//book

many bindings (to single books) many bindings (to single books)– letlet $bl $bl :=:= doc("bib.xml")//book doc("bib.xml")//book

a single binding (to sequence of books) a single binding (to sequence of books)

Page 46: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 46

Example: let clausesExample: let clauses

letlet $s $s :=:= (<one/>, <two/>, <three/>) (<one/>, <two/>, <three/>)returnreturn <out> {$s} </out> <out> {$s} </out>

Result:Result:

<out><out> <one/><one/> <two/><two/> <three/><three/></out></out>

forfor $s $s inin (<one/>,<two/>,<three/>) (<one/>,<two/>,<three/>)returnreturn <out> {$s} </out> <out> {$s} </out>

-->--> <out><one/></out><out><one/></out> <out><two/></out><out><two/></out> <out><three/></out><out><three/></out>

Page 47: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 47

forfor//letlet clauses clauses

A FLWOR expr may contain several A FLWOR expr may contain several forfors and s and letletss– each may refer to variables bound in previous clauseseach may refer to variables bound in previous clauses

the result of the the result of the forfor//letlet sequence: sequence:– an ordered list of tuples (an ordered list of tuples (monikkomonikko) of bound variables) of bound variables– number of tuples = product of the cardinalities of the number of tuples = product of the cardinalities of the

sequences returned by the sequences returned by the forfor expressions expressions

Page 48: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 48

wherewhere clause clause

binding tuples generated by binding tuples generated by forfor and and letlet clauses clauses are filtered by an optional are filtered by an optional wherewhere clause clause– tuples with a tuples with a truetrue condition are used to instantiate the condition are used to instantiate the

returnreturn clause clause

the the wherewhere clause may contain several predicates clause may contain several predicates connected by connected by andand, , oror, and , and fn:notfn:not()()– usually refer to the bound variablesusually refer to the bound variables

– sequences as Booleans (similarly to node-sets in sequences as Booleans (similarly to node-sets in XPath 1.0): empty ~ XPath 1.0): empty ~ falsefalse; non-empty ~ ; non-empty ~ truetrue

Page 49: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 49

wherewhere clause clause

forfor binds variables to single items binds variables to single items value comparisonsvalue comparisons, e.g. , e.g. $color $color eqeq ""redred""

letlet to whole sequences to whole sequences general comparisonsgeneral comparisons, e.g. , e.g. $colors $colors == "red" "red"

((~ ~ somesome $c $c inin $colors $colors satisfiessatisfies $c $c eqeq "red" "red"))

– a number of aggregation functions available: a number of aggregation functions available: avgavg()(), , sumsum()(), , countcount()(), , maxmax()(), , minmin()()

(also in XPath 1.0)(also in XPath 1.0)

Page 50: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 50

returnreturn clause clause

The The returnreturn clause generates the output of the clause generates the output of the FLWOR expressionFLWOR expression

instantiated once for each binding tupleinstantiated once for each binding tuple often contains element constuctors, references to often contains element constuctors, references to

bound variables, and nested sub-expressionsbound variables, and nested sub-expressions

Page 51: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 51

Example:Example: for for + + returnreturn

forfor $i $i inin (1,2), (1,2), $j $j inin (1 (1 toto $i) $i)returnreturn <tuple> <tuple>

<i>{<i>{$i$i}</i> <j>{}</i> <j>{$j$j}</j></tuple>}</j></tuple>

Result:Result:

<tuple><i>1</i><j>1</j></tuple><tuple><i>1</i><j>1</j></tuple>

<tuple><i>2</i><j>1</j></tuple><tuple><i>2</i><j>1</j></tuple>

<tuple><i>2</i><j>2</j></tuple><tuple><i>2</i><j>2</j></tuple>

Page 52: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 52

Positional variables: 'at'Positional variables: 'at'

For items, can also get their position in the For items, can also get their position in the seq:seq:

for $char for $char at $iat $i in ("a", "b", "c") in ("a", "b", "c")return concat($i, ".", $char, ";")return concat($i, ".", $char, ";") 1.a;2.b;3.c;1.a;2.b;3.c;

Could pair items by their position:Could pair items by their position:let $boys:= doc("kids.xml")//boy,let $boys:= doc("kids.xml")//boy, $girls:= doc("kids.xml")//girl $girls:= doc("kids.xml")//girlfor $b for $b atat $i$i in $boys in $boyswhere $i le count($girls)where $i le count($girls)return <pair>{ $b, $girls[$i] }</pair>return <pair>{ $b, $girls[$i] }</pair>

Page 53: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 53

Examples (modified from "XML Query Use Cases")Examples (modified from "XML Query Use Cases")

Assume: a document named ”Assume: a document named ”bib.xmlbib.xml” ” containing of a list of containing of a list of bookbooks:s:

<book><book>++

<title><title><author><author>++

<publisher><publisher><year><year><price><price>

Page 54: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 54

<recent-MK-books> {<recent-MK-books> {

} </recent-MK-books>} </recent-MK-books>

List Morgan Kaufmann book titles since 1998List Morgan Kaufmann book titles since 1998

forfor $b $b in docin doc(”bib.xml”)//book(”bib.xml”)//book

wherewhere $b/publisher = ”Morgan Kaufmann” $b/publisher = ”Morgan Kaufmann”

andand $b/year >= 1998 $b/year >= 1998

returnreturn <book year="{$b/year}"> <book year="{$b/year}">

{$b/title}{$b/title}

</book></book>

Page 55: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 55

Result could be...Result could be...

<recent-MK-books><recent-MK-books> <book year=”1999”><book year=”1999”> <title>TCP/IP Illustrated</title><title>TCP/IP Illustrated</title> </book></book> <book year=”2000”><book year=”2000”> <title>Advanced Programming in the Unix <title>Advanced Programming in the Unix environment</title>environment</title> </book></book></recent-MK-books></recent-MK-books>

Page 56: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 56

Publishers with avg price of their books:Publishers with avg price of their books:

forfor $p $p in fn:distinct-valuesin fn:distinct-values(( fn:doc(”bib.xml”)//publisher ) fn:doc(”bib.xml”)//publisher )

letlet $a := avg( doc(”bib.xml”)//book[ $a := avg( doc(”bib.xml”)//book[publisher = $p]/price )publisher = $p]/price )

returnreturn <publisher> <publisher><name> {$p} </name><name> {$p} </name><avgprice> {$a} </avgprice><avgprice> {$a} </avgprice>

</publisher> </publisher>

string values of the string values of the sequence, without sequence, without duplicatesduplicates

Page 57: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 57

Invert the book-list structureInvert the book-list structure

<author_list>{ (: group books by authors :)<author_list>{ (: group books by authors :)

forfor $a $a in in distinct-values(distinct-values(doc(”bib.xml”)//author )doc(”bib.xml”)//author )

returnreturn <author> <author> {{

<name> <name> {{$a$a}} </name> </name>,,

forfor $b $b in in doc(”bib.xml”)//book[doc(”bib.xml”)//book[

author = $a]author = $a]

returnreturn $b/title $b/title }}

</author></author>

} </author_list>} </author_list>

Page 58: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 58

List of publishers alphabetically, List of publishers alphabetically, and their books in descending order of priceand their books in descending order of price

forfor $p $p in in distinct-values(distinct-values(doc(”bib.xml”)//publisher )doc(”bib.xml”)//publisher )

order byorder by $p $preturnreturn <publisher><publisher> <name>{$p}</name><name>{$p}</name> { { forfor $b $b in in doc(”bib.xml”)//book[doc(”bib.xml”)//book[

publisher = $p]publisher = $p] order byorder by $b/price $b/price descendingdescending returnreturn <book> {$b/title, <book> {$b/title, $b/price} </book> }$b/price} </book> } </publisher></publisher>

Page 59: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 59

Queries on Document OrderQueries on Document Order

Operators Operators <<<< and and >>>>: :

– x x <<<< y = true iff node x precedes node y in document y = true iff node x precedes node y in document order; (y order; (y >>>> x similarly) x similarly)

Consider a surgical report with Consider a surgical report with

– procedure procedure elements containing elements containing

» incision sub-incision sub-elements elements

Return a "critical sequence" of contents between the first Return a "critical sequence" of contents between the first and the second incisions of the first procedureand the second incisions of the first procedure

Page 60: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 60

Computing a "critical sequence"Computing a "critical sequence"

<critical_sequence> {<critical_sequence> { letlet $p := $p := (doc("report.xml")//procedure)[1](doc("report.xml")//procedure)[1] forfor $n $n inin $p/node() $p/node() wherewhere $n $n >>>> ($p//incision)[1] ($p//incision)[1] andand

$n $n <<<< ($p//incision)[2] ($p//incision)[2] returnreturn $n } $n } </critical_sequence></critical_sequence>

NB: if NB: if incisionincisions are not children of the s are not children of the procedureprocedure, , then an ancestor of the second then an ancestor of the second incisionincision gets to the gets to the result; How to avoid this?result; How to avoid this?

Page 61: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 61

User-defined functions: ExampleUser-defined functions: Example

declare functiondeclare function local:precedes($a local:precedes($a asas node(), node(), $b $b asas node()) node()) asas xs:boolean xs:boolean

{ $a { $a <<<< $b $b and and (: $a is no ancestor of $b: :) (: $a is no ancestor of $b: :) empty(empty($a//node() intersect $b$a//node() intersect $b)) }; };

locallocal:: is predeclared prefix for the namespace of local is predeclared prefix for the namespace of local function namesfunction names

– Alternatively: Alternatively:

declare namespace declare namespace my=http://my.namespace.org;my=http://my.namespace.org; declare functiondeclare function my:precedes( my:precedes(... (as above)... (as above)

Page 62: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 62

User-defined functions: ExampleUser-defined functions: Example

Now, ”critical sequence” without ancestors of Now, ”critical sequence” without ancestors of incisionincision: :

<critical_sequence> {<critical_sequence> { letlet $p := $p :=

(doc("report.xml")//procedure)[1](doc("report.xml")//procedure)[1] forfor $n $n inin $p/node() $p/node() wherewhere $n $n >>>> ($p//incision)[1] ($p//incision)[1] andand

local:precedes(local:precedes($n, $n, ($p//incision)[2]($p//incision)[2]))

returnreturn $n $n} </critical_sequence>} </critical_sequence>

Page 63: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 63

Recursive TransformationsRecursive Transformations

Example: “Table-of-contents” for nested sectionsExample: “Table-of-contents” for nested sections– NB NB if-then-else if-then-else (in ordinary XPath 2.0 expressions, too)(in ordinary XPath 2.0 expressions, too)

declare namespace declare namespace my=http://my.own-ns.org;my=http://my.own-ns.org;

declare functiondeclare function my:toc( $n my:toc( $n asas element() ) element() ) asas element()* element()*

{ { if if ((name(name($n$n))=”sect”)=”sect”) thenthen <sect> { <sect> {

forfor $c $c inin $n/* $n/* returnreturn my:toc($c) } </sect> my:toc($c) } </sect> else if else if ((name(name($n$n))=”title”) =”title”) thenthen $n $n else else (: check child elements, if any: :)(: check child elements, if any: :)

forfor $c $c inin $n/* $n/* returnreturn my:toc($c) }; my:toc($c) };

Page 64: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 64

Querying relational dataQuerying relational data

Lots of data is stored in relational databasesLots of data is stored in relational databases

Should be able to access also themShould be able to access also them

Example: Tables for Parts and SuppliersExample: Tables for Parts and Suppliers– P (P (pnopno, , descripdescrip) : part numbers and descriptions) : part numbers and descriptions

– S (S (snosno, sname, sname) : supplier numbers and names) : supplier numbers and names

– SP (SP (snosno, , pnopno, , priceprice): ):

who supplies which parts and for what price?who supplies which parts and for what price?

Page 65: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 65

Possible XML representation of relationsPossible XML representation of relations

**

**

**

Page 66: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 66

Selecting in SQL vs. XQuery Selecting in SQL vs. XQuery

SQL:SQL:

XQuery:XQuery:

SELECTSELECT pnopnoFROMFROM ppWHEREWHERE descripdescrip LIKELIKE ’Gear%’’Gear%’ORDER BYORDER BY pnopno;;

forfor $p $p in in docdoc(”p.xml”)//p_tuple(”p.xml”)//p_tuplewhere starts-withwhere starts-with($p/descrip, ”Gear”)($p/descrip, ”Gear”)order by order by $p/pno $p/pno returnreturn $p/pno $p/pno

Page 67: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 67

GroupingGrouping

Many queries involve grouping data and applying Many queries involve grouping data and applying aggregation function like aggregation function like countcount or or avgavg to each to each groupgroup

in SQL: in SQL: GROUP BYGROUP BY and and HAVINGHAVING clauses clauses Example: Find the part number and average price Example: Find the part number and average price

for parts with at least 3 suppliersfor parts with at least 3 suppliers

Page 68: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 68

Grouping: SQLGrouping: SQL

SELECTSELECT pno, pno, avgavg(price) (price) ASAS avgprice avgprice

FROMFROM sp sp

GROUP BYGROUP BY pno pno

HAVING countHAVING count(*) >= 3(*) >= 3

ORDER BYORDER BY pno; pno;

Page 69: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 69

Grouping: XQueryGrouping: XQuery

forfor $pn $pn in distinct-values(in distinct-values(doc(doc(”sp.xml”)//pno)”sp.xml”)//pno)

letlet $sp:= $sp:=docdoc(”sp.xml”)//sp_tuple[pno=$pn](”sp.xml”)//sp_tuple[pno=$pn]

where countwhere count($sp) >= 3($sp) >= 3

order byorder by $pn $pn

returnreturn

<well_supplied_item> <well_supplied_item> {{

<pno>{$pn}</pno><pno>{$pn}</pno>,,

<avgprice> <avgprice> {avg{avg($sp/price)($sp/price)}} </avgprice> </avgprice>

}} <well_supplied_item> <well_supplied_item>

Page 70: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 70

JoinsJoins

Example: Return a ”flat” list of supplier names and their Example: Return a ”flat” list of supplier names and their part descriptions, in alphabetic orderpart descriptions, in alphabetic order

forfor $sp $sp in docin doc(”sp.xml”)//sp_tuple,(”sp.xml”)//sp_tuple, $p $p in docin doc(”p.xml”)//p_tuple[pno = $sp/pno],(”p.xml”)//p_tuple[pno = $sp/pno], $s $s in docin doc(”s.xml”)//s_tuple[sno = $sp/sno](”s.xml”)//s_tuple[sno = $sp/sno]order byorder by $p/descrip, $s/sname $p/descrip, $s/snamereturnreturn <sp_pair>{ <sp_pair>{

$s/sname ,$s/sname , $p/descrip$p/descrip

}<sp_pair>}<sp_pair>

Page 71: CBU 2007XQuery and XPath1 W3C XML Query n How to access various XML data sources? n XQuery, XML Query Lang, W3C Rec, Jan '07 –joint work by XML Query and.

CBU 2007 XQuery and XPath 71

XQuery: SummaryXQuery: Summary

– A recent W3C XML query language, also capable of A recent W3C XML query language, also capable of general XML processinggeneral XML processing

– Vendor support??Vendor support??» http://www.w3.org/XML/Queryhttp://www.w3.org/XML/Query

mentions ~ 50 prototypes or products (2004: ~ 30, 2005: ~ 40; mentions ~ 50 prototypes or products (2004: ~ 30, 2005: ~ 40; free, commercial, ... Oracle, IBM)free, commercial, ... Oracle, IBM)

– Future?? Interesting confluence of document and Future?? Interesting confluence of document and database research, and highly potential for XML-based database research, and highly potential for XML-based data integrationdata integration