Query Languages for XMLCommon Querying TasksQuery LanguagesXML data: Running exampleDTDData modelXPathXPath constructsDownward traversalExamples:Filters (qualifiers)Upward traversalSidewaysSlide 14XQueryFLWR ExpressionsjoinConditional expressionSummary and ReviewQSX (LN 3) 1Query Languages for XMLXPath XQueryXSLT (not being covered today!)(Slides courtesy Wenfei Fan, Univ Edinburgh and Bell Labs)QSX (LN 3) 2Common Querying Tasks Filter, select XML values–Navigation, selection, extractionMerge, integrate values from multiple XML sources–Joins, aggregationTransform XML values from one schema to another–XML constructionQSX (LN 3) 3Query LanguagesXPath –Common language for navigation, selection, extraction–Used in XSLT, XQuery, XML Schema, . . .XQuery 1.0: XML XML–Strongly-typed query language–Large-scale database access–Safety/correctness of operations on dataXSLT: XML XML, HTML, Text–Loosely-typed scripting language–Format XML in HTML for display in browser–Highly tolerant of variability/errors in dataQSX (LN 3) 4XML data: Running exampleXML input: www.a.b/bib.xml<book year=“1996”><title> HTML </title><author> <last> Lee </last> <first> T. </first></author><author> <last> Smith</last> <first>C.</first></author><publisher> Addison-Wesley </publisher><price> 59.99 </price></book><book year=“2003”><title> WMD </title><author> <last> Bush</last> <first> G.</first></author><publisher> white house </publisher></book>QSX (LN 3) 5 DTD<!ELEMENT bib (book*) ><!ELEMENT book (title, (author+ | editor+), publisher?, price?) ><!ATTLIST book year CDATA #required ><!ELEMENT author (last, first)><!ELEMENT editor (last, first, affiliation)><!ELEMENT publisher (#PCDATA) >….QSX (LN 3) 6Data modelNode-labeled, ordered treebibtitlebookbookphone @yearpublisherauthor authortitle author publisherlastfirstlastfirstfirstlast@yearQSX (LN 3) 7XPathW3C standard: www.w3.org/TR/xpath Navigating an XML tree and finding parts of the tree (node selection and value extraction) Given an XML tree T and a context node n, an XPath query Q returns– the set of nodes reachable via Q from the node n in T – if Q is a unary query–truth value indicating whether Q is true at n in T – if Q is a Boolean query.Implementations: XALAN, SAXON, Berkeley DB XML, Monet XML – freeware, which you can play withA major element of XSLT, XQuery and XML SchemaQSX (LN 3) 8XPath constructsXPath query Q:–Tree traversal: downward, upward, sideways–Relational/Boolean expressions: qualifiers (predicates)–Functions: aggregation (e.g., count), string functions//author[last=“Bush”]//book[author/last=“Bush”]/title | //book[author/last=“Blair”]/title bibtitlebookbookphone @yearpublisherauthor authortitle author publisherlastfirstlastfirstfirstlast@yearQSX (LN 3) 9Downward traversalSyntax:Q ::= . | l | @l | Q/Q | Q | Q | //Q | /Q | Q[q]q ::= Q | Q op c | q and q | q or q | not(q) .: self, the current nodel: either a tag (label) or *: wildcard that matches any label@l: attribute/, |: concatenation (child), union//: descendants or self, “recursion”[q]: qualifier (filter, predicate)–op: =, !=, <=, <, >, >=, >–c: constant–and, or, not(): conjunction, disjunction, negationExistential semantics: /bib/book[author/last=“Bush”]QSX (LN 3) 10Examples:parent/child: /bib/bookancestor//descendant: bib//last, //last wild card: bib/book/*attributes: bib/book/@yearattributes with wild cards: //book/@*union: editor | authorAre book/author and //author “equivalent” at context nodes (1) root, (2) book, (3) author?bibtitlebookbookphone @yearpublisherauthor authortitle author publisherlastfirstlastfirstfirstlast@yearQSX (LN 3) 11Filters (qualifiers)//book[price]/title -- titles of books with a price//book[@year > 1991]/title -- titles of books published after 1991//book[title and author and not(price)]/titletitles of books with authors, title but no price//book[author/last = “Bush”]/title titles of books with an author whose last name is Bush//book[author or editor]/title titles of books with either an author or an editor Existential semantics: What is /[//@id]? /[//[not(@id)]]? /[not(//[not(@id))]] ?QSX (LN 3) 12Upward traversalSyntax:Q ::= . . . | ../Q | ancestor ::Q | ancestor-or-self::Q../: parentancestor, ancestor-or-self: recursion Example://author[../title = “WMD”]/last find the last names of authors of books with the title “WMD” ancestor :: book[//last=“Bush”] find book ancestors with “Bush” as its last descendant Are the following equivalent to each other (context node: a book)? ../book/author, ./authorQSX (LN 3) 13Sideways Syntax:Q ::= . . . | following-sibling ::Q | preceding-sibling::Qfollowing-sibling: the right siblingspreceding-sibling: the left siblingsposition function (starting from 1): e.g., //author[position( ) < 2]Example:following-sibling :: book [//last=“Bush”] find the books that are right siblings and are written by Bushpreceding-sibling :: book[//last=“Bush”]find the books that are left siblings and are written by BushQSX (LN 3) 14Query Languages for XMLXPath XQueryXSLTQSX (LN 3) 15XQueryW3C working draft www.w3.org/TR/xqueryFunctional, strongly typed query language: Turing-completeXQuery = XPath + … for-let-where-return (FLWR) ~ SQL’s SELECT-FROM-WHERE Sort-byXML construction (Transformation) Operators on types (Compile & run-time type tests)+ User-defined functionsModularize large queriesProcess recursive data+ Strong typingEnforced statically or dynamicallyImplementation: GALAX, SAXONhttp://www-db.research.bell-labs.com/galax/http://www.saxonica.comQSX (LN 3) 16FLWR ExpressionsFor, Let, Where, OrderBy, returnQ1: Find titles and authors of all books published by Addison-Wesley after 1991.<answer>{for $book in /bib/bookwhere $book/@year > 1991 and $book/publisher=‘Addison-Wesley’return <book> <title> {$book/title } </title>, for $author in $book/author return <author> {$author } </author> </book>}</answer>for loop; $x: variablewhere: condition test; selectionreturn: evaluate an expression and return its valueQSX (LN 3) 17joinFind books that cost more at Amazon than at
View Full Document