1 Querying XML Documents. 2 Objectives How XML generalizes relational databases The XQuery language How XML may be supported in databases.

Slides:



Advertisements
Similar presentations
Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: C2.
Advertisements

XML Data Management 8. XQuery Werner Nutt. Requirements for an XML Query Language David Maier, W3C XML Query Requirements: Closedness: output must be.
XML Examples. Bank Information Basic structure: A-101 Downtown 500 … Johnson Alma Surrey … A-101 Johnson …
XML: Extensible Markup Language
XQuery John Annechino Steven Pow. Agenda What is XQuery? Uses of XQuery XQuery vs. XSLT Syntax –Built-In Functions –FLWOR –if-then-else –User-Defined.
XQuery Or, what about REAL databases?. XQuery - its place in the XML team XLink XSLT XQuery XPath XPointer.
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?
Friday, September 4 th, 2009 The Systems Group at ETH Zurich XML and Databases Exercise Session 6 courtesy of Ghislain Fourny/ETH © Department of Computer.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 357 Database Systems I Query Languages for XML.
1 COS 425: Database and Information Management Systems XML and information exchange.
XML and The Relational Data Model
September 15, 2003Houssam Haitof1 XSL Transformation Houssam Haitof.
1 Advanced Topics XML and Databases. 2 XML u Overview u Structure of XML Data –XML Document Type Definition DTD –Namespaces –XML Schema u Query and Transformation.
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
XMLII XSchema XSchema XQuery XQuery. XML Schema XML Schema is a more sophisticated schema language which addresses the drawbacks of DTDs. Supports XML.
4/20/2017.
Lecture 7 of Advanced Databases XML Querying & Transformation Instructor: Mr.Ahmed Al Astal.
CSE3201/CSE4500 XPath. 2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.
Lecture 6 of Advanced Databases XML Schema, Querying & Transformation Instructor: Mr.Ahmed Al Astal.
COP4540 Database Management System Final Review Reviewed by Ramakrishna. Parts of this are taken from Fernando Farfan’s presentation.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 1.
XML TECHNOLOGY FAMILY XML XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to describe data[carry data],
CSE3201/CSE4500 Information Retrieval Systems
Lecture 6 of Advanced Databases XML Querying & Transformation Instructor: Mr.Eyad Almassri.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation An Introduction to XQuery.
XP New Perspectives on XML Tutorial 6 1 TUTORIAL 6 XSLT Tutorial – Carey ISBN
WORKING WITH XSLT AND XPATH
1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation An Introduction to XQuery.
Sofia, Bulgaria | 9-10 October Using XQuery to Query and Manipulate XML Data Stephen Forte CTO, Corzen Inc Microsoft Regional Director NY/NJ (USA) Stephen.
XML Model and Processing Transparency No. 1 Query XML Documents with XQuery Cheng-Chia Chen.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
 XML is designed to describe data and to focus on what data is. HTML is designed to display data and to focus on how data looks.  XML is created to structure,
Lecture 22 XML querying. 2 Example 31.5 – XQuery FLWOR Expressions ‘=’ operator is a general comparison operator. XQuery also defines value comparison.
Introduction to XQuery Bun Yue Professor, CS/CIS UHCL.
XQL, OQL and SQL Xia Tang Sixin Qian Shijun Shen Feb 18, 2000.
XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Nikos dimitrakas – IS4/2i1242/2i4042 spring XML Query Languages Database Systems (4th edition) Chapter 30.5, , Articles & Excerpts.
Unit 04 : XQuery COMP 5323 Web Database Technologies and Applications 2014.
August Chapter 6 - XPath & XPointer Learning XML by Erik T. Ray Slides were developed by Jack Davis College of Information Science and Technology.
Database Systems Part VII: XML Querying Software School of Hunan University
5/2/20051 XML Data Management Yaw-Huei Chen Department of Computer Science and Information Engineering National Chiayi University.
Namespace information are represented as namespace node which maps in scope on an element Attach to every element node where namespace is declared root.
 2002 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 – XSL: Extensible Stylesheet Language Transformations (XSLT) Outline 12.1Introduction 12.2Setup.
[ Part III of The XML seminar ] Presenter: Xiaogeng Zhao A Introduction of XQL.
XML query. introduction An XML document can represent almost anything, and users of an XML query language expect it to perform useful queries on whatever.
XMLTA Transparency No. 1 Query XML Documents with XQuery Cheng-Chia Chen.
Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa ME (Modular) CSE 2011 Batch.
XML Query: xQuery Reference: Xquery By Priscilla Walmsley, Published by O’Reilly.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
What is XLink? XLink is short for XML Linking Language XLink is used to create hyperlinks in XML documents Any element in an XML document can behave as.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
XP 1 XSLT II Robin Burke ECT 360. XP 2 Homework #4 Forgot we hadn't covered "choose" becomes extra credit My books.xml didn't include descriptions.
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
1 The XPath Language. 2 XPath Expressions Flexible notation for navigating around trees A basic technology that is widely used uniqueness and scope in.
XML Introduction to XML Extensible Markup Language.
XML Schema – XSLT Week 8 Web site:
XQUERY The contents of this slide are obtained from various sources including, Wikipedia, W3School, Stanford website etc. January 2011 Dr.Kwanchai Eurviriyanukul.
Rendering XML Documents ©NIITeXtensible Markup Language/Lesson 5/Slide 1 of 46 Objectives In this session, you will learn to: * Define rendering * Identify.
XML. What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data.
XML Query languages--XPath. Objectives Understand XPath, and be able to use XPath expressions to find fragments of an XML document Understand tree patterns,
XML: Extensible Markup Language
Querying and Transforming XML Data
Session IV Chapter 16 – XQuery
{ XML Technologies } BY: DR. M’HAMED MATAOUI
More XML XML schema, XPATH, XSLT
Session II Chapter 16 – XQuery
Presentation transcript:

1 Querying XML Documents

2 Objectives How XML generalizes relational databases The XQuery language How XML may be supported in databases

3 Only Some Trees are Relations They have height two The root has an unbounded number of children All nodes in the second layer (records) have a fixed number of child nodes (fields) Trees are ordered, while both rows and columns of tables may be permuted without changing the meaning of the data

4 An XML tree books book author titleyear 2000 Data on the Web S. Abiteboul P. Buneman D. Suciu author … A. Moller

5 Relational Tables bidtitleyear b1Data on the Web2000 b2An Introduction to XML and Web Technologies2006 bidauthor b1S. Abiteboul b1P. Buneman b1D. Suciu b2A. Moller b2M. Schwartzbach

6 Query Usage Scenarios Data-oriented to query XML data, like using SQL to query relational databases Document-oriented to retrieve parts of documents to provide dynamic indexes to perform context-sensitive searching to generate new documents as combinations of existing documents Programming to automatically generate documentation, similar to the Javedoc tool Hybrid to retrieve information from hybrid data, such as patient records

7 XQuery Design Requirements XML syntax and human-readable Declarative Namespace aware Coordinate with XML Schema Support simple and complex data types Can combine multiple documents Can transform and create XML trees

8 What is XQuery? XQuery is the language for querying XML data XQuery 1.0 is a strict superset of XPath 2.0 XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators XQuery has the extra expressive power to join information from different sources and generate new XML fragments XQuery and XSLT are both domain-specific languages for combining and transforming XML data from multiple sources XQuery is designed from scratch, XSLT is an intellectual descendant of CSS Technically, they may emulate each other XQuery is defined by the W3C XQuery is supported by all the major database engines (IBM, Oracle, Microsoft, etc.)

9 XQuery Prolog XQuery expressions are evaluated relatively to a context, which is explicitly provided by a prolog Declarations specify various parameters for the XQuery processor, such as: xquery version "1.0"; declare xmlspace preserve; declare xmlspace strip; declare default element namespace URI; declare default function namespace URI; import schema at URI; declare namespace NCName = URI;

10 Implicit Declarations Declarations implicitly defined in any XQuery implementation: declare namespace xml = " declare namespace xs = " declare namespace xsi = " declare namespace fn = " declare namespace xdt = " declare namespace local = "

11 XPath vs. XQuery XPath expressions are also XQuery expressions XPath expressions are required to be evaluated in a static context, which must be provided by the invoking application The XQuery prolog gives the required static context The initial context node, position, and size are undefined because an XQuery expression may work on multiple XML documents Only axes child, descendant, parent, attribute, self, and descendant-of-self are required to be implemented in XQuery.

12 Datatype Expressions Same atomic values as XPath 2.0 Also lots of primitive simple values: xs:string("XML is fun") xs:boolean("true") xs:decimal("3.1415") xs:float(" E23") xs:dateTime(" T13:20:00-05:00") xs:time("13:20:00-05:00") xs:date(" ") xs:gYearMonth(" ") xs:gYear("1999") xs:hexBinary("48656c6c6f0a") xs:base64Binary("SGVsbG8K") xs:anyURI(" xs:QName("rcp:recipe")

13 XML Expressions XQuery expressions may compute new XML nodes Expressions may denote element, character data, comment, and processing instruction nodes Each node is created with a unique node identity Constructors may be either direct or computed

14 Direct Constructors Uses the standard XML syntax The expression baz evaluates to the given XML fragment Note that nodes are created with unique identity and therefore is evaluates to false (operators is, > are used to compare nodes on identity and document order)

15 Same Namespace Declarations (1/3) declare default element namespace " John Doe CEO, Widget Inc. (202)

16 Same Namespace Declarations (2/3) declare namespace b = " John Doe CEO, Widget Inc. (202)

17 Same Namespace Declarations (3/3) John Doe CEO, Widget Inc. (202)

18 Enclosed Expressions Expressions create an element named numbers which has a single character data node with value (if boundary-space is set to strip) {1, 2, 3, 4, 5} {1, "2", 3, 4, 5} {1 to 5} 1 {1+1} {" "} {"3"} {" "} {4 to 5} Enclosed expressions are allowed inside attribute values

19 Explicit Constructors The constant expression John Doe CEO, Widget Inc. (202) Can be written as: element card { namespace { " }, element name { text { "John Doe" } }, element title { text { "CEO, Widget Inc." } }, element { text { } }, element phone { text { "(202) " } }, element logo { attribute uri { "widget.gif" } } }

20 Computed QNames Qualified names can be replaced by enclosed expressions evaluating to equivalent strings: element { "card" } { namespace { " }, element { "name" } { text { "John Doe" } }, element { "title" } { text { "CEO, Widget Inc." } }, element { " " } { text { } }, element { "phone" } { text { "(202) " } }, element { "logo" } { attribute { "uri" } { "widget.gif" } }

21 Biliingual Business Cards Controlled by a global variable $lang : element { if ($lang="Danish") then "kort" else "card" } { namespace { " }, element { if ($lang="Danish") then "navn" else "name" } { text { "John Doe" } }, element { if ($lang="Danish") then "titel" else "title" } { text { "CEO, Widget Inc." } }, element { " " } { text { } }, element { if ($lang="Danish") then "telefon" else "phone"} { text { "(202) " } }, element { "logo" } { attribute { "uri" } { "widget.gif" } }

22 FLWOR Expressions Used for general queries: { for $s in fn:doc("students.xml")//student let $m := $s/major where fn:count($m) ge 2 order by return { $s/name/text() } }

23 The Difference Between For and Let (1/4) A FLWOR expression for $x in (1, 2, 3, 4) let $y := ("a", "b", "c") return ($x, $y) Output 1, a, b, c, 2, a, b, c, 3, a, b, c, 4, a, b, c

24 The Difference Between For and Let (2/4) A FLWOR expression let $x in (1, 2, 3, 4) for $y := ("a", "b", "c") return ($x, $y) Output 1, 2, 3, 4, a, 1, 2, 3, 4, b, 1, 2, 3, 4, c

25 The Difference Between For and Let (3/4) A FLWOR expression for $x in (1, 2, 3, 4) for $y in ("a", "b", "c") return ($x, $y) Output 1, a, 1, b, 1, c, 2, a, 2, b, 2, c, 3, a, 3, b, 3, c, 4, a, 4, b, 4, c

26 The Difference Between For and Let (4/4) A FLWOR expression let $x := (1, 2, 3, 4) let $y := ("a", "b", "c") return ($x, $y) Output 1, 2, 3, 4, a, b, c

27 An XML Example Document: books.xml Everyday Italian Giada De Laurentiis Harry Potter J K. Rowling

28 An XML Example Document: books.xml (cont.) XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan Learning XML Erik T. Ray

29 Path Expressions The following path expression is used to select all the title elements in the "books.xml" file: doc("books.xml")/bookstore/book/title The XQuery above will extract the following: Everyday Italian Harry Potter XQuery Kick Start Learning XML

30 Predicates The following predicate is used to 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] The XQuery above will extract the following: Harry Potter J K. Rowling

31 FLOWR Expressions The following FLWOR expression will 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 for $x in doc("books.xml")/bookstore/book where $x/price>30 return $x/title The result will be: XQuery Kick Start Learning XML

32 Conditional Expressions Notes on the "if-then-else" syntax: parentheses around the if expression are required. else is required, but it can be just else (). for $x in doc("books.xml")/bookstore/book return if then {data($x/title)} else {data($x/title)} The result of the example above will be: Everyday Italian Harry Potter Learning XML XQuery Kick Start

33 Comparisons General comparisons: =, !=,, >= Value comparisons: eq, ne, lt, le, gt, ge > 10 The expression above returns true if any q attributes have values greater than 10. gt 10 The expression above returns true if there is only one q attribute returned by the expression, and its value is greater than 10. If more than one q is returned, an error occurs.

34 Selecting and Filtering Elements The at keyword can be used to count the iteration: for $x at $i in doc("books.xml")/bookstore/book/title return {$i}. {data($x)} Result: 1. Everyday Italian 2. Harry Potter 3. XQuery Kick Start 4. Learning XML It is also allowed with more than one in expression in the for clause. Use comma to separate each in expression: for $x in (10,20), $y in (100,200) return x={$x} and y={$y} Result: x=10 and y=100 x=10 and y=200 x=20 and y=100 x=20 and y=200

35 Computing Joins fridge.xml: eggs olive oil ketchup unrecognizable moldy thing Find recipes that use some ingredients in the refrigerator declare namespace rcp = " for $r in fn:doc("recipes.xml")//rcp:recipe for $i in for $s in fn:doc("fridge.xml")//stuff[text()=$i] return fn:distinct-values($r/rcp:title/text() )

36 Inverting a Relation declare namespace rcp = " { for $i in distinct-values( ) return { for $r in fn:doc("recipes.xml")//rcp:recipe where return $r/rcp:title/text() } }

37 Sorting the Results declare namespace rcp = " { for $i in order by $i return { for $r in fn:doc("recipes.xml")//rcp:recipe where order by $r/rcp:title/text() return $r/rcp:title/text() } }

38 A More Complicated Sorting for $s in document("students.xml")//student order by fn:count( ) descending, fn:count($s/major) descending, xs:integer($s/age/text()) ascending return $s/name/text()

39 Using Functions declare function local:grade($g) { if ($g="A") then 4.0 else if ($g="A-") then 3.7 else if ($g="B+") then 3.3 else if ($g="B") then 3.0 else if ($g="B-") then 2.7 else if ($g="C+") then 2.3 else if ($g="C") then 2.0 else if ($g="C-") then 1.7 else if ($g="D+") then 1.3 else if ($g="D") then 1.0 else if ($g="D-") then 0.7 else 0 }; declare function local:gpa($s) { fn:avg(for $g in return local:grade($g)) }; { for $s in fn:doc("students.xml")//student return }

40 Extend the Expressive Power Using recursive function to generate an XML tree of a given height: declare function gen($n) { if ($n eq 0) else { gen($n - 1), gen($n - 1) } }; Compute the height of an XML tree: declare function local:height($x) { if (fn:empty($x/*)) then 1 else fn:max(for $y in $x/* return local:height($y))+1 };

41 A Textual Outline Cailles en Sarcophages pastry chilled unsalted butter flour salt ice water filling baked chicken marinated chicken small chickens, cut up Herbes de Provence dry white wine orange juice minced garlic truffle oil...

42 Computing Textual Outlines declare namespace rcp = " declare function local:ingredients($i,$p) { fn:string-join( for $j in $i/rcp:ingredient return local:ingredients($j,fn:concat($p," "))),""),"") }; declare function local:recipes($r) { fn:concat($r/rcp:title/text(),"&#x0A",local:ingredients($r," ")) }; fn:string-join( for $r in fn:doc("recipes.xml")//rcp:recipe[5] return local:recipes($r),"" )

43 XML Databases How can XML and databases be merged? Several different approaches: extract XML views of relations use SQL to generate XML shred XML into relational databases

44 Automatic XML Views (1/2) xmlelement(name, "Students", select xmlelement(name, "record", xmlattributes(s.id, s.name, s.age)) from Students )

45 Automatic XML Views (2/2) Joe Average Jack Doe 18 xmlelement(name, "Students", select xmlelement(name, "record", xmlforest(s.id, s.name, s.age)) from Students )

46 Storing XML Documents Designing a specialized system for storing native XML data Using a DBMS to store the whole XML documents as text fields Using a DBMS to store the document contents as data elements It must support the XML’s ordered data model

47 Using a DBMS: Relational DTD Schema-aware An element that can occur at most once in its parent is stored as a column of the table representing its parent ParentIDIDTEXT ParentIDIDtitleyear35“S. Abiteboul” 23“Data on The Web”“2000”36“P. Buneman” 24……37“D. Suciu” The book tableThe author table references book author titleyear 2000Data on the Web S. Abiteboul P. Buneman D. Suciu author … …

48 Using a DBMS: Edge Schema-less A single table is used to store the entire document Each node is assigned an ID in depth first order references book author titleyear 2000 Data on the Web S. Abiteboul P. Buneman D. Suciu author … … root

49 Using a DBMS: Edge (cont.) SourceIDtagordinalTargetIDData 1reference12NULL 2book13NULL 2book24NULL 3author10“S. Abiteboul” 3author20“P. Buneman” 3author30“D. Suciu” 3title40“Data on The Web” 3year50“2000” The edge table