1 / 53

Query Languages for XML: XQuery

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

gary
Download Presentation

Query Languages for XML: XQuery

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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

  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

  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

  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

  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

  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

  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

  9. Path Expressions Bib &o1 paper paper book references &o12 &o24 &o29 references references author page author year author title http title title publisher author author author &o43 &25 &o44 &o45 &o46 &o52 &96 1997 &o51 &o50 &o49 &o47 &o48 last firstname firstname lastname first lastname &o70 &o71 &243 &206 “Serge” “Abiteboul” “Victor” 122 133 “Vianu” • Data, modeled as an edge-labeleddirected graph Bib.paper={&o12,&o29} Bib.book.publisher={&o51} Bib.paper.author.lastname={&o71,&206}

  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

  11. XML Query Languages • Semistructured databases • XML-QLA. 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 • XQLJ. Robie. The design of XQL, 1999, http://www.texcel.no/whitepapers/xql-design.html

  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

  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>

  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.

  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)

  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

  17. Lorel select xml(bib:{ (select xml(book:{@year:y, title:t}) from bib.book b, b.title t, b.year y where 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.

  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

  19. XQuery: An XML Query Language • W3C standard • http://www.w3.org/TR/xquery • Derived from QuiltJonathan 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)

  20. XQuery <bib>{ for $b in //bib/book where $b/publisher = "Addison-Wesley" and $b/@year > 1991 return <book year = {$b/@year}>{ $b/title }</book> }</bib> • Overview • Path expressions: XPath • FLWR (“flower”) expressions • FOR vs. LET expressions • Collections and sorting • Other constructs

  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

  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

  23. FLWR (“Flower”) Expressions • “Flower” expressions FOR ... LET... FOR... LET... WHERE... RETURN... • Example: find all books titles published after 1995 FOR $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>

  24. FLWR (“Flower”) Expressions, Cont. FOR/LET Clauses List of tuples WHERE Clause List of tuples RETURN Clause Instance of XQuery data model • 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

  25. FOR vs. LET • FOR Query FOR $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 Query LET $x:=document("bib.xml")/bib/book RETURN <result> $x </result> • Returns <result> <book>...</book> <book>...</book> <book>...</book> ... </result>

  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

  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/@pricelist of n prices • $b/@price * 0.7 list of n numbers • $b/@price * $b/@quantity list of n x m numbers

  28. Sorting in XQuery <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> • 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.

  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)

  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

  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?

  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>

  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.

  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.

  35. Schedule Table P1 P4 24 ms P3 P2 m3 m4 P1 S1 S0 m1 m2 Round 1 Round 2 Round 3 Round 4 Round 5 m1 m2 P2 P3 m3 m4 P4

  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>

  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!

  38. Resource Requirements: Query The 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")))

  39. Resource Requirements: Result query result:check_resource_consistency.xml <?xml version="1.0" ?> <quip:result xmlns:quip="http://.."> <processor Name="CEM" Id="P1" HasMemory="128KB" MemoryUsedByScheduledProcesses="20KB"> <processName="PR1" Id="PR1" Memory="1KB" /> <processName="PR31" Id="PR31" Memory="4KB" /> <processName="PR30" Id="PR30" Memory="5KB" /> <processName="PR2" Id="PR2" Memory="10KB" /> </processor> <processor Name="ABS" Id="P2" HasMemory="256KB" MemoryUsedByScheduledProcesses="25KB"> <processName="PR32" Id="PR32" Memory="1KB" /> <processName="PR27" Id="PR27" Memory="2KB" /> <processName="PR3" Id="PR3" Memory="2KB" /> <processName="PR4" Id="PR4" Memory="5KB" /> <processName="PR29" Id="PR29" Memory="7KB" /> <processName="PR28" Id="PR28" Memory="8KB" /> </processor> <processor Name="ETM" Id="P3" HasMemory="128KB“ MemoryUsedByScheduledProcesses="40KB"> <processName="PR14" Id="PR14" Memory="2KB" /> <processName="PR11" Id="PR11" Memory="2KB" /> … </processor> … </quip:result>

  40. Use Case “XMP”: Experiences and Exemplars xmp-data.xml XMPQ1.xquery <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>

  41. Use Case “XMP”: Experiences and Exemplars Result of: List books published by Addison-Wesley after 1991, including their year and title. <?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>

  42. Use Case “TREE”: Qs that preserve hierarchy tree-data.xml TREEQ2.xquery <?xml version="1.0" ?> <!DOCTYPE bookSYSTEM “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> <imagesource="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>

  43. Use Case “TREE”: Qs that preserve hierarchy 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. <?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>

  44. Use Case “TREE”: Qs that preserve hierarchy TREEQ3.xquery/Result TREEQ4.xquery/Result (<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> <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>

  45. Use Case “SEQ”: Queries based on sequence report1.xml SEQQ2.xquery 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//instrument for $i in 1 to 2 return $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>

  46. Use Case “R”: Access to Relational Data relational data RQ2.xquery USERS USERID NAME RATING U01 Tom Jones B U02 Mary Doe A U03 Dee Linquent D U04 Roger Smith C U05 Jack Sprat B U06 Rip Van Winkle B ITEMS ITEMNO DESCR O_BY DATE PRICE 1001 Red Bicycle U01 99-01-05 99-01-20 40 1002 Motorcycle U02 99-02-11 99-03-15 500 1003 Old Bicycle U02 99-01-10 99-02-20 25 1004 Tricycle U01 99-02-25 99-03-08 15 1005 Tennis Racket U03 99-03-19 99-04-30 20 1006 Helicopter U03 99-05-05 99-05-25 50000 1007 Racing Bicycle U04 99-01-20 99-02-20 200 1008 Broken Bicycle U01 99-02-05 99-03-06 25 BIDS USERID ITEMNO BID BID_DATE U02 1001 35 99-01-07 U04 1001 40 99-01-08 U02 1001 45 99-01-11 U04 1001 50 99-01-13 U02 1001 55 99-01-15 U01 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>

  47. Use Case “R”: Access to Relational Data 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. <?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>

  48. Use Case “R”: Access to Relational Data Result of: List names of users who have placed multiple bids of at least $100 each. <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>

  49. Use Case “PARTS”: Recursive Parts Explosion parts-data.xml PARTSQ1a.xquery <?xml version="1.0" encoding="ISO-8859-1" ?> partlist> <partpartid="0" name="car" /> <partpartid="1" partof="0" name="engine" /> <partpartid="2" partof="0" name="door" /> <partpartid="3" partof="1" name="piston" /> <partpartid="4" partof="2" name="window" /> <partpartid="5" partof="2" name="lock" /> <partpartid="10" name="skateboard" /> <partpartid="11" partof="10" name="board" /> <partpartid="12" partof="10" name="wheel" /> <partpartid="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)

  50. Use Case “PARTS”: Recursive Parts Explosion 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. <?xml version="1.0" ?> - <quip:result xmlns:quip="http://namespaces.softwareag.com/tamino/quip/"> <part partid="0" name="car"> <part partid="1" name="engine"> <partpartid="3" name="piston" /> </part> <part partid="2" name="door"> <partpartid="4" name="window" /> <partpartid="5" name="lock" /> </part> </part> <part partid="10" name="skateboard"> <partpartid="11" name="board" /> <partpartid="12" name="wheel" /> </part> <partpartid="20" name="canoe" /> </quip:result>

More Related