5 Querying XML

  • View
    45

  • Download
    0

Embed Size (px)

DESCRIPTION

5 Querying XML. How to access various XML data sources? XQuery , XML Query Lang, W3C Rec , Jan '07 joint work by XML Query and XSL WGs with XPath 2.0 and XSLT 2.0 Started ~1999; 2 nd Ed. in Dec 2010 influenced by many research groups and query languages - PowerPoint PPT Presentation

Text of 5 Querying XML

  • SDPL 20115. Querying XML with XQuery*5 Querying XMLHow to access various XML data sources?XQuery, XML Query Lang, W3C Rec, Jan '07joint work by XML Query and XSL WGswith XPath 2.0 and XSLT 2.0Started ~1999; 2nd Ed. in Dec 2010influenced by many research groups and query languagesQuilt, XPath, XQL, XML-QL, SQL, OQL, Lorel, ...A query language for any XML-represented data: both documents and databases

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Outline of this sectionQuick overview of XQueryReview of XPath, emphasizing XPath 2.0 vs 1.0items, types, and sequences tree model, location path expressions; comparison operatorsCentral features of XQuery (over those of XPath 2.0)element constructors, FLWOR expressionsuse cases, user-defined functions, querying relational dataComparison of XQuery and XSLT 1.0XQuery for problem solvingexamples of application to puzzlesSummary

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Capabilities of XQuery (1)XQuery allows to select, reorganize and transform XML datarespecting document content, structure, hierarchy, and orderSelection, filtering, and searchCombine and joindata from different parts of a document, or from multiple documentsSort, group, and aggregateTransform, restructure and create XML dataOperate on numbers and datesManipulate content strings

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Capabilities of XQuery (2)Closure property: Results of XML queries are also XML (well-formed document fragments)> queries can be combined, without limitExtensibility:supports user-defined functions on all data types of the data modelIn-place update of XML data not supportedspecified in XQuery Update Facility 1.0, W3C Rec. March 2011

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*XQuery in a NutshellFunctional expression language (lausekekieli) Strongly-typed: (optional) type-checking of expressions, and validation of results (Well concentrate to processing)predeclared prefix for type names: xs="http://www.w3.org/2001/XMLSchema"Extends XPath 2.0XQuery 1.0 and XPath 2.0 Functions and Operators, Rec. Jan. 2007over 100; for numbers, strings, dates and times, Booleans, documents & URIs, nodes, and sequences XQuery XPath 2.0 + XSLT' + SQL' (roughly)

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Example Queryxquery version "1.0"; (: optional declaration :) Cheap Books { for $b in doc("bib.xml")//book[@price < 50] order by $b/title return $b } XML-based syntax (XQueryX) has also been specified easier for applications, harder for humans Syntax "concise and easily understood"

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*A possible result Cheap Books Computing with Logic David Maier Benjamin Cummings 1999 Designing Internet applications Michael Leventhal Prentice Hall 1998

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*XQuery and XPathXQuery is an extension of XPath (2.0)Common data model, 108 functions and 68 operators> review some XPath firstXPath used in several other contexts, too:For pattern matching and selection in XSLTin validation rules of SchematronFor uniqueness constraints in XML SchemaFor addressing in XLink and XPointer

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*XPath in a NutshellXPath 1.0 (W3C Rec. 11/99)a compact non-XML syntax for addressing parts of XML documents (as node-sets)also operations on strings, numbers and truth valuesXPath 2.0 (W3C Rec. 1/07) extends and generalizes: data manipulated as sequences of items Item = a node or an atomic value of a simple XML Schema datatype

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Literal Atomic Values and Their TypesExamples:"-12" instance of xs:string-12 instance of xs:integer1.2 instance of xs:decimal1.2E3 instance of xs:doublestring(1.2E3) instance of xs:stringnumber("+12") instance of xs:doublexs:date("2009-05-11") instance of xs:datetrue() instance of xs:boolean

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*XPath 2.0/XQuery Type Hierarchy

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*XPath 2.0/XQuery Type Hierarchy (cont.)

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*XQuery/XPath 2.0 SequencesExpressions operate on, and return sequences ofatomic values (of simple XML Schema types) andnodesan item a singleton sequencesequences are flat: no sequences as items(1, (2, 3), (), 1) = (1, 2, 3, 1)sequences are ordered, and can contain duplicates Unlimited combination of expressions, often with automatic type conversions (e.g. for arithmetics)

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Sequence ExpressionsConstant sequences constructed by listing valuescomma (,) is a catenation operator (1, (2, 3), (), 1) = (1, 2, 3, 1)Range expressions for integer sequences:1 to 44 to 1reverse(1 to 4) -> (1, 2, 3, 4)-> ()-> (4, 3, 2, 1)

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Accessing DocumentsXQuery operates on nodes accessible by input functionsfn:doc("URI") document-node of the XML document available at URIroughly same as document("URI") in XSLT 1.0fn:collection("URI")sequence of nodes from URIassociation defined by implementationpredeclared prefix for the default function namespace: fn="http://www.w3.org/2005/04/xpath-functions"

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*XQuery/XPath 2.0 Data ModelDocuments are viewed as trees made of six types of nodes:document (additional root above document element)element nodesattribute nodestext nodesComments and processing instructions Obs 1: No entity nodes, and no CDATA sectionsObs 2: Namespace nodes have been deprecated

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Document TreesDefined in Sect. 5 of XPath 1.0 specfor XSLT/XPath 2.0 & XQuery in their joint Data Model Element nodes have elements, text nodes, comments and processing instructions of their (direct) content as their childrenNB: attribute nodes are not children (but have a parent)> they have no siblings eitherthe string value of an document/element is the concatenation of its all text-node descendants

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Document OrderDocument order of nodes:= their left-to-right pre-orderDocument root firstOther nodes in the order of the first character of their XML markup in the document text> an element precedes it's attribute nodes, which precede any content nodes of the elementOrder btw nodes belonging to different trees is implementation dependent, but consistent and stable

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Location PathsXPath can select any parts of a document tree using Location pathsevaluated with respect to a context item (.)assigned on path steps, after the first onePath expression typically starts with $x or doc()Result: sequence of nodes, in document order, without duplicates

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Path ExpressionsSimilar to XPath 1.0: [/ [/]]Expr/ /Exprbut steps more liberal:arbitrary expressions OK, but steps before the last one must produce node sequences6 (of 13 XPath) axes required: child, descendant, attribute, self, descendant-or-self, parentothers (except namespace) optional, available if the Full Axis Feature is supportedwith document-order operators () sufficient for expressing queries ( Exercises)

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Location pathsConsist of location steps separated by '/'each step produces a sequence of itemssteps evaluated left-to-right, each item in turn as the context itemComplete location step: AxisName:: NodeTest ([PredicateExpr])*axis specifies the tree relationship between the context node and the selected nodes node test restricts the type and and name of nodesfiltered further by 0 or more predicates

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Location steps: AxesIn total 12 axes (~ directions in tree)for staying at the context node: selffor going downwards:child, descendant, descendant-or-selffor going upwards:parent, ancestor, ancestor-or-selffor moving towards start/end of the document:preceding-sibling, following-sibling, preceding, followingSpecial axesattribute (namespace deprecated in XPath 2.0) (Axes required in XQuery implementations underlined)

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Notes on Location Paths (1)XPath 2.0 allows unrestricted expressions as stepsbut intermediate steps must produce nodes onlyNumeric predicates support array-style access: $rows[$i]Predicates evaluated step at a time. This sometimes causes confusion with shorthand notations:doc("doc.xml")//title[3] third title child of each parent (likely none!). Why?= doc("doc.xml")/ descendant-or-self::node()/child::title[3]To get the third title in the doc use (doc("doc.xml")//title)[3]

    5. Querying XML with XQuery

  • SDPL 20115. Querying XML with XQuery*Notes on Location Paths (2)References to attributes and subelements easy to use as predicatesGet divisions that are of class C or have a head: doc("doc.xml")//div[@class="C" or head]Values are coerced to Booleans on demandstring/sequence true iff non-emptynumber false if and only if zero or NaN(but a single number as a predicate tests f