XML Query Languages XPATH XQUERY Zaki Malik November 11, 2008.

Slides:



Advertisements
Similar presentations
2/10/05Salman Azhar: Database Systems1 XML Query Languages Salman Azhar XPATH XQUERY These slides use some figures, definitions, and explanations from.
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 …
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
SQL Group Members: Shijun Shen Xia Tang Sixin Qiang.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
XML, XML Schema, Xpath and XQuery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
SQL CSET 3300.
XPath Eugenia Fernandez IUPUI. XML Path Language (XPath) a data model for representing an XML document as an abstract node tree a mechanism for addressing.
From Semistructured Data to XML: Migrating The Lore Data Model and Query Language Roy Goldman, Jason McHugh, Jennifer Widom Stanford University
1 XPath Path Expressions Conditions. 2 Paths in XML Documents uXPath is a language for describing paths in XML documents. uReally think of the semistructured.
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 Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
Winter 2002Arthur Keller – CS 18018–1 Schedule Today: Mar. 12 (T) u Semistructured Data, XML, XQuery. u Read Sections Assignment 8 due. Mar. 14.
1 XML Document Type Definitions XML Schema. 2 Well-Formed and Valid XML uWell-Formed XML allows you to invent your own tags. uValid XML conforms to a.
Fall 2001Arthur Keller – CS 18017–1 Schedule Nov. 27 (T) Semistructured Data, XML. u Read Sections Assignment 8 due. Nov. 29 (TH) The Real World,
1 SQL/PSM Procedures Stored in the Database General-Purpose Programming.
1 XML Semistructured Data Extensible Markup Language Document Type Definitions.
XML –Query Languages, Extracting from Relational Databases ADVANCED DATABASES Khawaja Mohiuddin Assistant Professor Department of Computer Sciences Bahria.
1 XML Query Languages XPATH XQUERY. 2 XPATH and XQUERY uXPATH is a language for describing paths in XML documents. wReally think of the semistructured.
1 XQuery Values FLWR Expressions Other Expressions.
1 Query Languages for XML XPath XQuery XSLT. 2 The XPath/XQuery Data Model uCorresponding to the fundamental “relation” of the relational model is: sequence.
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.
1 XML Semistructured Data Extensible Markup Language Document Type Definitions.
Overview of XPath Author: Dan McCreary Date: October, 2008 Version: 0.2 with TEI Examples M D.
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
XML Document Type Definitions XML Schema. Motivation for Semistructured data Serves as a model suitable for integration of databases Notations such as.
Lecture 6 of Advanced Databases XML Schema, Querying & Transformation Instructor: Mr.Ahmed Al Astal.
XML Semi-structured data XML Document Type Definitions (DTD)
1 Lecture 5: XML and XQuery. 2 Semistructured Data uAnother data model, based on trees. uMotivation: flexible representation of data. wOften, data comes.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 1.
Introduction to XPath Web Engineering, SS 2007 Tomáš Pitner.
CSCE 520- Relational Data Model Lecture 2. Relational Data Model The following slides are reused by the permission of the author, J. Ullman, from the.
Document Type Definitions XML Schema
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
Advance Database S Week-7 Dr.Kwanchai Eurviriyanukul
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
XPath Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Processing of structured documents Spring 2003, Part 7 Helena Ahonen-Myka.
XPath. Why XPath? Common syntax, semantics for [XSLT] [XPointer][XSLT] [XPointer] Used to address parts of an XML document Provides basic facilities for.
Winter 2006Keller, Ullman, Cushing18–1 Plan 1.Information integration: important new application that motivates what follows. 2.Semistructured data: a.
XSLT part of XSL (Extensible Stylesheet Language) –includes also XPath and XSL Formatting Objects used to transform an XML document into: –another XML.
1 XQuery uXQuery extends XPath to a query language that has power similar to SQL. uUses the same sequence-of-items data model. uXQuery is an expression.
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
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 Information Integration Mediators Warehousing Answering Queries Using Views Slides are modified from Dr. Ullman’s notes.
Jeff Ullman: Introduction to XML 1 XML Semistructured Data Extensible Markup Language Document Type Definitions.
Semistructured Data Extensible Markup Language Document Type Definitions Zaki Malik November 04, 2008.
The Semistructured-Data Model Programming Languages for XML Spring 2011 Instructor: Hassan Khosravi.
Exam II Syllabus uStorage & Buffer Management uIndexing: Btrees & Hash uMulti-dimensional Indexing uQuery processing (relational ops) uQuery optimization.
1 XML Data Management XPath Principles Werner Nutt.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
CSE3201/CSE4500 XPath. 2 XPath A locator for items in XML document. XPath expression gives direction of navigation.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
CSE 6331 © Leonidas Fegaras XQuery 1 XQuery Leonidas Fegaras.
1 The XPath Language. 2 XPath Expressions Flexible notation for navigating around trees A basic technology that is widely used uniqueness and scope in.
1 XSLT XSLT (extensible stylesheet language – transforms ) is another language to process XML documents. Originally intended as a presentation language:
Query Languages for XML
XML: Extensible Markup Language
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
Querying and Transforming XML Data
CS 440 Database Management Systems
IST 210: Organization of Data
Basic Operations Algebra of Bags
XQuery Leonidas Fegaras.
Query Languages for XML
Presentation transcript:

XML Query Languages XPATH XQUERY Zaki Malik November 11, 2008

The XPath/XQuery Data Model uCorresponding to the fundamental “relation” of the relational model is: sequence of items. uAn item is either: 1.A primitive value, e.g., integer or string. 2.A node.

Principal Kinds of Nodes 1.Document nodes represent entire documents. 2.Elements are pieces of a document consisting of some opening tag, its matching closing tag (if any), and everything in between. 3.Attributes are names that are given values inside opening tags.

Document Nodes uFormed by doc(URL) or document(URL) (or doc(filename) or document(filename) uExample: doc(“/usr/class/cs145/bars.xml”) uAll XPath (and XQuery) queries refer to a doc node, either explicitly or implicitly.

Example DTD <!DOCTYPE Bars [ ]>

Example Document … <BEER name = ”Export” soldBy = ”JoesBar SuesBar … ”/> … An element node An attribute node Document node is all of this, plus the header ( <? xml version… ).

Nodes as Semistructured Data BARS PRICE BEERBAR name = ”JoesBar” theBeer = ”Gr.Is.” theBeer = ”Export” SoldBy = ”…” name = ”Export” Blue =document Green = element Orange = attribute Purple = primitive value bars.xml

XPATH and XQUERY uXPATH is a language for describing paths in XML documents. wReally think of the semi-structured data graph and its paths. wThe result of the described path is a sequence of items. wCompare with SQL: SQL is a language for describing relations in terms of other relations. The result of a query is a relation (bag) made up of tuples uXQUERY is a full query language for XML documents with power similar to SQL.

Path Descriptors uSimple path descriptors are sequences of tags separated by slashes (/). wThe format used is strongly reminiscent of UNIX naming conventions. wConstruct the result by starting with just the doc node and processing each tag from the left. uIf the descriptor begins with /, then the path starts at the root and has those tags, in order. uIf the descriptor begins with //, then the path can start anywhere.

Example: /BARS/BAR/PRICE … <BEER name = “Bud”, soldBy = “JoesBar, SuesBar,…”> … /BARS/BAR/PRICE describes the set with these two PRICE objects as well as the PRICE objects for any other bars.

Example: //PRICE … <BEER name = “Bud”, soldBy = “JoesBar, SuesBar,…”> … //PRICE describes the same PRICE objects, but only because the DTD forces every PRICE to appear within a BARS and a BAR.

Wild-Card * uA star (*) in place of a tag represents any one tag. uExample: /*/*/PRICE represents all price objects at the third level of nesting.

Example: /BARS/* … <BEER name = “Bud”, soldBy = “JoesBar, SuesBar,…”> … /BARS/* captures all BAR and BEER objects, such as these.

Attributes uIn XPATH, we refer to attributes by to their name. uAttributes of a tag may appear in paths as if they were nested within that tag.

Example: … <BEER name = “Bud”, soldBy = “JoesBar, SuesBar,…”> … selects all name attributes of immediate subobjects of the BARS object.

Selection Conditions uA condition inside […] may follow a tag. uIf so, then only paths that have that tag and also satisfy the condition are included in the result of a path expression.

Example: Selection Condition u/BARS/BAR/PRICE[PRICE < 2.75] … The condition that the PRICE be < $2.75 makes this price, but not the Miller price

Example: Attribute in Selection = “Miller”] … Now, this PRICE object is selected, along with any other prices for Miller.

Axes uIn general, path expressions allow us to start at the root and execute a sequence of steps to find a set of nodes at each step. uAt each step, we may follow any one of several axes. uThe default axis is child:: --- go to any child of the current set of nodes.

Example: Axes u/BARS/BEER is really shorthand for /BARS/child::BEER. is really shorthand for the attribute:: axis. wThus, = “Bud” ] is shorthand for /BARS/BEER[attribute::name = “Bud”]

More Axes uSome other useful axes are: 1.parent:: = parent(s) of the current node(s). 2.descendant-or-self:: = the current node(s) and all descendants. wNote: // is really a shorthand for this axis. 3.ancestor::, ancestor-or-self, etc.

XQuery uXQuery extends XPath to a query language that has power similar to SQL. uUses the same sequence-of-items data model as XPath. uXQuery is an expression language. wLike relational algebra --- any XQuery expression can be an argument of any other XQuery expression.

FLWR Expressions The most important form of XQuery expressions involves for-, let-, where-, return- (FLWR) clauses. 1. A qurey begins with one or more for and/or let clauses. The for’s and let’s can be interspersed. 2. Then an optional where clause. 3. A single return clause. Form: for variable in expression let variable := expression where condition return expression

Semantics of FLWR Expressions uEach for creates a loop. wlet produces only a local variable assignment. uAt each iteration of the nested loops, if any, evaluate the where clause. uIf the where clause returns TRUE, invoke the return clause, and append its value to the output. wSo return can be thought of as “add to result”

FOR Clauses FOR IN,… uVariables begin with $. uA FOR variable takes on each object in the set denoted by the path expression, in turn. uWhatever follows this FOR is executed once for each value of the variable.

Example: FOR FOR $beer IN RETURN $beer u$beer ranges over the name attributes of all beers in our example document. uResult is a list of tagged names, like Bud Miller …

LET Clauses LET :=,… uValue of the variable becomes the set of objects defined by the path expression. uNote LET does not cause iteration; FOR does.

Example: LET LET $beers := RETURN $beers uReturns one object with all the names of the beers, like: Bud, Miller,…

Order-By Clauses FLWR is really FLWOR: an order-by clause can precede the return. Form: order by With optional ascending or descending. The expression is evaluated for each assignment to variables. Determines placement in output sequence.

Example: Order-By List all prices for Export, lowest price first. let $d := document(”bars.xml”) for $p in order by $p return $p Generates bindings for $p to PRICE elements. Order those bindings by the values inside the elements. Each binding is evaluated for the output. The result is a sequence of PRICE elements.

Following IDREF’s uXQUERY (but not XPATH) allows us to use paths that follow attributes that are IDREF’s. uIf x denotes a set of IDREF’s, then x =>y denotes all the objects with tag y whose ID’s are one of these IDREF’s.

Example uFind all the beer objects where the beer is sold by Joe’s Bar for less than uStrategy: 1.$beer will for-loop over all beer objects. 2.For each $beer, let $joe be either the Joe’s- Bar object, if Joe sells the beer, or the empty set of bar objects. 3.Test whether $joe sells the beer for < 3.00.

Example: The Query FOR $beer IN /BARS/BEER LET $joe := LET $joePrice := WHERE $joePrice < 3.00 RETURN $beer Attribute soldBy is of type IDREFS. Follow each ref to a BAR and check if its name is Joe’s Bar. Find that PRICE subobject of the Joe’s Bar object that represents whatever beer is currently $beer. Only pass the values of $beer, $joe, $joePrice to the RETURN clause if the string inside the PRICE object $joePrice is < 3.00

Aggregations uXQuery allows the usual aggregations, such as sum, count, max, min. uThey take any sequence as argument. uE.g. find bars where all beers are under $5. let $bars = doc(”bars.xml”)/BARS for $price in $bars/BAR/PRICE where max($price) < 5 return