+ All Categories
Home > Documents > Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA...

Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA...

Date post: 18-Jan-2018
Category:
Upload: bathsheba-nash
View: 222 times
Download: 0 times
Share this document with a friend
Description:
3 Usage scenarios zHuman-readable documents yperform queries on structured documents and collections of documents, such as technical manuals, xto retrieve individual documents, xto generate tables of contents, xto search for information in structures found within a document, or xto generate new documents as the result of a query
61
Processing of structured documents Part 6
Transcript
Page 1: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

Processing of structured documents

Part 6

Page 2: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

2

XML Query language

W3C 20.12.2001: working drafts A data model (XQuery 1.0 and XPath 2.0) XQuery 1.0 Formal Semantics (June 2001) XQuery 1.0: A Query Language for XML

influenced by the work of many research groups and query languages

goal: a query language that is broadly applicable across all types of XML data sources

Page 3: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

3

Usage scenarios

Human-readable documents perform queries on structured documents and

collections of documents, such as technical manuals,

to retrieve individual documents, to generate tables of contents, to search for information in structures found within a

document, or to generate new documents as the result of a query

Page 4: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

4

Usage scenariosData-oriented documents

perform queries on the XML representation of database data, object data, or other traditional data sources

to extract data from these sourcesto transform data into new XML representationsto integrate data from multiple heterogeneous data sources

the XML representation of data sources may be either physical or virtual:

data may be physically encoded in XML, or an XML representation of the data may be produced

Page 5: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

5

Usage scenarios

Mixed-model documents perform both document-oriented and data-

oriented queries on documents with embedded data, such as catalogs, patient health records, employment records

Administrative data perform queries on configuration files, user

profiles, or administrative logs represented in XML

Native XML repositories (databases)

Page 6: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

6

Usage scenarios

Filtering streams perform queries on streams of XML data to process

the data (logs of email messages, network packets, stock market data, newswire feeds, EDI)

to filter and route messages represented in XMLto extract data from XML streamsto transform data in XML streams

DOM perform queries on DOM structures to return sets

of nodes that meet the specified criteria

Page 7: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

7

Usage scenarios

Multiple syntactic environments queries may be used in many environments a query might be embedded in a URL, an XML

page, or a JSP or ASP page represented by a string in a program written in

a general-purpose programming language provided as an argument on the command-line

or standard input

Page 8: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

8

RequirementsQuery language syntax

the XML Query Language may have more than one syntax binding

one query language syntax must be convenient for humans to read and write

one query language syntax must be expressed in XML in a way that reflects the underlying structure of the query

Declarativity the language must be declarative it must not enforce a particular evaluation strategy

Page 9: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

9

RequirementsReliance on XML Information Set

the XML Query data model relies on information provided by XML Processors and Schema Processors

it must ensure that it does not require information that is not made available by such processors

Datatypes the data model must represent both XML 1.0 character data

and the simple and complex types of the XML Schema specification

Schema availability queries must be possible whether or not a schema is

available

Page 10: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

10

Requirements: functionality Support operations (selection, projection, Support operations (selection, projection,

aggregation, sorting, etc.) on all data types: aggregation, sorting, etc.) on all data types: Choose a part of the data based on content or structure Also operations on hierarchy and sequence of document

structures Structural preservation and transformation:Structural preservation and transformation:

Preserve the relative hierarchy and sequence of input document structures in the query results

Transform XML structures and create new XML structures Combination and joining:Combination and joining:

Combine related information from different parts of a given document or from multiple documents

Page 11: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

11

Requirements: functionality References:References:

Queries must be able to traverse intra- and inter-document references

Closure property:Closure property: The result of an XML document query is also an XML

document (usually not valid but well-formed) The results of a query can be used as input to another

query Extensibility:Extensibility:

The query language should support the use of externally defined functions on all datatypes of the data model

Page 12: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

12

XQuery

Design goals: a small, easily implementable language queries are concise and easily understood flexible enough to query a broad spectrum of

XML information sources (incl. both databases and documents)

a human-readable query syntax features borrowed from many languages

Quilt, Xpath, XQL, XML-QL, SQL, OQL, Lorel, ...

Page 13: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

13

XQuery vs. another XML activities

XQuery 1.0 and XPath 2.0 Data Modelsemantics of XQuery is defined in the

XQuery Formal Semanticstype system is based on the type system of

XML Schemapath expressions (for navigating in hierarchic

documents) = path expressions of XPath 2.0 the XML-based syntax is described in

XQueryX

Page 14: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

14

XQuery

A query is represented as an expression several kinds of expressions -> several formsexpressions can be nested with full generalitythe input and output of a query are instances

of a data model (XQuery 1.0 and XPath 2.0 Data Model) a fragment of a document or a collection of

documents may lack a common root and may be modeled as an ordered forest of nodes

Page 15: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

15

An instance of the Data Model - an ordered forest

Page 16: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

16

XQuery expressionspath expressionselement constructorsFLWR (”flower”; for-let-where-return)

expressionsexpressions involving operators and functionsconditional expressionsquantified expressionsexpressions that test or modify datatypes

Page 17: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

17

Path expressions

the result of a path expression is an ordered list of nodes (document order) each node includes its descendant nodes ->

the result is an ordered forestthe top-level nodes in the result are

ordered according to their position in the original hierarchy (in top-down, left-right order)

no duplicate nodes

Page 18: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

18

Element constructorsAn element constructor creates an XML

elementconsists of a start tag and an end tag,

enclosing an optional list of expressions that provide the content of the element the start tag may also specify the values of one of

more attributestypical use:

nested inside another expression that binds variables that are used in the element constructor

Page 19: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

19

Example

Generate an <emp> element containing an ”empid” attribute and nested <name> and <job> elements. The values of the attribute and nested elements are specified elsewhere.

<emp empid = {$id}> <name> {$n} </name> <job> {$j} </job></emp>

Page 20: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

20

Element constructors

In an element constructor, curly braces {} delimit enclosed expressions, distinguishing them from literal text

enclosed expressions are evaluated and replaced by their value, whereas material outside curly braces is simply treated as literal text

an enclosed expression may evaluate to any sequence of nodes and/or simple values

Page 21: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

21

Computed element constructors

Generate an element with a computed name, containing nested elements named <description> and <price>

element {$tagname}{ <description> {$d} </description> , <price> {$p} </price>}

Page 22: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

22

FLWR expressions

Constructed from for, let, where, and return clauses

~SQL select-from-whereclauses must appear in a specific order

1. for/let, 2. where, 3. returna FLWR expression binds values to one or

more variables and then uses these variables to construct a result (in general, an ordered forest of nodes)

Page 23: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

23

A flow of data in a FLWR expression

Page 24: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

24

for clauses

A for clause introduces one or more variables, associating each variable with an expression that returns a list of nodes (e.g. a path expression)

the result of a for clause is a list of tuples, each of which contains a binding for each of the variables

each variable in a for clause can be thought of as iterating over the nodes returned by its respective expression

Page 25: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

25

let clauses

A let clause is also used to bind one or more variables to one or more expressions

a let clause binds each variable to the value of its respective expression without iteration

results in a single binding for each variableCompare:

for $x in /library/book -> many bindings (books) let $x := /library/book -> single binding (a list of

books)

Page 26: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

26

for/let clauses

A FLWR expression may contain several for and let clauses each of these clauses may contain references to

variables bound in previous clausesthe result of the for/let sequence:

an ordered list of tuples of bound variablesthe number of tuples generated by the for/let

sequence: the product of the cardinalities of the node-lists

returned by the expressions in the for clauses

Page 27: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

27

for/let clauses

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

Result:<out> <one/> <two/> <three/></out>

Page 28: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

28

for/let clauses

for $s in (<one/>, <two/>, <three/>)return <out>{$s}</out>

Result:<out><one/></out><out><two/></out><out><three/></out>

Page 29: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

29

for/let clauses

for $i in (1,2), $j in (3,4)

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

Result:

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

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

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

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

Page 30: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

30

where clause

Each of the binding tuples generated by the for and let clauses can be filtered by an optional where clause

only those tuples for which the condition in the where clause is true are used to invoke the return clause

the where clause may contain several predicates connected by and, or, and not predicates usually contain references to the bound

variables

Page 31: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

31

where clause

Variables bound by a for clause represent a single node -> scalar predicates, e.g. $p/color = ”Red”

Variables bound by a let clause may represent lists of nodes -> list-oriented predicates, e.g. avg($p/price)

> 100

Page 32: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

32

return clause

The return clause generates the output of the FLWR expression a node, an ordered forest of nodes, primitive

valueis executed on each tuplecontains an expression that often contains

element constuctors, references to bound variables, and nested subexpressions

Page 33: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

33

Examples

Assume: a document named ”bib.xml”contains a list of <book> elementseach <book> contains a <title> element,

one or more <author> elements, a <publisher> element, a <year> element, and a <price> element

Page 34: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

34

List the titles of books published by Addison Wesley after 1998

<bib>{ for $b in document(”bib.xml”)//book where $b/publisher = ”Addison Wesley” and $b/year > ”1998” return <book year= {$b/year}>

{$b/title} </book>}</bib>

Page 35: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

35

Result could be...

<bib><book year=”1999”> <title>TCP/IP Illustrated</title></book><book year=”2000”> <title>Advanced Programming in the Unix environment</title></book></bib>

Page 36: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

36

List each publisher and the average price of its books

for $p in distinct-values(document(”bib.xml”)//publisher)let $a := avg(document(”bib.xml”)//book[publisher = $p]/price)return

<publisher>{<name> {$p/text()} </name> ,<avgprice> {$a} </avgprice>

}</publisher>

Page 37: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

37

List the publishers who have published more than 100 books

<big_publishers>{for $p in distinct-values(document(”bib.xml”)//publisher)let $b := document(”bib.xml”)//book[publisher = $p]where count($b) > 100return $p

}</big_publishers>

Page 38: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

38

Invert the structure of the input document so that each distinct author element contains a list of book-titles

<author_list> { let $input := document(”bib.xml”) for $a in distinct-values($input//author) return <author> { <name>{ $a/text() }</name>,

<books>{ for $b in $input//book where $b/author = $a

return $b/title } </books> }</author>}</author_list>

Page 39: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

39

Make an alphabetic list of publishers, within each publisher, make a list of books (title & price), in descending order by price

for $p in distinct-values(document(”bib.xml”)//publisher)return <publisher>

<name> {$p/text()} </name> {for $b in document(”bib.xml”)//book[publisher = $p] return

<book> {$b/title} {$b/price} </book> sortby(price descending)

} </publisher> sortby(name)

Page 40: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

40

Operators in expressions

Expressions can be constructed using infix and prefix operators; nested expressions inside parenthesis can serve as operands

arithmetic and logical operators; collection operators (union, intersect, except)

Page 41: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

41

Queries on sequenceXQuery uses the precedes and follows

operators to express conditions based on sequence

the following example involves a surgical report that contains procedure, incision and anesthesia elements

the query returns a critical sequence that contains all elements and nodes found between the 1st and 2nd incisions of the 1st procedure

Page 42: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

42

Queries on sequence

<critical-sequence>{

let $proc := //procedure[1]

for $n in $proc//node()

where $n follows ($proc//incision)[1]

and $n precedes ($proc//incision)[2]

return $n

}</critical-sequence>

Page 43: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

43

Conditional expressions

if-then-elseconditional expressions can be nested and

used wherever a value is expectedassume: a library has many holdings

(element <holding> with a ”type” attribute that identifies its type, e.g. book or journal). All holdings have a title and other nested elements that depend on the type of holding

Page 44: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

44

Make a list of holdings, ordered by title. For journals, include the editor, and for all others, include the author

for $h in //holdingreturn <holding>

{$h/title}, {if ($h/@type = ”Journal”) then $h/editor else $h/author}</holding> sortby (title)

Page 45: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

45

Quantifiers

It may be necessary to test for existence of some element that satisfies a condition, or to determine whether all elements in some collection satisfy a condition

-> existential and universal quantifiers

Page 46: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

46

Find titles of books in which both sailing and windsurfing are mentioned in the same paragraph

for $b in //bookwhere some $p in $b//para satisfies

contains($p/text(), ”sailing”)and contains($p/text(), ”windsurfing”)

return $b/title

Page 47: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

47

Find titles of books in which sailing is mentioned in every paragraph

for $b in //book where every $p in $b//para satisfies

contains($p/text(), ”sailing”) return $b/title

Page 48: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

48

FilteringFunction filter (in XQuery core function library)one parameter

expression that evaluates to an ordered forest of nodes

filter returns copies of some of the nodes in the original document order and hierarchy are preserved

nodes that are copied: nodes that are present at any level in the original

document and are also top-level nodes of the forest returned by the parameter

Page 49: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

49

Action of filter on a hierarchy

filter (C\\(A | B))

Page 50: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

50

Prepare a table of contents for the document ”cookbook.xml”, containing nested sections and their title

let $b := document(”cookbook.xml”)return <toc>{ filter($b// (section | section/title | section/title/text() )) }</toc>

Page 51: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

51

Other built-in functions A core library of built-in functionsdocument: returns the root node of a named

documentall functions of the XPath core function library all the aggregation functions of SQL

avg, sum, count, max, min…distinct-values: eliminates duplicates from a listempty: returns true if and only if its argument is

an empty list

Page 52: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

52

User-defined functions Users are allowed to define own functionseach function definition must

declare the datatypes of its parameters and result provide an expression that defines how the result of

the function is computed from its parameterswhen a function is invoked, its arguments must

be valid instances of the declared parameter types

the result must also be a valid instance of its declared type

Page 53: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

53

Functions Example: assume a purchase order bound

to variable $po1

<complexType name=”USAddress”>…<element name=”shipTo” type=”po:USAddress”/><element name=”billTo” type=”po:USAddress”/>

define function timezone(element of type po:USAddress $a)returns integer {...}

call: timezone($po1/shipTo) - timezone($po1/billTo)

Page 54: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

54

Querying relational data

A lot of data is stored in relational databasesan XML query language should be able to

access this dataExample: suppliers and parts

Table S: supplier numbers (sno) and names (sname) Table P: part numbers (pno) and descriptions

(descrip) Table SP: relationships between suppliers and the

parts they supply, including the price (price) of each part from each supplier

Page 55: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

55

One possible XML representation of relational data

Page 56: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

56

SQL vs. XQuery

SQL:

XQuery:

SELECT pnoFROM pWHERE descrip LIKE ’Gear’ORDER BY pno;

for $p in document(”p.xml”)//p_tuplewhere contains($p/descrip, ”Gear”)return $p/pno sortby(.)

Page 57: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

57

Grouping

Many relational queries involve forming data into groups and applying some aggregation function such as count or avg to each group

in SQL: GROUP BY and HAVING clausesExample: Find the part number and

average price for parts that have at least 3 suppliers

Page 58: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

58

Grouping: SQL

SELECT pno, avg(price) AS avgpriceFROM spGROUP BY pnoHAVING count(*) >= 3ORDER BY pno;

Page 59: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

59

Grouping: XQuery

for $pn in distinct-values(document(”sp.xml”)//pno)let $sp := document(”sp.xml”)//sp_tuple[pno = $pn]where count($sp) >= 3return

<well_supplied_item> {$pn} <avgprice> {avg($sp/price)} </avgprice)</well_supplied_item> sortby(pno)

Page 60: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

60

Joins Joins combine data from multiple sources into a

single query result Example: Return a ”flat” list of supplier names

and their part descriptions, in alphabetic orderfor $sp in document(”sp.xml”)//sp_tuple, $p in document(”p.xml”)//p_tuple[pno = $sp/pno], $s in document(”s.xml”)//s_tuple[sno = $sp/sno]return <sp_pair>{

$s/sname , $p/descrip

}<sp_pair> sortby (sname, descrip)

Page 61: Processing of structured documents Part 6. 2 XML Query language zW3C 20.12.2001: working drafts yA data model (XQuery 1.0 and XPath 2.0) yXQuery 1.0 Formal.

61

Example: ”left outer join” Return names of all the suppliers in alphabetic order,

including those that supply no parts; inside each supplier element, list the descriptions of all the parts it supplies, in alphabetic order

for $s in document(”s.xml”)//s_tuplereturn <supplier>{ $s/sname, for $sp in document(”sp.xml”)//sp_tuple[sno = $s/sno], $p in document(”p.xml”)//p_tuple[pno = $sp/pno] return $p/descrip sortby(.) }</supplier> sortby(sname)


Recommended