01 / 77Mastering XPath and XQuery Techniques for XML Querying
1 / 17
Download Presentation
Mastering XPath and XQuery Techniques for XML Querying
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.
XPath • A language of “path expressions” • Loosely related to “file paths” • root (/) • sequential composition (p/q) • wildcards (*) • axis steps (child, parent, descendant, etc.) • also: filters, text nodes, label tests • plus positional & string functions • Used for navigation • component of XSLT, XQuery, etc.
Partition ancestor preceding following self descendant
Syntax • axis ::= • test ::= * | text() | node() | a | b | @a | ... • p ::= ax::tst[q] | p/p’ • q ::= p | q and q’ | q or q’ | not(q) | ... • ap ::= /p • child | descendant | descendant-or-self • | parent | ancestor | ancestor-or-self • | preceding-sibling | following-sibling • | self | preceding | following
Abbreviations • /a = child::a • //a = descendant-or-self::*/child::a • . = self::* • .. = parent::* • Starting path with “/” means “start from document root” • this is a special node above the root element
Sequential composition doc r a b a b c b b a a b c a c b d d d d b a e e b /child::*/child::*
Node label tests doc r a b a b c b b a a b c a c b d d d d b a e e b /r/a//b
Node label tests doc r a b a b c b b a a b c a c b d d d d b a e e b /r/a//*
Sequential composition doc r a b a b c b b a a b c a c b d d d d b a e e b //a/following-sibling::c
Sequential composition doc r a b a b c b b a a b c a c b d d d d b a e e b //a/following-sibling::b//*
Filters doc r a b a b c b b a a b c a c b d d d d b a e e b //a[b]
Filters doc r a b a b c b b a a b c a c b d d d d b a e e b //a[b]/c
Filters doc r a b a b c b b a a b c a c b d d d d b a e e b //a[following-sibling::c]
Positional tests doc r a b a b c b b a a b c a c b d d d d b a e e b //*[position()=2] (or just //a[2])
Positional tests doc r a b a b c b b a a b c a c b d d d d b a e e b //a/*[first()]
Positional tests doc r a b a b c b b a a b c a c b d d d d b a e e b //a/*[last()]
Quiz 1. Write XPath to select red nodes. 2. Without using child. 3. Or filters. doc r a b a b c b b a a b c a c b d d d d b a e e b
Attributes & Text r a a b b att = “bar” b b att = “foo” c “1234” c “efgh” “abcd” “1234” //a/@att
Attributes & Text r a a b b att = “bar” b b att = “foo” c “1234” c “efgh” “abcd” “1234” //a/b/text()
Equality r a a b b att = “bar” b b att = “foo” c “1234” c “efgh” “abcd” “1234” //a[@att=”bar”]//*/text()
Equality r a a b b att = “bar” b b att = “foo” c “1234” c “efgh” “abcd” “1234” //a[@att=”bar”]
Equality Child, Descendant do not select attributes! r a a b b att = “bar” b b att = “foo” c “1234” c “efgh” “abcd” “1234” //a[@att=”bar”]//*
Equality r a a b b att = “bar” b b att = “foo” c “1234” c “efgh” “abcd” “1234” //a[@att=”bar”]//text()
Equality quiz r r is selected! Equality of sequences evaluates to true if there is any common value in the two sequences a a b b att = “bar” b b att = “foo” c “1234” c “efgh” “abcd” “1234” /r[a/b/text() = a/b/c/text()]
Tree patterns • A graphical notation for (downward) XPath queries/filters Descendant a a[b]//c/d ≅ Selected node b c d
Tree pattern matching • A function h: P -> T such that: • Child edges map to edges • Descendant edges map to paths a a a b b c a d d d d b a c e e b d
Semantics of XPath • Represent tree as T = (V,E,λ,<) • Σ is set of possible node labels • E ⊆ V × V is parent/child edge relation • λ : V → Σ gives node labels • < ⊆ V × V linearly orders children of each node • For simplicity, will ignore text nodes, attributes • but in general these need to be modeled too!
Semantics of XPath: steps • Ax[self](T) = {(x,x) | x ∈ V} • Ax[child](T) = E • Ax[descendant](T) = E+ • Ax[descendant-or-self](T) = E* • Ax[parent](T) = {(y,x) | (x,y) in E} • ... • Ax[following-sibling](T) = {(x,y) | x < y}
What can XPath not do (well)? • Construct new XML documents • Combine information from different parts of document • Joins • Abstraction over parts of query • Function definitions/recursion
XQuery - a query language for XML • Goals: • “SQL-like” query language for XML • Support query optimization • Support data types/XML Schema (will cover next week) • Design: • Purely functional (more or less) • Every expression evaluates to a value (= sequence of XML trees or primitive values) • Extends XPath 2.0 with comprehensions, functions
A first example • for $x in document(“books.xml”)/books/book • where $x/author=”Abiteboul” • return <result> • <title>{$x/title/text()}</title> • <year>{$x/year/text()}</year></result> • ... • <result><title>Data on the Web</title> • <year>2000</year></result> • <result><title>Web Data Management</title> • <year>2011</year></result> document() loads data from file/by name
Atomic values • Integers 1,2,3 • Strings 'abcd', "abcd" • Dates / times • Other basic types from XML Schema (will cover these later)
Values • Atomic constants (last slide) • XML trees • <elt att1=v1 ... attn=vn>...value seq...</elt> • Value sequences are sequences of atomic/tree values • (), (v1,v2, ..., vn) • cannot be nested, i.e., ((v1,v2), v3) = (v1,v2,v3) • however, v1 could be an element with another sequence as content • Formally: • v ::= c | <elt att=v ... att=v>{vs}</elt> • vs ::= () | (v1,...,vn)
Variables • In XQuery, variables always start with $ • $x, $y, $z, $i • This is common in other W3C standards with human-readable syntax • A variable denotes a value sequence (more or less)
XML constructors • XML values can be embedded in XQuery directly • <element att1=”v1” ...>...</element> • Can “antiquote” to embed XQuery expressions in elements • <element>{$x/a/b}</element> • Can explicitly construct elements (with arbitrary names, attributes • element $foo { attribute {$bar} {$baz}, • text {$some_text}}