query languages for xml: xquery

53
Query Languages for XML: XQuery Adrian Pop, Paul Pop Computer and Information Science Dept. Linköpings universitet

Upload: gary

Post on 18-Jan-2016

91 views

Category:

Documents


6 download

DESCRIPTION

Query Languages for XML: XQuery. Adrian Pop, Paul Pop Computer and Information Science Dept. Linköpings universitet. Outline. Motivation XML applications, types of queries Approaches Requirements on a query language Path expressions, the basic building block - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Query Languages  for XML: XQuery

Query Languages for XML: XQuery

Adrian Pop, Paul PopComputer and Information Science Dept.

Linköpings universitet

Page 2: Query Languages  for XML: XQuery

2

Outline

Motivation XML applications, types of queries

Approaches Requirements on a query language Path expressions, the basic building block XML query languages: XML-QL, YATL, Lorel, XQL

XQuery Background, history Concepts, examples

FLWR expressions FOR and LET expressions Collections and sorting

Available software, demo Examples

XQuery vs. XSLT Summary

Page 3: Query Languages  for XML: XQuery

3

Motivation

XML applications Representing many types of information, many

sources Structured and semi-structured documents Relational databases Object repositories

Information has to be Accessed, filtered, grouped, transformed, etc.

Query languages are needed! Retrieve and interpret information Diverse sources

Querying a database is different from transforming a document

Page 4: Query Languages  for XML: XQuery

4

Document World vs. Database World

Two worlds, two querying approaches XML-as-document

Roots in SGML Queried using path expressions

XML-as-data Middleware, interface to databases Queried with SQL-like constructs

XML query language has to work in both worlds A query language for XML should work across all types of

XML data sources and applications

Problem Exiting query languages designed for specific types of data

Robust for those types, weak for other

Page 5: Query Languages  for XML: XQuery

5

Types of Queries

W3C specification: Important classes of queries Filtering

Compute a table of contents for a document

Joins Combine data from multiple sources in a single result

Grouping Forming data into groups, applying aggregate function

like “average” or “count”

Queries on sequence Queries where the sequence, hierarchy, (i.e.

precedence relationships) are important

Page 6: Query Languages  for XML: XQuery

6

Requirements on a Query Language

Output: a query language should output XML Composition of queries! Views can be defined via a single query Transparent to applications

Server-side processing Selection: choosing a document, element, based on

content, structure or attributes; Extraction: pulling out particular elements of a document; Reduction: removing selected sub-elements of an element; Restructuring: Constructing a new set of element

instances to hold queried data; Combination: Merging two or more elements into one; should all be possible in a single query.

No schema required / exploit available schema Queries should work on XML data when there is no schema,

DTD Use the exiting schema for detecting errors at compile time

Page 7: Query Languages  for XML: XQuery

7

Requirements on a Query Language, Cont.

Preserve order and association A query should preserve the order of elements, grouping

Programmatic manipulation Queries will be constructed via programs, interfaces;

programs should in an easy fashion with the representation of queries

XML representation Mutually embedding with XML XLink and XPointer cognizant Namespace alias independence

A query should not be dependent on namespace aliases local to an XML document

Support for new datatypes Suitable for metadata

Page 8: Query Languages  for XML: XQuery

8

Path Expressions

Query language for XML, semi-structured data Semi-structured data modeled as a edge-labeled directed

graph Ability to reach to arbitrary depths in the data graph

Achieved using “path expressions”

Path expressions: basic building block of a query language

A sequence of edge labels l1, l2, …, ln A query, whose result for a given data graph is a set of nodes Can be specified based on some properties

Property of the path: the path must traverse the book edge Property of an individual edge label: the label contains the

substring “Victor” Regular expressions are used to describe path properties

Limitations Cannot create new nodes in the database Cannot perform “Joins” Cannot test values stored in a database

Page 9: Query Languages  for XML: XQuery

9

Path Expressions

Data, modeled as an edge-labeleddirected graph

&o1

&o12 &o24 &o29

&o43

&o70 &o71

&96

&243 &206

&25

“Serge” “Abiteboul”

1997

“Victor” “Vianu” 122 133

paper bookpaper

references

references references

authortitle year httpauthor

authorauthor

title publisherauthor

authortitle

page

firstnamelastname firstname lastname first

last

Bib

&o44 &o45 &o46

&o47 &o48 &o49 &o50 &o51

&o52

Bib.paper={&o12,&o29}Bib.book.publisher={&o51}Bib.paper.author.lastname={&o71,&206}

Page 10: Query Languages  for XML: XQuery

10

Regular Path Expressions

R ::= label | _ | R.R | (R|R) | R* | R+ | R?

Examples: Bib.(paper|book).author Bib.book.author.lastname? Bib.book.(references)*.author Bib.(_)*.zip

Page 11: Query Languages  for XML: XQuery

11

XML Query Languages

Semistructured databases XML-QL

A. Deutsch, M. Fernandez, D. Florescu, A. Levy, and D. Suciu. A query language for XML, http://www.research.att.com/~mff/files/final.html

YATLS. Cluet, S. Jacqmin and J. Siméon The New YATL: Design and Specifications. Working draft.

LorelS. Abiteboul, D. Quass, J. McHugh, J. Widom, and J. Wiener. The Lorel query language for semistructured data, ftp://db.stanford.edu/pub/papers/lorel96.ps

Structured text, search techniques XQL

J. Robie. The design of XQL, 1999, http://www.texcel.no/whitepapers/xql-design.html

Page 12: Query Languages  for XML: XQuery

12

XML Query Examples <bib> <book year="1994">  <title>TCP/IP Illustrated</title> <author><last>Stevens</last>   <first>W.</first></author>  <publisher>Addison-Wesley</publisher> <price>65.95</price> </book> <book year="1992"> <title>Advanced Programming the Unix environment</title> <author><last>Stevens</last><first>W.</first>   </author>  <publisher>Addison-Wesley</publisher> <price>65.95</price> </book> <book year="2000"> <title>Data on the Web</title> <author><last>Abiteboul</last><first>Serge</first></author> <author><last>Buneman</last><first>Peter</first></author> <author><last>Suciu</last><first>Dan</first></author> <publisher>Morgan Kaufmann Publishers</publisher> <price>39.95</price> </book>- <book year="1999"> <title>The Economics of Technology and Content for Digital

TV</title> <editor><last>Gerbarg</last><first>Darcy</first> <affiliation>CITI</affiliation> </editor> <publisher>Kluwer Academic Publishers</publisher> <price>129.95</price> </book></bib>

Example data:list of books

Page 13: Query Languages  for XML: XQuery

13

XML Query Examples, Cont.

Query List books published by Addison-Wesley after 1991,

including their year and title. Result:

<?xml version="1.0" ?> <result> <bib> <book year="1994">  <title>TCP/IP Illustrated</title> </book> <book year="1992"> <title>Advanced Programming in the Unix

environment</title>   </book>  </bib>  </result>

Page 14: Query Languages  for XML: XQuery

14

Features of Query Languages

A query has three parts pattern clause

matches nested elements in the input document and binds variables filter clause

tests the bound variables constructor clause

specifies the result in terms of the bound variables

Join operator Combine data from different portions of documents

Path expressions Querying without precise knowledge of the document structure

Other useful features: to check for the absence of information, e.g., missing fields. Use of arbitrary external functions, such as aggregation

functions, string comparison functions, etc. Use of navigation operators, simplify handling data with

references.

Page 15: Query Languages  for XML: XQuery

15

XML-QL

CONSTRUCT <bib> { WHERE <bib> <book year=$y> <title>$t</title> <publisher><name>Addison-Wesley</name></publisher> </book> </bib> IN "www.bn.com/bib.xml", $y > 1991 CONSTRUCT <book year=$y><title>$t</title></book>} </bib>

patterns and filters appear in the WHERE clause the constructor appears in the CONSTRUCT clause The result of the inner WHERE clause is a relation, that maps

variables to tuples of values that satisfy the clause all pairs of year and title values bound to ($y, $t) that satisfy the

clause The result contains one <book> element for each book that

satisfies the WHERE clause of the inner query, one for each pair ($y, $t)

Page 16: Query Languages  for XML: XQuery

16

YATL

make bib [ *book [ @year [ $y ], title [ $t ] ] ]match "www.bn.com/bib.xml" with bib [ *book [ @year [ $y ], title [ $t ] ], publisher [ name [ $n ] ] ] where $n = "Addison-Wesley" and $y > 1991

the constructor appears in the make clause patterns appear in the match clause

a bib element may have many book elements, but that each book element has

one year attribute, one publisher element, and one title element filters appear in the where clause

Page 17: Query Languages  for XML: XQuery

17

Lorel

select xml(bib:{(select xml(book:{@year:y, title:t})from bib.book b, b.title t, b.year ywhere b.publisher = "Addison-Wesley" and y > 1991)})

constructor appears in the select clause patterns appear in the from clause both patterns and filters appear in the where clause.

bib is used as the entry point for the data in the XML document

The from clause binds variables to the element ids of elements denoted by the given pattern, and the where clause selects those elements that satisfy the given filters

The select clause constructs a new XML book element with a year attribute and a title element.

Page 18: Query Languages  for XML: XQuery

18

XQL

document("http://www.bn.com")/bib { book[publisher/name="Addison-Wesley" and @year>1991] { @year | title }}

XQL: from the “document world” The pattern document("http://www.bn.com")/bib

selects all top-level bib elements evaluates the nested expression for each such element

selects the book elements that are children of a bib element and that satisfy the filter clause in brackets

XQL does not have a constructor clause; the pattern expressions determine the result of the query

the inner-most expression: the book's year attribute and title element

Page 19: Query Languages  for XML: XQuery

19

XQuery: An XML Query Language

W3C standard http://www.w3.org/TR/xquery Derived from Quilt

Jonathan Robie, Don Chamberlin, and Daniela Florescu Based on XML-QL

Relevant W3C documents XML Query Requirements XML Query Use Cases XQuery 1.0: An XML Query Language XQuery 1.0 and XPath 2.0 Data Model XQuery 1.0 Formal Semantics XML Syntax for XQuery 1.0 (XQueryX)

Page 20: Query Languages  for XML: XQuery

20

XQuery

<bib>{for $b in //bib/bookwhere $b/publisher = "Addison-Wesley" and $b/@year > 1991return

<book year = {$b/@year}>{$b/title

}</book>}</bib>

Overview Path expressions: XPath FLWR (“flower”) expressions FOR vs. LET expressions Collections and sorting Other constructs

Page 21: Query Languages  for XML: XQuery

21

XPath

W3C Standard http://www.w3.org/TR/xpath

Building block for other W3C standards: XSL Transformations (XSLT) XML Link (XLink) XML Pointer (XPointer) XML Query

Was originally part of XSL

Page 22: Query Languages  for XML: XQuery

22

XPath Overview

bib matches a bib element

* matches any element

/ matches the root element

/bib matches a bib element under root

bib/paper matches a paper in bib

bib//paper matches a paper in bib, at any depth

//paper matches a paper at any depth

paper|book matches a paper or a book

@price matches a price attribute

bib/book/@price matches price attribute in book, in bib

bib/book/[@price<“55”]/author/lastname

Page 23: Query Languages  for XML: XQuery

23

FLWR (“Flower”) Expressions

“Flower” expressionsFOR ... LET... FOR... LET...WHERE...RETURN...

Example: find all books titles published after 1995FOR $x IN document("bib.xml")/bib/book

WHERE $x/year > 1995

RETURN $x/title

Result:<title>TCP/IP Illustrated</title><title>Advanced Programming the Unix

environment</title><title>Data on the Web</title><title>The Economics of Technology and Content

…</title>

Page 24: Query Languages  for XML: XQuery

24

FLWR (“Flower”) Expressions, Cont.

FOR $x in expr binds $x to each element in the list expr

LET $x = expr binds $x to the entire list expr Useful for common subexpressions and for aggregations

FOR/LET Clauses

WHERE Clause

RETURN Clause

List of tuples

List of tuples

Instance of XQuery data model

Page 25: Query Languages  for XML: XQuery

25

FOR vs. LET

FOR QueryFOR $x IN document("bib.xml")/bib/book

RETURN <result> $x </result> Returns

<result> <book>...</book></result><result> <book>...</book></result><result> <book>...</book></result>

LET QueryLET $x := document("bib.xml")/bib/book

RETURN <result> $x </result> Returns

<result> <book>...</book> <book>...</book> <book>...</book> ...</result>

Page 26: Query Languages  for XML: XQuery

26

More Complex FLWR Expressions

For each author of a book by Morgan Kaufmann, list all his/her books:

FOR $a IN distinct(document("bib.xml") /bib/book[publisher=“Morgan Kaufmann”]/author)

RETURN <result>

$a,

FOR $t IN /bib/book[author=$a]/title

RETURN $t

</result>

(distinct: eliminates duplicates) Find books whose price is larger than average:

LET $a=avg(document("bib.xml")/bib/book/@price)

FOR $b in document("bib.xml")/bib/book

WHERE $b/@price > $a

RETURN $b

Page 27: Query Languages  for XML: XQuery

27

Collections in XQuery

Ordered and unordered collections Ordered collection

/bib/book/author

Unordered collection distinct(/bib/book/author)

LET $a = /bib/book $a is a collection $b/author a collection (several authors...) $b/@price list of n prices $b/@price * 0.7 list of n numbers $b/@price * $b/@quantity list of n x m numbers

Page 28: Query Languages  for XML: XQuery

28

Sorting in XQuery

Sorting arguments Refer to the name space of the RETURN clause,

not the FOR clause

To sort on an element you don’t want to display Return it, then remove it with an additional query.

<publisher_list> FOR $p IN distinct(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) </publisher_list>

Page 29: Query Languages  for XML: XQuery

29

If-Then-Else

FOR $h IN //holding

RETURN <holding>

$h/title,

IF $h/@type = "Journal"

THEN $h/editor

ELSE $h/author

</holding> SORTBY (title)

Page 30: Query Languages  for XML: XQuery

30

Existential Quantifiers

FOR $b IN //book

WHERE SOME $p IN $b//para SATISFIES

contains($p, "sailing")

AND contains($p, "windsurfing")

RETURN $b/title

FOR $b IN //book

WHERE EVERY $p IN $b//para SATISFIES

contains($p, "sailing")

RETURN $b/title

Page 31: Query Languages  for XML: XQuery

31

Other Constructs

BEFORE and AFTER for dealing with order in the input

FILTER deletes some edges in the result tree

Recursive functions Currently: arbitrary recursion Perhaps more restrictions in the future?

Page 32: Query Languages  for XML: XQuery

32

XQueryX

LET $authors := /book/author RETURN <AUTHORS>{ $authors }</AUTHORS>

<q:query xmlns:q="http://www.w3.org/2001/06/xqueryx"> <q:flwr> <q:letAssignment variable="$authors"> <q:step axis="CHILD"> <q:identifier/>

<q:step axis="CHILD"> <q:identifier>book</q:identifier><q:identifier>author</q:identifier> </q:step> </q:step> </q:letAssignment> <q:return> <q:elementConstructor> <q:tagName><q:identifier>AUTHORS</q:identifier></q:tagName> <q:variable>$authors</q:variable> </q:elementConstructor> </q:return> </q:flwr> </q:query>

Page 33: Query Languages  for XML: XQuery

33

XQuery Software

QuiP http://www.softwareag.com/

developer/downloads/default.htm Software AG

Windows and Linux on x86 Features

Latest W3C syntax Graphical user interface.

Kweelt http://kweelt.sourceforge.net/ Open Source

Runs on all Java platforms Problems

Older syntax, from previous W3C requirements. No graphical user interface.

Page 34: Query Languages  for XML: XQuery

34

Example Application: Cruise Controller

Vehicle cruise controller. Modelled with a process graph of

32 processes. Mapped on 5 nodes:

CEM, ABS, ETM, ECM, TCM.

Page 35: Query Languages  for XML: XQuery

35

P1P1

P4P4

P2P2 P3P3

m1 m2

m3 m4

S1 S0

Round 1 Round 2 Round 3 Round 4 Round 5

P1 P4

P2

m1 m2m3 m4

P324 ms

Schedule Table

Page 36: Query Languages  for XML: XQuery

36

XML Model of the Cruise Controller

architecture.xml<NODE Name="CEM" Id="P1">

<Processor Name="AMD"><P_Type>I</P_Type></Processor>  <Memory unit="KB">128</Memory>

</NODE> behaviour.xml

<PROCESS Name="PR3" Id="PR3"><WCET unit="ms">7</WCET><Memory unit="KB">2</Memory>

</PROCESS><ARC Name="ARC3" Id="ARC3">

<Src>PR3</Src><Dest>PR4</Dest><Delay unit="ms">0</Delay></ARC>

mapping.xml<MAP Resource="P1">

<Process>PR1</Process><Process>PR2</Process><Process>PR30</Process></MAP>

schedule.xml<SLOT Id="PR1"><Start unit="ms">0</Start><Duration

unit="ms">0</Duration>   <Resource>P6</Resource></SLOT>

<SLOT Id="PR2">  <Start unit="ms">0</Start><Duration unit="ms">12</Duration><Resource>P1</Resource></SLOT>

Page 37: Query Languages  for XML: XQuery

37

Requirements on the Cruise Controller

Requirements on the model The model should be consistent

Every process should be mapped to one and only one node Every sensor/actuator should be connected

The schedule should be correct The schedule should respect the precedence constraints No two slots in the schedule should overlap

Cruise Controller Timing requirements

The CC should execute within 100 ms Resource requirements

The sum of processes’ memory on a nodeshould not exceed that node's capacity

Should be expressed in XQuery!

Page 38: Query Languages  for XML: XQuery

38

Resource Requirements: QueryThe sum of processes’ memory on a node should not exceed that node's

capacity

for $map in document("data/sweb/mapping.xml")//MAP, $nod in document("data/sweb/architecture.xml")//:NODE[@Id =

$map/@Resource] let $proc := document("data/sweb/behaviour.xml")//PROCESS[@Id =

$map/Process] return <processor Name={$nod/@Name} Id={$nod/@Id} HasMemory={$nod/Memory/text(),$nod/Memory/@unit}

MemoryUsedByScheduledProcesses={sum($proc/Memory),$nod/Memory/@unit}>

{ for $process in $proc return <process Name={$process/@Name} Id={$process/@Id} Memory={$process/Memory/text(),$process/Memory/@unit} /> sortby(int(substring-before(@Memory,"K"))) } </processor> sortby(int(substring-after(@Id,"P")))

Page 39: Query Languages  for XML: XQuery

39

Resource Requirements: Resultquery result:check_resource_consistency.xml

<?xml version="1.0" ?><quip:result xmlns:quip="http://.."> <processor Name="CEM" Id="P1" HasMemory="128KB"

MemoryUsedByScheduledProcesses="20KB"> <process Name="PR1" Id="PR1" Memory="1KB" /> <process Name="PR31" Id="PR31" Memory="4KB" /> <process Name="PR30" Id="PR30" Memory="5KB" /> <process Name="PR2" Id="PR2" Memory="10KB" /> </processor> <processor Name="ABS" Id="P2" HasMemory="256KB"

MemoryUsedByScheduledProcesses="25KB"> <process Name="PR32" Id="PR32" Memory="1KB" /> <process Name="PR27" Id="PR27" Memory="2KB" /> <process Name="PR3" Id="PR3" Memory="2KB" /> <process Name="PR4" Id="PR4" Memory="5KB" /> <process Name="PR29" Id="PR29" Memory="7KB" /> <process Name="PR28" Id="PR28" Memory="8KB" /> </processor> <processor Name="ETM" Id="P3" HasMemory="128KB“

MemoryUsedByScheduledProcesses="40KB"> <process Name="PR14" Id="PR14" Memory="2KB" /> <process Name="PR11" Id="PR11" Memory="2KB" /> … </processor> …</quip:result> 

Page 40: Query Languages  for XML: XQuery

40

Use Case “XMP”: Experiences and Exemplars

<bib> <book year="1994">  <title>TCP/IP Illustrated</title> <author><last>Stevens</last>  

<first>W.</first></author>  <publisher>Addison-Wesley</publisher> <price>65.95</price> </book> <book year="1992"> <title>Advanced Prog… the Unix environment</title> <author><last>Stevens</last><first>W.</first>  

</author>  <publisher>Addison-Wesley</publisher> <price>65.95</price> </book> <book year="2000"> <title>Data on the Web</title>

<author><last>Abiteboul</last><first>Serge</first></author>

<author><last>Buneman</last><first>Peter</first></author>

<author><last>Suciu</last><first>Dan</first></author>

<publisher>Morgan Kaufmann Publishers</publisher>

<price>39.95</price> </book>- <book year="1999"> <title>The Economics of Technology and Content

for Digital TV</title> <editor><last>Gerbarg</last><first>Darcy</first> <affiliation>CITI</affiliation> </editor> <publisher>Kluwer Academic Publishers</publisher> <price>129.95</price> </book></bib>

<bib>{ for $b in

document("data/xmp-data.xml")/bib/book

where $b/publisher = "Addison-Wesley" and int($b/@year) > 1991

return <book year = {$b/@year}>{ $b/title }</book> }</bib>

xmp-data.xml XMPQ1.xquery

Page 41: Query Languages  for XML: XQuery

41

Use Case “XMP”: Experiences and Exemplars

<?xml version="1.0" ?> <quip:result xmlns:quip="http://namespaces.softwareag.com/tamino/quip/"> <bib> <book year="1994">  <title>TCP/IP Illustrated</title> </book> <book year="1992"> <title>Advanced Programming in the Unix environment</title>   </book>  </bib>  </quip:result>

Result of: List books published by Addison-Wesley after 1991, including their year and title.

Page 42: Query Languages  for XML: XQuery

42

Use Case “TREE”: Qs that preserve hierarchy

<?xml version="1.0" ?> <!DOCTYPE book SYSTEM “book.dtd”>

<book> <title>Data on the Web</title>   <author>Serge Abiteboul</author>   <author>Peter Buneman</author>   <author>Dan Suciu</author> <section id="intro" difficulty="easy">  <title>Introduction</title>   <p>Text ...</p> <section>  <title>Audience</title> <p>Text ...</p> </section> <section> <title>Web Data and the Two Cultures</title> <p>Text ...</p> <figure height="400" width="400">  <title>Traditional client/server architecture</title>   <image source="csarch.gif" />   </figure>  <p>Text ...</p>   </section>  </section> <section id="syntax" difficulty="medium">  …. </section>  …. </book>

<figlist>{ for $f in document("data/tree-data.xml")//figure return <figure>{ ($f/@* ,$f/title ) }</figure> }</figlist>

tree-data.xml TREEQ2.xquery

Page 43: Query Languages  for XML: XQuery

43

Use Case “TREE”: Qs that preserve hierarchy

  <?xml version="1.0" ?> <quip:result xmlns:quip="http://namespaces.softwareag.com/tamino/quip/"> <figlist> <figure height="400" width="400">  <title>Traditional client/server architecture</title> </figure> <figure height="200" width="500"> <title>Graph representations of structures</title> </figure> <figure height="250" width="400"> <title>Examples of Relations</title> </figure> </figlist>  </quip:result>

Result of: Prepare a (flat) figure list for first book, listing all the figures and their titles. Preserve the original attributes of each <figure> element, if any.

Page 44: Query Languages  for XML: XQuery

44

Use Case “TREE”: Qs that preserve hierarchy

(<section_count>{ count(document("data/tree-data.xml")//section) }</section_count>,<figure_count>{ count(document("data/tree-data.xml")//figure) }</figure_count>)

  <?xml version="1.0" ?> - <quip:result

xmlns:quip="http://namespaces. softwareag.com/tamino/quip/">  <section_count>7</section_count>   <figure_count>3</figure_count>   </quip:result>

TREEQ3.xquery/Result TREEQ4.xquery/Result

<top_section_count>{

count(document("data/tree-data.xml")/book/section)

}</top_section_count>

<?xml version="1.0" ?> <quip:result

xmlns:quip="http://namespaces. softwareag.com/tamino/quip/">

<top_section_count>2</top_section_count>  </quip:result>

Page 45: Query Languages  for XML: XQuery

45

Use Case “SEQ”: Queries based on sequence

report> <section> <section.title>Procedure</section.title> <section.content>  The patient was taken to the

operating room where she was placed in supine position and  

<anesthesia>induced under general anesthesia.</anesthesia>

<prep> <action>A Foley catheter was placed to

decompress the bladder </action>   and the abdomen was then prepped

and draped in sterile fashion.   </prep> <incision>  A curvilinear incision was made   <geography>in the midline immediately infraumbilical </geography> and the subcutaneous tissue was divided   <instrument>using

electrocautery.</instrument>   </incision>  The fascia was identified and   <action>#2 0 Maxon stay sutures were placed on

each side of the midline. </action> <incision>  The fascia was divided using   <instrument>electrocautery</instrument>   and the peritoneum was entered.   </incision> …   </section.content>  </section>  </report>

for $s in document("data/report1.xml")//section[section.title = "Procedure"]

let $instruments := $s//instrumentfor $i in 1 to 2return $instruments[$i]

Result of: In the Procedure section of Report1, what are the first two Instruments to be used?

<?xml version="1.0" ?><quip:result

xmlns:quip="http://namespaces. softwareag.com/tamino/quip/"> <instrument>using electrocautery. </instrument>

<instrument>electrocautery</instrument>

</quip:result>

report1.xml SEQQ2.xquery

Page 46: Query Languages  for XML: XQuery

46

Use Case “R”: Access to Relational Data

USERS USERID NAME RATINGU01 Tom Jones BU02 Mary Doe AU03 Dee Linquent DU04 Roger Smith CU05 Jack Sprat BU06 Rip Van Winkle B

ITEMS ITEMNO DESCR O_BY DATE PRICE1001 Red Bicycle U01 99-01-05 99-01-20 401002 Motorcycle U02 99-02-11 99-03-15 5001003 Old Bicycle U02 99-01-10 99-02-20 251004 Tricycle U01 99-02-25 99-03-08 151005 Tennis Racket U03 99-03-19 99-04-30 201006 Helicopter U03 99-05-05 99-05-25 500001007 Racing Bicycle U04 99-01-20 99-02-20 2001008 Broken Bicycle U01 99-02-05 99-03-06 25

BIDS USERID ITEMNO BID BID_DATEU02 1001 35 99-01-07U04 1001 40 99-01-08U02 1001 45 99-01-11U04 1001 50 99-01-13U02 1001 55 99-01-15U01 1002 400 99-02-14….

<result> { for $u in document("users.xml")//user_tuple for $i in document("items.xml")//item_tuple where $u/rating > "C" and $i/reserve_price >

1000 and $i/offered_by = $u/userid return <warning> { $u/name } { $u/rating } {

$i/description } { $i/reserve_price } </warning> } </result>

relational data RQ2.xquery

Page 47: Query Languages  for XML: XQuery

47

  <?xml version="1.0" ?> -

<quip:result …> <result>- <warning>  <name>Dee Linquent</name> <rating>D</rating>   <description>Helicopter</description>   <reserve_price>50000</reserve_price>   </warning> </result>  </quip:result>

Result of: Find cases where a user with a rating worse (alphabetically, greater) than "C" is offering an item with a reserve price of more than 1000.  

Use Case “R”: Access to Relational Data

Page 48: Query Languages  for XML: XQuery

48

  <result>{ for $u in document("data/R-users.xml")//user_tuple let $b := document("data/R-bids.xml")//bid_tuple[userid = $u/userid and int(string-

value(bid)) >= 100] where count($b) > 1 return <big_spender>{ $u/name/text() }</big_spender>}</result>

Result:

  <?xml version="1.0" ?> <quip:result xmlns:quip="http://namespaces.softwareag.com/tamino/quip/"> <result> <big_spender>Mary Doe</big_spender> <big_spender>Dee Linquent</big_spender> <big_spender>Roger Smith</big_spender> </result>  </quip:result>

Result of: List names of users who have placed multiple bids of at least $100 each.

 

Use Case “R”: Access to Relational Data

Page 49: Query Languages  for XML: XQuery

49

Use Case “PARTS”: Recursive Parts Explosion

  <?xml version="1.0" encoding="ISO-8859-1" ?>

partlist> <part partid="0" name="car" /> <part partid="1" partof="0"

name="engine" /> <part partid="2" partof="0"

name="door" />   <part partid="3" partof="1"

name="piston" /> <part partid="4" partof="2"

name="window" />  <part partid="5" partof="2" name="lock"

/>  <part partid="10"

name="skateboard" />  <part partid="11" partof="10"

name="board" />  <part partid="12" partof="10"

name="wheel" />  <part partid="20" name="canoe" /></partlist>

define function one_level(xs:AnyType $p, xs:AnyType $ps) returns xs:AnyType {

<part>{ ($p/@partid ,$p/@name ,for $s in $ps[$p/@partid = @partof] return one_level($s,$ps) ) }</part>}

let $ps := document("data/parts-data.xml")/partlist/part

for $p in $ps[not(@partof)] return one_level($p,$ps)

parts-data.xml PARTSQ1a.xquery

Page 50: Query Languages  for XML: XQuery

50

<?xml version="1.0" ?> - <quip:result xmlns:quip="http://namespaces.softwareag.com/tamino/quip/"> <part partid="0" name="car"> <part partid="1" name="engine">  <part partid="3" name="piston" /> </part> <part partid="2" name="door"> <part partid="4" name="window" /> <part partid="5" name="lock" /> </part> </part> <part partid="10" name="skateboard"> <part partid="11" name="board" /> <part partid="12" name="wheel" /> </part> <part partid="20" name="canoe" /> </quip:result>

Result of: Convert the sample document from "partlist" format to "parttree" format (see DTD section for definitions). In the result document, part containment is represented by containment of one <part> element inside another. Each part that is not part of any other part should appear as a separate top-level element in the output document.

 

Use Case “PARTS”: Recursive Parts Explosion

Page 51: Query Languages  for XML: XQuery

51

XSLT & XQuery: is there a difference?

<xsl:transform xmlns:xsl="http://www.w3.org/1999/

- XSL/Transform" version="1.0">  <xsl:output method="xml"

indent="yes" /> <xsl:template match="/"> <xsl:element name="bib"> <xsl:for-each select="document('xmp- data.xml')/bib/book"> <xsl:if test="publisher='Addison-

Wesley‘ and @year>'1991'"> <xsl:element name="book"> <xsl:attribute name="year"> <xsl:value-of select="@year"/> </xsl:attribute> <xsl:copy-of select="title" /> </xsl:element> </xsl:if> </xsl:for-each> </xsl:element>  </xsl:template></xsl:transform>

<bib>{ for $b in

document("data/xmp-data.xml")/bib/book

where $b/publisher = "Addison-Wesley" and int($b/@year) > 1991

return <book year = {$b/@year}>{ $b/title }</book> }</bib>

xslt.xls XMPQ1.xquery

Page 52: Query Languages  for XML: XQuery

52

XSLT & XQUERYXQuery Result:

<?xml version="1.0" ?> <quip:result xmlns:quip="http://namespaces.softwareag.com/tamino/quip/"> <bib> <book year="1994">  <title>TCP/IP Illustrated</title> </book> <book year="1992"> <title>Advanced Programming in the Unix environment</title>   </book>  </bib>  </quip:result>

XSLT Xalan engine Result:

  <?xml version="1.0" encoding="UTF-8" ?> <bib> <book year="1994"> <title>TCP/IP Illustrated</title> </book> <book year="1992"> <title>Advanced Programming in the Unix environment</title>   </book>  </bib>

Page 53: Query Languages  for XML: XQuery

53

Summary

Motivation XML applications, types of queries

Approaches Requirements on a query language Path expressions, the basic building block XML query languages

XQuery Background, history Concepts, examples

FLWR expressions FOR and LET expressions Collections and sorting

Available software, demo Examples

XQuery vs. XSLT Summary