Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: XQuery.

Slides:



Advertisements
Similar presentations
1 A B C
Advertisements

Simplifications of Context-Free Grammars
Chungnam National University DataBase System Lab
1 Quick Intro to XPath Roger L. Costello 14 December, 2012.
AP STUDY SESSION 2.
1
1 Roger L. Costello 16 June 2010 XQuery
1 XPath Roger.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 Created by Cheryl M. Hughes The Web Wizards Guide to XML by Cheryl M. Hughes.
Copyright © 2013 Elsevier Inc. All rights reserved.
David Burdett May 11, 2004 Package Binding for WS CDL.
NTDB ® Annual Report 2009 © American College of Surgeons All Rights Reserved Worldwide Percent of Hospitals Submitting Data to NTDB by State and.
9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
Create an Application Title 1Y - Youth Chapter 5.
CALENDAR.
Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: XPath, NameSpaces.
Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: C2.
Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: C3.
Programming Language Concepts
The 5S numbers game..
Media-Monitoring Final Report April - May 2010 News.
Welcome. © 2008 ADP, Inc. 2 Overview A Look at the Web Site Question and Answer Session Agenda.
Break Time Remaining 10:00.
Factoring Quadratics — ax² + bx + c Topic
EE, NCKU Tien-Hao Chang (Darby Chang)
Anything But Typical Learning to Love JavaScript Prototypes Page 1 © 2010 Razorfish. All rights reserved. Dan Nichols March 14, 2010.
Database Performance Tuning and Query Optimization
PP Test Review Sections 6-1 to 6-6
Briana B. Morrison Adapted from William Collins
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Lexical Analysis Arial Font Family.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Copyright © [2002]. Roger L. Costello. All Rights Reserved. 1 XML Schemas Reference Manual Roger L. Costello XML Technologies Course.
Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23, 03-46,
Adding Up In Chunks.
FAFSA on the Web Preview Presentation December 2013.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
State of Connecticut Core-CT Project Query 8 hrs Updated 4/14/2003.
1 Termination and shape-shifting heaps Byron Cook Microsoft Research, Cambridge Joint work with Josh Berdine, Dino Distefano, and.
Artificial Intelligence
Slide R - 1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Prentice Hall Active Learning Lecture Slides For use with Classroom Response.
Dr. Alexandra I. Cristea XPath and Namespaces.
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Types of selection structures
One-Degree Imager (ODI), WIYN Observatory What’s REALLY New in SolidWorks 2010 Richard Doyle, User Community Manager inspiration.
Converting a Fraction to %
Numerical Analysis 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CSE20 Lecture 15 Karnaugh Maps Professor CK Cheng CSE Dept. UC San Diego 1.
Clock will move after 1 minute
famous photographer Ara Guler famous photographer ARA GULER.
Chapter 11 Creating Framed Layouts Principles of Web Design, 4 th Edition.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
Web Data Management XQuery 1. In this lecture Summary of XQuery FLWOR expressions – For, Let, Where, Order by, Return FOR and LET expressions Collections.
9. Two Functions of Two Random Variables
1 Dr. Scott Schaefer Least Squares Curves, Rational Representations, Splines and Continuity.
Advanced Users Training 1 ENTERPRISE REPORTING FINANCIAL REPORTS.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
XQUERY. What is XQuery? XQuery is the language for querying XML data The best way to explain XQuery is to say that XQuery is to XML what SQL is to database.
1 XQuery Web and Database Management System. 2 XQuery XQuery is to XML what SQL is to database tables XQuery is designed to query XML data What is XQuery?
Presentation transcript:

Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: XQuery

2 Previously we looked at: –XPath –Namespaces Next: –XQuery

3 Xquery

4 What is XQuery? XQuery is the language for querying XML data XQuery for XML is like SQL for databases XQuery is built on XPath expressions XQuery is defined by the W3C XQuery is supported by all the major database engines (IBM, Oracle, Microsoft, etc.) XQuery is a W3C recommendation (Jan 2007; latest 14 Dec 2010) thus a standard

5 XQuery - Examples of Use Extract information to use in a Web Service Generate summary reports Transform XML data to XHTML Search Web documents for relevant information

6 XQuery compared to XPath XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators. XQuery 1.0 is a strict superset of XPath 2.0 XPath 2.0 expression is directly an XQuery 1.0 expression (a query) The extra expressive power is the ability to: –Join information from different sources and –Generate new XML fragments

7 Xquery compilers Download: Or try out at*: Syntax check at:

8 XQuery query makeup Prolog –Like XPath, XQuery expressions are evaluated relatively to a context –explicitly provided by a prolog (header) ~ header with definitions Body –The actual query Generate Join Select

9 XQuery Ex.: Prolog + Query

10 XQuery Prolog (i.e., header(s)) Settings define various parameters for the XQuery processor language, such as: xquery version "1.0"; declare base-uri " declare default element namespace " declare namespace xs= " import module " at "logo.xq"; declare variable $x as xs:integer := 7; declare function addLogo($root as node()) as node()*{ }; (: etc :)

Module definition xquery version 1.0; module namespace mylib = declare variable $mylib:foo as xs:string := foo; declare function mylib:foobar() as xs:string { concat ($mylib:foo, bar) }; 11

12 Body: Constructors Direct constructors in Xquery: my fragment –Evaluates to the given XML fragment

13 Explicit constructors computed constructors

14 Variable bindings (implicit constructors) {$name} {$job} {$deptno} {$SGMLspecialist }

15 How to Select Nodes with XQuery? Functions –XQuery uses functions to extract data from XML documents. (X)Path Expressions –XQuery uses path expressions to navigate through elements in an XML document. Predicates –XQuery uses predicates to limit the extracted data from XML documents.

16 Functions doc() –function to open a file Example: –doc("books.xml") Note: A call to a function can appear where an expression may appear.

17 Path Expressions Example: select all the title elements in the "books.xml" file: doc("books.xml")/bookstore/book/title

18 Predicates Example: select all the book elements under the bookstore element that have a price element with a value that is less than 30 : doc("books.xml")/bookstore/book[price<30]

19 At a glance: function, path, predicate

20 FLWOR For, Let, Where, Order by, Return = main engine ~ SQL syntax (SFW(GH)O) ~ programs and function calls

21 FLWOR by comparison with Path expressions select all the title elements under the book elements that are under the bookstore element that have a price element with a value that is higher than 30. Path expression : doc("books.xml")/bookstore/book[price>30]/title FLWOR expression : for $x in doc("books.xml")/bookstore/book where $x/price>30 return $x/title

22 Sorting in FLWOR for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title

23 Present the Result In an HTML List { for $x in doc("books.xml")/bookstore/book/title order by $x return {$x} }

24 Result HTML List Everyday Italian Harry Potter Learning XML XQuery Kick Start

25 Eliminate element (here: title) { for $x in doc("books.xml")/bookstore/book/title order by $x return {data($x)} (: also text{} :) }

26 New result HTML List Everyday Italian Harry Potter Learning XML XQuery Kick Start

27 Another FLWOR Expression { for $s in doc("students.xml")//student let $m := $s/major where count($m) ge 2 order by return { $s/name/text()} }

28 The Difference between for and let

29 The Difference between for and let := in

30 The Difference between for and let

31 The Difference between for and let

32 FLWOR Basic Building Blocks

33 General rules for and let may be used many times in any order only one where is allowed many different sorting criteria can be specified (descending, ascending, etc.)

34 Reversing order Reverses the order of a sequence, for nodes or atomic values reverse (( 1, 2, 3)) -> 321

35 Joining documents for $p in doc(" for $n in doc("neighbors.xml")//neighbor[ssn = $p/ssn] return { $p/ssn } { $n/name } { $p/income }

36 Two-way join in a where Clause for $item in doc(ord.xml)//item, $product in doc(cat.xml)//product where = $product/number return <item name={$product/name} />

37 Aggregating Make summary calculations on grouped data Functions: –sum, avg, max, min, count

38 Conditionals for $b in doc(bib.xml)/book return {$b/title} {if ( count($b/author) and others ) }

39 Nesting Conditional Expressions Conditional expressions can be nested else if functionality is provided if ( count($b/author) = 1 ) then $b/author else if (count($b/author) = 2 )then (:.. :) else ( $b/author[1], and others )

40 Logical Expressions and, or operators: –and has precedence over or –Parentheses can change precedence if ($isDiscounted and ($discount > 5 or $discount < 0 ) ) then 5 else $discount not function for negations: if (not($isDiscounted)) then 0 else $discount

41 XQuery Built-in Functions XQuery function namespace URI is: default prefix: fn:. E.g.: fn:string() or fn:concat(). fn: is the default prefix of the namespace, the function names does not need to be prefixed when called.

42 Built-in Functions String-related –substring, contains, matches, concat, normalize- space, tokenize Date-related –current-date, month-from-date, adjust-time-to- timezone Number-related –round, avg, sum, ceiling Sequence-related –index-of, insert-before, reverse, subsequence, distinct-values

43 Built-in Functions (2) Node-related –data, empty, exists, id, idref Name-related –local-name, in-scope-prefixes, QName, resolve- QName Error handling and trapping –error, trace, exactly-one Document and URI-related –collection, doc, root, base-uri

44 Function calls doc("books.xml")//book[substring(title,1,5)='Harry'] let $name := (substring($booktitle,1,4)) {upper-case($booktitle)}

45 for $x in doc(" stea/courses/CS253/2009/books.xml")// book/title for $y in data($x) for $name in (substring($y,1,4)) return $name

46 User Defined Functions declare function prefix:function_name($parameter AS datatype) AS returnDatatype { (:...function code here... :) };

47 User-defined Functions declare function depth($e AS xsd:integer) AS xsd:integer { if (empty($e/*) then 1 else max(for $c in $e/* return depth($c)) ) +1 }; (: usage :) for $b in doc(bib.xml)/book return depth($b)

48 Existential and Universal Quantifiers for $b in doc(bib.xml)/book where some $author in $b/author satisfies $author/text() = Ullman return $b for $b in doc(bib.xml)/book where every $author in $b/author satisfies $author/text() = Ullman return $b Return books where all authors are Ullman Return books where at least one author is Ullman

49 Comments

50 Comparisons Value comparisons Eq, ne, lt, le, gt, ge Used to compare individual values Each operand must be a single atomic value (or a node containing a single atomic value) General comparisons =, !=,, >= Can be used with sequences of multiple items

51 Example

52 XQuery Syntax Declarative, functional language ~ SQL Nested expressions Case sensitive White spaces: –Tabs, space, CR, LF –Ignored between language constructs –Significant in quoted strings No special EOL character

53 Keywords and names Keywords and operators –Case-sensitive, generally lower case –May have several meanings depending on the context E.g. * or in –No reserved words All names must be valid XML names –variables, functions, elements, attributes –Can be associated with a namespace

54 XQuery gives you a choice: Path Expressions: –If you just want to copy certain elements and attributes as is FLWOR Expressions: –Allow sorting –Allow adding elements/attributes –Verbose, but can be clearer

55 XQuery tools XStylus Studio oad.html (free trial version) oad.html –See also short XQuery intro at:

56 Other info: –XQuery on Distributed Resources –Extensions for generic programming with XML

57 XQuery on Distributed Sources

58

59

60

61

62

63 XML and programming XSLT, XPath and XQuery provide tools for specialized tasks. But many applications are not covered: –domain-specific tools for concrete XML languages –general tools that nobody has thought of yet

64 XML in general-purpose programming languages parse XML documents into XML trees navigate through XML trees construct XML trees output XML trees as XML documents DOM and SAX are corresponding APIs that are language independent and supported by numerous languages. JDOM is an API that is tailored to Java.DOMSAXJDOM

65 XQuery Conclusion We have learned: –XQuery definition –Usage scenarios –Comparison w. XSLT and XPath –Capabilities –Functions, path expressions and predicates –FLWOR