1 / 101

CS 253: Topics in Database Systems: C2

CS 253: Topics in Database Systems: C2. Dr. Alexandra I. Cristea http://www.dcs.warwick.ac.uk/~acristea/. Previously we looked at: XML XSL XSLT Next: XPath XQuery. XPath. XPath. XPath is a syntax for defining parts of an XML document

austin
Download Presentation

CS 253: Topics in Database Systems: C2

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.

E N D

Presentation Transcript


  1. CS 253: Topics in Database Systems: C2 Dr. Alexandra I. Cristea http://www.dcs.warwick.ac.uk/~acristea/

  2. Previously we looked at: • XML • XSL • XSLT • Next: • XPath • XQuery

  3. XPath

  4. XPath • XPath is a syntax for defining parts of an XML document • XPath uses path expressions to navigate in XML documents • XPath contains a library of standard functions • XPath is a major element in XSLT • XPath is a W3C recommendation, thus a Standard (16. November 1999 )

  5. XPath Path Expressions • Uses path expressions to select nodes or node-sets in an XML document. • These path expressions look very much like the expressions you see when you work with a traditional computer file system.

  6. XPath Standard Functions • over 100 built-in functions. • string values, • numeric values, • date and time comparison, • node and QName manipulation, • sequence manipulation, • Boolean values, • and more.

  7. XPath Terminology • Nodes • Atomic values • Items (atomic values or nodes) • Relationships of nodes • Parent • Children • Siblings • Ancestors • Descendants

  8. XPath Nodes • 7 kinds of nodes: • element, • attribute, • text, • namespace, • processing-instruction, • comment, and • document (root) nodes. • XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node).

  9. Document (root) node Element node Attribute node Nodes Examples <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>

  10. Atomic values Examples* <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore> *nodes with no children or parent

  11. Selecting nodes

  12. Examples of selecting nodes

  13. Predicates • Predicates are used to find a specific node or a node that contains a specific value. • Predicates are always embedded in square brackets.

  14. Example predicates

  15. Example predicates – cont. Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

  16. Selecting Unknown Nodes

  17. Example: selecting several paths Selects all the title AND price elements in the document Selects all the title elements of the book element of the bookstore element AND all the price elements in the document

  18. Location Path Expression • A location path can be absolute or relative. • An absolute location path: /step/step/... • A relative location path: step/step/... • Location step: axisname::nodetest[predicate]

  19. XPath Axes

  20. axisname::nodetest[predicate] • //DDD/parent::* <AAA>  <BBB>               <DDD>                </DDD> </BBB> </AAA>

  21. axisname::nodetest[predicate] • //BBB/child::* <AAA> <BBB>  <DDD>                </DDD>           </BBB> </AAA> Note: /AAA is equivalent to /child::AAA

  22. More examples • http://www.zvon.org/xxl/XPathTutorial/General/examples.html • Check basics, //, *, predicates, attributes, functions (new ones: count, name, normalize-space, starts-with, contains, string-length, floor, ceiling), axes, operators (mod) • Note: The ancestor, descendant, following, preceding and self axes partition a document (ignoring attribute and namespace nodes): they do not overlap and together they contain all the nodes in the document. (see example)

  23. XPath Conclusion • We have learned: • XPath definition • Path expressions • Standard functions • Terminology • Predicates • Location paths • Axes • Some operators

  24. Before we go on, one more thing about XML: • XML Namespaces

  25. Naming ambiguity

  26. The Idea to Solve it • Assign a URI (~ URL) to every sub-language: • E.g., for XHTML 1.0: http://www.w3.org/1999/xhtml • Qualify element names with URIs: • {http://www.w3.org/1999/xhtml}head Web Naming and Addressing Overview (URIs, URLs, ...)

  27. The actual solution • Namespace declarations bind URIs to prefixes: • Default namespace (no prefix) declared with: xmlns=“…” • Lexical Scope • Attribute names can also be prefixed

  28. Applying namespaces

  29. Next we look at how to query XML • This can be done, to some extent, as we have seen, within XSLT, • but the main language developed for this purpose is …

  30. XQuery

  31. What is XQuery? • XQuery is the language for querying XML data • XQuery for XML is like SQL for databases • XQuery is built on XPath expressions • XQuery is defined by the W3C • XQuery is supported by all the major database engines (IBM, Oracle, Microsoft, etc.) • XQuery is a W3C recommendation (Jan 2007) thus a standard

  32. Maturity Levels Towards W3C Recommendation • Working Draft (WD) • Candidate Recommendation (CR) • Proposed Recommendation (PR) • W3C Recommendation (REC)

  33. XQuery and XPath • XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators.

  34. XQuery - Examples of Use • Extract information to use in a Web Service • Generate summary reports • Transform XML data to XHTML • Search Web documents for relevant information

  35. Usage Scenario: Document-Oriented • Queries could be used • To retrieve parts of documents • To provide dynamic indexes • To perform context-sensitive searching • To generate new documents as combinations of existing ones

  36. Usage Scenario: Programming • Queries could be used to automatically generate documentation

  37. Usage Scenario: Hybrid • Queries could be used to data mine hybrid data, such as patient records

  38. XQuery compared to XPath • XQuery 1.0 is a strict superset of XPath 2.0 •  XPath 2.0 expression is directly an XQuery 1.0 expression (a query) • The extra expressive power is the ability to: • Join information from different sources and • Generate new XML fragments

  39. Relationship to XSLT • XQuery, XSLT: both domain-specific languages for combining and transforming data from multiple sources • different in design - historical reasons • XQuery: designed from scratch • XSLT: intellectual descendant of CSS • technically, they may emulate each other

  40. XQuery query makeup • Prolog • Like XPath, XQuery expressions are evaluated relatively to a context • explicitly provided by a prolog (header) ~ header with definitions • Body • The actual query

  41. XQuery Ex.: Prolog + Query

  42. XQuery Prolog (i.e., header(s)) • Settings define various parameters for the XQuery processor language, such as: xquery version "1.0"; module namespace math = "http://example.org/math-functions"; declare base-uri "http://example.org"; declare default element namespace "http://example.org/names"; declare namespace xs= "http://www.w3.org/2001/XMLSchema" import module "http://www.w3.org/2003/05/xpath-functions" at "logo.xq“; declare variable $x as xs:integer := 7; declare function addLogo($root as node()) as node()*{ }; (: etc :)

  43. XQuery body: XQuery capabilities • Generate • Join • Select

  44. Generate: constructors • XQuery expressions may compute new XML nodes • Expressions may denote: • element, character data, comment and processing instruction nodes •  node is created with a unique node identity • Constructors may be either • direct or • computed

  45. Direct constructors in XQuery <XMLfragment>my fragment </XMLfragment> • Evaluates to the given XML fragment • Try out at*: • http://support.x-hive.com/xquery/index.html

  46. Explicit, computed constructors

  47. Variable bindings (implicit constructors) <employee empid="{$id}"> <name>{$name}</name> {$job} <deptno>{$deptno}</deptno> <salary>{$SGMLspecialist+100000}</salary> </employee>

  48. How to Select Nodes with XQuery? • Functions • XQuery uses functions to extract data from XML documents. • (X)Path Expressions • XQuery uses path expressions to navigate through elements in an XML document. • Predicates • XQuery uses predicates to limit the extracted data from XML documents.

  49. Functions • doc() • function to open a file • Example: • doc("books.xml") • Note: A call to a function can appear where an expression may appear.

More Related