XML Databases 6. XML Query Languages II

Similar documents
Part VII. Querying XML The XQuery Data Model. Marc H. Scholl (DBIS, Uni KN) XML and Databases Winter 2005/06 153

The XQuery Data Model

6. XML Query Languages II. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. XML Databases. 6. XML Query Languages II

9. XQuery. 9.1 Introduction. 9.1 Introduction. 9.1 Introduction. 9.2 Preliminaries

Part X. XQuery Querying XML Documents

Part X. XQuery Querying XML Documents. Torsten Grust (WSI) Database-Supported XML Processors Winter 2008/09 246

XML Databases 7. XML Query Languages III XQuery

Outline of this part (I) Part IV. Querying XML Documents. Querying XML Documents. Outline of this part (II)

Part X. XQuery Querying XML Documents. Marc H. Scholl (DBIS, Uni KN) XML and Databases Winter 2005/06 248

Introduction to XQuery. Overview. Basic Principles. .. Fall 2007 CSC 560: Management of XML Data Alexander Dekhtyar..

XML Databases 5. XML Query Languages,

Progress Report on XQuery

5. XML Query Languages I. 5.1 Introduction. 5.1 Introduction. 5.1 Introduction. 5.1 Introduction. XML Databases 5. XML Query Languages,

XQuery. Leonidas Fegaras University of Texas at Arlington. Web Databases and XML L7: XQuery 1

Advanced Database Technologies XQuery

XML Databases 11. XML storage details

XML Databases 4. XML Processing,

11. XML storage details Introduction Last Lecture Introduction Introduction. XML Databases XML storage details

Part XII. Mapping XML to Databases. Torsten Grust (WSI) Database-Supported XML Processors Winter 2008/09 321

The PCAT Programming Language Reference Manual

Big Data 12. Querying

XML databases. Jan Chomicki. University at Buffalo. Jan Chomicki (University at Buffalo) XML databases 1 / 9

Pre-Discussion. XQuery: An XML Query Language. Outline. 1. The story, in brief is. Other query languages. XML vs. Relational Data

4. XML Processing. XML Databases 4. XML Processing, The XML Processing Model. 4.1The XML Processing Model. 4.1The XML Processing Model

Big Data 10. Querying

XQuery 1.0: An XML Query Language

Course: The XPath Language

The SPL Programming Language Reference Manual

Course: The XPath Language

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 27-1

XPath Expression Syntax

11. XML storage details Introduction Introduction Introduction Introduction. XML Databases XML storage details

1 <?xml encoding="utf-8"?> 1 2 <bubbles> 2 3 <!-- Dilbert looks stunned --> 3

SAX Simple API for XML

H2 Spring B. We can abstract out the interactions and policy points from DoDAF operational views

Index-Driven XQuery Processing in the exist XML Database

A Modular modular XQuery implementation

Chapter 13 XML: Extensible Markup Language

XML. Semi-structured data (SSD) SSD Graphs. SSD Examples. Schemas for SSD. More flexible data model than the relational model.

XML Technologies. Doc. RNDr. Irena Holubova, Ph.D. Web pages:

2.2 Syntax Definition

Introduction to Semistructured Data and XML. Overview. How the Web is Today. Based on slides by Dan Suciu University of Washington

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

4 SAX. XML Application. SAX Parser. callback table. startelement! startelement() characters! 23

Semi-structured Data. 8 - XPath

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

A Simple Syntax-Directed Translator

Spoke. Language Reference Manual* CS4118 PROGRAMMING LANGUAGES AND TRANSLATORS. William Yang Wang, Chia-che Tsai, Zhou Yu, Xin Chen 2010/11/03

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Programming Language Concepts, cs2104 Lecture 04 ( )

Navigating Input Documents Using Paths4

Introduction to XQuery and XML Databases

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Big Data Exercises. Fall 2016 Week 9 ETH Zurich

Table of Contents Chapter 1 - Introduction Chapter 2 - Designing XML Data and Applications Chapter 3 - Designing and Managing XML Storage Objects

XML: Extensible Markup Language

5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator

Querying purexml Part 1 The Basics

Chapter 5: XPath/XQuery Data Model

Relational Database Systems 2 5. Query Processing

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

1 Lexical Considerations

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Semantics of programming languages

Introduction to Parsing. Lecture 5

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

Semistructured Data and XML

SMURF Language Reference Manual Serial MUsic Represented as Functions

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Semantics of programming languages

Programming Lecture 3

XML Query Languages. Yanlei Diao UMass Amherst April 22, Slide content courtesy of Ramakrishnan & Gehrke, Donald Kossmann, and Gerome Miklau

IEEE LANGUAGE REFERENCE MANUAL Std P1076a /D3

XML Path Language (XPath)

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Functional programming with Common Lisp

L-System Fractal Generator: Language Reference Manual

CPS122 Lecture: From Python to Java

CS112 Lecture: Primitive Types, Operators, Strings

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College

UNIT- 3 Introduction to C++

Teiid Designer User Guide 7.5.0

Querying XML Documents. Organization of Presentation

Query Languages for XML

LECTURE 17. Expressions and Assignment

XPath. Asst. Prof. Dr. Kanda Runapongsa Saikaew Dept. of Computer Engineering Khon Kaen University

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

EZ- ASCII: Language Reference Manual

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Relational Databases

Database Languages and their Compilers

M359 Block5 - Lecture12 Eng/ Waleed Omar

COMP9321 Web Application Engineering. Extensible Markup Language (XML)

Informatics 1: Data & Analysis

Documentation for Quark xquery_model and related libraries Pet-Chean Ang and Anand Bhaskar 30 th May 2005

Decaf Language Reference Manual

Programming Languages Third Edition. Chapter 7 Basic Semantics

6.001 Notes: Section 8.1

XQuery Semantics CSE 232B. March 31, 2016

Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Transcription:

XML Databases 6. XML Query Languages II Silke Eckstein Andreas Kupfer Institut für Informationssysteme Technische Universität Braunschweig http://www.ifis.cs.tu-bs.de

6. XML Query Languages II 6.1 Introduction 6.2 XQuery Type System 6.3 Node Properties 6.4 Items and Sequences 6.5 Atomic Types and Node Types 6.6 XQuery Preliminaries 6.7 Iteration (FLWORs) 6.8 Overview and References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 2

6.1 Introduction Querying XML Documents "Querying XML data" essentially means to identify (or address) nodes, to test certain further properties of these nodes, then to operate on the matches, and finally, to construct result XML documents as answers. In the XML context, the language XQuery plays the role that SQL has in relational databases. XQuery can express all of the above constituents of XML querying: XPath, as an embedded sublanguage, expresses the locate and test parts; XQuery can then iterate over selected parts, operate on and construct answers from these. We will first look into the (XML-based) data model used by XQuery and XPath... XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 3

6.1 Introduction Motivating example Recall DilbertML (chap. 4): 1. "Find all bubbles with Wally being angry with Dilbert." Query: Starting from the root, locate all bubble elements somewhere below the panel element. Select those bubble elements with attributes @tone = "angry",@speaker = "Wally", and @to = "Dilbert" 2. "Find all strips featuring Dogbert." Query: Starting from the root, step down to the element prolog, then take a step down to element characters. Inside the latter, step down to all character elements and check for contents being equal to Dogbert. Note the locate, then test pattern in both queries. An XML parser (with DOM/SAX backend) is all we need to implement such queries. Tedious! XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 4

6.1 Introduction XQuery XQuery is a truly declarative language specifically designed for the purpose of querying XML data. As such, XML assumes the role that SQL occupies in the context of relational databases. XQuery exhibits properties known from database (DB) languages as well as from (functional) programming (PL) languages. The language is designed and formally specified by the W3C XQuery Working Group (http://www.w3.org/xml/xquery/). The first working draft documents date back to February 2001. TheXQuery specification has become a W3C Recommendation in January 2007. Members of the working group include Dana Florescu DB, Ioana Manolescu DB, Phil Wadler PL, Mary Fernandez DB+PL, Don Chamberlin DB (the "father" of SQL), Jérôme Simeon DB, Michael Rys DB, and many others. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 5

6.1 Introduction 1/2 Programming Language, 1/2 Query Language XQuery is a hybrid exhibiting features commonly found in programming as well as database query languages: Programming language features: explicit iteration and variable bindings (for in, let in) recursive, user-defined functions regular expressions, strong [static] typing ordered sequences (much like lists or arrays) Database query language features: filtering grouping, joins } expressed via nestedfor loops XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 6

6.1 Introduction History of XQuery XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 7

6. XML Query Languages II 6.1 Introduction 6.2 XQuery Type System 6.3 Node Properties 6.4 Items and Sequences 6.5 Atomic Types and Node Types 6.6 XQuery Preliminaries 6.7 Iteration (FLWORs) 6.8 Overview and References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 8

6.2 XQuery Type System The XQuery Data Model Like for any other database query language, before we talk about the operators of the language, we have to specify exactly what it is that these operate on... XQuery (and the other languages) use an abstract view of the XML data, the so-called XQuery data model. Data Model (DM) The XQuery DM determines which aspects of an XML document may be inspected and manipulated by an XQuery query. What exactly should the XQuery DM look like...? A simple sequence of characters or other lexical tokens certainly seems inappropriate (too fine-grained)! XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 9

6.2 XQuery Type System XQuery data model (1) Which aspects of XML data are relevant to queries? <x>symbols like & and![cdata[<]]> can be tricky in XML.</x> What is an adequate representation of XML element x? DOM style...? x text( Symbols like ) text( & ) text( and ) cdata( < ) text( can be tricky in XML. ) Faithfully preserves entities and CDATA sections, paying the price of creating more DM nodes during parsing. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 10

6.2 XQuery Type System XQuery data model (2) Which aspects of XML data are relevant to queries? <x>symbols like & and![cdata[<]]> can be tricky in XML.</x> XQuery style text("symbols like & and < can be tricky in XML.") x Do not distinguish between ordinary text, entities, and CDATA sections (the latter two are merely requirements of XML syntax). XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 11

6.2 XQuery Type System XQuery data model (3): untyped vs. typed An XML element containing an integer <x> 04<!-- unexpected comment -->2 </x> Untyped view... x text( LF 04 ) comment( unexpected comment ) text( 2 LF ) XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 12

6.2 XQuery Type System XQuery data model (3): untyped vs. typed An XML element containing an integer <x> 04<!-- unexpected comment -->2 </x> Typed view... integer(42) x XQuery can work with the typed view, if the input XML document has been validated against an XML Schema description. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 13

6. XML Query Languages II 6.1 Introduction 6.2 XQuery Type System 6.3 Node Properties 6.4 Items and Sequences 6.5 Atomic Types and Node Types 6.6 XQuery Preliminaries 6.7 Iteration (FLWORs) 6.8 Overview and References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 14

6.3 Node Properties XQuery DM: Node properties (1) A separate W3C document describes the XQuery DM in detail (http://www.w3.org/tr/xpath-datamodel/). In the XQuery DM, a tag in an XML document an element exhibits a number of properties, including: Property node-name parent children attributes string-value typed-value type-name Meaning tag name of this element parent element, may be empty children lists, may be empty set of attributes of this element, may be empty concatenation of all string values in content element value (after validation only) type name assigned by validation XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 15

6.3 Node Properties XQuery DM: Node properties (2) An XML element containing an integer <x> 04<!-- unexpected comment -->2 </x> Node properties of unvalidated element x node-name parent () children (t 1,c,t 2 ) attributes string-value typed-value type-name x Ø " LF 042 LF " " LF 042 LF " untypedatomic XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 16

6.3 Node Properties XQuery DM: Node properties (3) An XML element containing an integer <x> 04<!-- unexpected comment -->2 </x> Node properties of validated element x node-name x parent () children (t 1,c,t 2 ) attributes string-value Ø typed-value 42 type-name " LF 042 LF " integer XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 17

6.3 Node Properties XQuery: Access to the DM in a query XQuery provides various ways to access properties of nodes in a query. For example: access node-name name(<x>content here</x>) "x" access parent element (this is actually XPath functionality) <x>content here</x>/parent:: () access string value: string(<x>content here</x>) "content here" XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 18

6. XML Query Languages II 6.1 Introduction 6.2 XQuery Type System 6.3 Node Properties 6.4 Items and Sequences 6.5 Atomic Types and Node Types 6.6 XQuery Preliminaries 6.7 Iteration (FLWORs) 6.8 Overview and References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 19

6.4 Items and Sequences Items and sequences (1) Two data structures are pervasive in the XQuery DM: 1. Ordered, unranked trees of nodes (XML elements, attributes, text nodes, comments, processing instructions) and 2. ordered sequences of zero or more items. An XQuery item either is a node (of one of the kinds listed below), or an atomic value of one of the 50+ atomic types built into the XQuery DM. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 20

6.4 Items and Sequences Items and sequences (2) A sequence of n items x 1 is written in parentheses, comma-separated Sequence of length n and empty sequence (x 1, x 2,,x n ) () A single item x and the singleton sequence (x) are equivalent! Sequences cannot contain other sequences (i.e., nested sequences are implicitly flattened): Flattening, order (0, (), (1, 2), (3)) (0, 1, 2, 3) (0, 1) (1, 0) XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 21

6.4 Items and Sequences Sequence types (1) XQuery uses sequence types to describe the type of sequences: Sequence types t (simplified) t ::= empty-sequence() item occ occ ::= +? ε item ::= atomic node item() node ::= element(name) text() node()... name ::= QName atomic ::= integer string double... A QName (qualified name) denotes an element or attribute name, possibly with a name space prefix (e.g., ns:x). XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 22

6.4 Items and Sequences Sequence types (2) Sequence type examples Value Sequence type 42 integer, item() <x>foo</x> element(x), item() () empty-sequence(),integer ("foo", "bar") string+, item() (<x/>, <y/>) element( )+, node() In the table above, the most specific type is listed first. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 23

6. XML Query Languages II 6.1 Introduction 6.2 XQuery Type System 6.3 Node Properties 6.4 Items and Sequences 6.5 Atomic Types and Node Types 6.6 XQuery Preliminaries 6.7 Iteration (FLWORs) 6.8 Overview and References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 24

6.5 Atomic Types Items: atomic values XQuery, other than XPath 1.0 or XSLT which exclusively manipulate nodes, can also compute with atomic values (numbers, Boolean values, strings of characters,... ). XQuery knows a rich collection of atomic types (i.e., a versatile hierarchy of number types like fixed and arbitrary precision decimals, integers of different bit-widths, etc.). In this course, we will only cover a subset of this rich type hierarchy. The hierarchy of atomic types is rooted in the special type anyatomictype. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 25

6.5 Atomic Types Hierarchy of atomic types Atomic Type Hierachy (excerpt) anyatomictype untypedatomic string double decimal float boolean integer Numeric literals 12345 (: integer :) 12.345 (: decimal :) 12.345E0 (: double :) Boolean literals true() false() XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 26

6.5 Atomic Types Computing with untyped values Atomic values of type untypedatomic, which appear whenever text content is extracted from non-validated XML documents, are implicitly converted if they occur in expressions. Implicit extraction (known as atomization) of element content and conversion of values of type untypedatomic 42 + 1 type error (compile time) <x>42</x> + 1 43.0E0 (: double :) <x>fortytwo</x> + 1 conversion error (runtime) This behavior saves a lot of explicit casting in queries over nonvalidated XML documents. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 27

6.5 Node Types Items: nodes Just like XML, XQuery differentiates between several kinds of nodes: Six XML node kinds <element attribute="foo"> text <!--comment--> <?processing instruction?> </element> + The ("invisible") root node of any complete XML document is the so-called document node. In XQuery, a query may extract and construct nodes of all these kinds. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 28

6.5 Node Types Nodes: observable properties Each node kind has specific properties but a few important properties are shared by all kinds: Node identity and document order Each node has a unique node identity which is never modified. XQuery allows for node identity comparison using the operator is. All nodes are ordered relative to each other, determined by the so-called document order (XQuery operator <<). This orders nodes of the same tree according to a pre-order traversal. Nodes in different trees are ordered consistently. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 29

6.5 Node Types Node identity Node identity <x>foo</x> is <x>foo</x> false() Note: To compare items based on their value, XQuery offers the operators=andeq. Value comparison <x>foo</x> = <x>foo</x> true() XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 30

6.5 Node Types Document order <a> <b c="..." d="...">e</b> <f><!--g-->h</f> </a> a b f @c @d text("e") comment("g") text("h") Parent nodes precede their children and attributes (e.g., a << b and b << @d). << is transitive. Siblings are ordered with attributes coming first (e.g., b << f, @d << text("e")), but the relative order of attributes (@c, @d) is implementationdependent. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 31

6.5 Node Types Notes on document order XML documents always carry this implicit order of their contents. Typical XML processing follows this order when accessing components of an XML document (see, e.g., SAX parsing). Often, operations on XML documents are supposed to deliver their results also in this particular order. Document order is part of the (formal) semantics of many XML related languages. Contrast this with relational database query languages, where set-orientation always gives the freedom to the query processor to access and deliver tuples in arbitrary order! We will (later) see that document order has far-reaching consequences on XML query processing. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 32

6. XML Query Languages II 6.1 Introduction 6.2 XQuery Type System 6.3 Node Properties 6.4 Items and Sequences 6.5 Atomic Types and Node Types 6.6 XQuery Preliminaries 6.7 Iteration (FLWORs) 6.8 Overview and References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 33

6.6 XQuery Preliminaries XQuery-Preliminaries Remember: XPath is part of XQuery (as a sublanguage). Some constructs that have not previously been discussed, yet are not within the core of our focus on XQuery include: Comparisons: any XQuery expression evaluates to a sequence of items. Consequently, many XQuery concepts are prepared to accept sequences (as opposed to single items). General comparisons: The general comparison ᶿ ᶿ e 1 e 2 with {=,!=, <, <=, >=,>} yields true() if any of the items in the sequences e 1,2 compare true (existential semantics). XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 34

6.6 XQuery Preliminaries Comparisons General comparison examples (1,2,3) > (2,4,5) true() (1,2,3) = 1 true() () = 0 false() 2 <= 1 false() (1,2,3)!= 3 true() The six value comparison operators eq, ne, lt, le, ge, gt compare single items by value (atomization!): Value comparisons (1,2)!= (1,2) true() not((1,2) = (1,2)) false() 2 gt 1.0 true() <x>42</x> eq <y>42</y> true() (0,1) eq 0 (type error) XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 35

6.6 XQuery Preliminaries More on comparisons... Note: The existential semantics of the general comparison operators may lead to unexpected behavior: Surprises (1,2,3) = (1,3) true() a ("2",1) = 1 true() or (impl. dependent) a) For an item-by-item comparison use deep-equal(). XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 36

6.6 XQuery Preliminaries Node comparisons... based on identity and document order: e 1 is e 2 nodese 1,2 identical? e 1 << e 2 nodee 1 before e 2? e 1 >> e 2 nodee 1 after e 2? Node comparison examples <x>42</x> eq <x>42</x> true() <x>42</x> is <x>42</x> false() root(e 1 ) is root(e 2 ) let $a := <x><y/></x> return $a << $a/y nodes e 1,2 in same tree? true() XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 37

6.6 XQuery Preliminaries Working with sequences XQuery comes with an extensive library of builtin functions to perform common computations over sequences: Common sequence operations Function Example count count((0,4,2)) 3 max max((0,4,2)) 4 subsequence subsequence((1,3,5,7),2,3) (3,5,7) empty exists empty((0,4,2)) false() exists((0,4,2)) true() distinct-values distinct-values((4,4,2,4)) (4,2) to (1 to 10)[. mod 2 eq 1] (1,3,5,7,9) XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 38

6.6 XQuery Preliminaries Arithmetics Only a few words on arithmetics XQuery meets the common expectation here. Points to note: 1. Infix operators: +, -,, div, idiv (integer division), 2. operators first atomize their operands, then perform promotion to a common numeric type, 3. if at least one operand is (), the result is (). Examples and pitfalls <x>1</x> + 41 42.0 () * 42 () (1,2) - (2,3) (type error) x-42./child::x-42 (use x - 42) x/y./child::x/child::y (use x div y) XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 39

6. XML Query Languages II 6.1 Introduction 6.2 XQuery Type System 6.3 Node Properties 6.4 Items and Sequences 6.5 Atomic Types and Node Types 6.6 XQuery Preliminaries 6.7 Iteration (FLWORs) 6.8 Overview and References XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 40

6.7 Iteration (FLWORs) XQuery Iteration: FLWORs Remember that XPath steps perform implicit iteration: in cs/e, evaluation of e is iterated with '.' bound to each item in cs in turn. XPath subexpressions aside, iteration in XQuery is explicit via the FLWOR ("flower") construct. The versatile FLWOR is used to express nested iteration, joins between sequences (of nodes), groupings, orderings beyond document order, etc. In a sense, FLWOR assumes the role of the SELECT-FROM- WHERE block in SQL. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 41

6.7 Iteration (FLWORs) FLWOR: Iteration via for in Explicit iteration Explicit iteration is expressed using the for in construct: a for $v [at $p] in e 1 return e 2 If e 1 evaluates to the sequence (x 1,...,x n ), the loop body e 2 is evaluated n times with variable $v bound to each x i [and $p bound to i ] in order. The results of these evaluations are concatenated to form a single sequence. a the construct 'at $p ' is optional. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 42

6.7 Iteration (FLWORs) Iteration examples for $x in (3,2,1) return ($x," ") for $x in (3,2,1) return $x," " (3," ",2," ",1," ") (3,2,1," ") for $x in (3,2,1) (3,"a",3,"b", return for $y in ("a","b") 2,"a",2,"b", return ($x,$y) 1,"a",1,"b") FLWOR : Abbreviations for $v 1 in e 1 return for $v 2 in e 2 return e 3 for $v 1 in e 1 for $v 2 in e 2 return e 3 for $v 1 in e 1, $v 2 in e 2 return e 3 XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 43

6.7 Iteration (FLWORs) FLWOR: Iteration via for in Purpose of this query Q? max( for $i in cs/descendant-or-self::*[not(*)] return count($i/ancestor::*) ) A sample cs a Annotated sample cs a b c b c d e g f h d f 1 3 e g 3 h 3 Answer XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 44

6.7 Iteration (FLWORs) FLWOR: Iteration via for in Return every other item in sequence These queries both return the items at odd positions in the input sequence e: for $i in (1 to count(e))[. mod 2 eq 1] return e[$i] for $i at $p in e return if ($p mod 2) then e[$p] else () Remember: ebv(0) = false() ebv: effective boolean value XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 45

6.7 Iteration (FLWORs) FLWOR: Variable Binding via let := Note that in the examples on the last slide, expression e is re-evaluated count(e)/ 2 times although e is constant in the loop. Variable bindings The result of evaluating an expression e 1 may be bound to a variable $v via let: let $v := e 1 return e 2 evaluates e 2 with free occurrences of $v replaced by e 1. for and let clauses may be freely intermixed. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 46

6.7 Iteration (FLWORs) FLWOR: Variable Binding via let := Iteration vs. variable binding for $x in (3,2,1) return ($x,"*") (3,"*",2,"*",1,"*" ) let $x := (3,2,1) (3,2,1,"*") return ($x,"*") " Every other item" revisited (flip back two slides) The following hoists the constant e out of the loop body: let $seq := e return for $i at $p in $seq return if ($p mod 2) then $seq[$p] else () XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 47

6.7 Iteration (FLWORs) Adding a where clause Inside loop bodies, the idiom if (p) then eelse () is so common that FLWOR comes with a SQL-like where clause to address this. A where clause If ebv(p) evaluates to false() under the current variable bindings, the current iteration does not contribute to the result: for $v in e 1 where p return e 2 for $v in e 1 return if (p) then e 2 else () XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 48

6.7 Iteration (FLWORs) Explicit vs. implicit iteration XPath: implicit iteration a[@b = "foo"]/c[2]/d[@e = 42] Equivalent nested FLWOR blocks for $a in a where $a/@b = "foo" return for $c at $p in $a/c where $p = 2 return for $d in $c/d where $d/@e = 42 return $d NB. Unlike the XPath step operator /, for does not change the context item '. ' XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 49

6.7 Iteration (FLWORs) FLWOR: Reorder iteration result via order by In a FLWOR block for $vin e 1 return e 2, the order of e 1 determines the order of the resulting sequence. Reordering via order by In the FLWOR block for $v in e 1 order by e 3 [ascending descending][empty greatest least] return e 2 the value (atomization!) of e 3 determines the order in which the bindings of $v are used to evaluate e 2. XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 50

6.7 Iteration (FLWORs) FLWOR: Reordering examples An order by "no-op": reordering by sequence order for $xat $p in (5,3,1,4,2) order by $p return $x All bound variables in scope in order by for $xat $p in (5,3,1,4,2) order by $p + $x return $x (5,3,1,4,2) (1,3,5,2,4) Reordering as in SQL's ORDER BY for $xat $p in (5,3,1,4,2) order by $x return $x (1,2,3,4,5) XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 51

6.7 Iteration (FLWORs) FLWOR: Reordering examples Value-based reordering of an XPath step result This query reorders the result of the XPath location step descendant::b based on (string) value. Which result is to be expected? let $a := <a> <b id="0">42</b> Answer <b id="1">5</b> <b id="2"/> <b id="3">3</b> <b id="4">1</b> </a> for $b in $a/descendant::b order by $b/text() empty greatest return $b/@id XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 52

6.8 Overview Introduction and Basics 1. Introduction 2. XML Basics 3. Schema Definition 4. XML Processing Querying XML 5. XPath & SQL/XML Queries 6. XQuery Data Model 7. XQuery XML Updates 8. XML Updates & XSLT Producing XML 9. Mapping relational data to XML Storing XML 10. XML storage 11. Relational XML storage 12. Storage Optimization Systems 13. Technology Overview XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 53

6.9 References "Database-Supported XML Processors", Th. Grust Lecture, Uni Tübingen, WS 08/09 "Querying XML XQuery, XPath and SQL/XML in Context" J. Melton, S. Buxton Morgan Kaufmann/Elsevier, 2006, ISBN 978-1558607118 DB2 purexml CookBook [NK09] Matthias Nicola and Pav Kumar-Chatterjee IBMPress, 2009, ISBN 9780138150471 XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 54

Questions, Ideas, Comments Now, or... Room: IZ 232 Office our: Tuesday, 12:30 13:30 Uhr or on appointment Email: eckstein@ifis.cs.tu-bs.de XML Databases Silke Eckstein Institut für Informationssysteme TU Braunschweig 55