Untitled presentation.pdf
Document Details
Uploaded by EffusiveWilliamsite1674
Tags
Full Transcript
Querying XML XPath Querying XML Not as mature as SQL ▪ Relatively new ▪ No underlying algebra (as in relational algebra) Related Topics ( to be followed) development 1. XPath 2. XSLT XPATH “The Basic Building Block” XPath Widely adopted -- in XML-Schema and in many query l...
Querying XML XPath Querying XML Not as mature as SQL ▪ Relatively new ▪ No underlying algebra (as in relational algebra) Related Topics ( to be followed) development 1. XPath 2. XSLT XPATH “The Basic Building Block” XPath Widely adopted -- in XML-Schema and in many query languages. About as expressive as regular path expressions CS561 - Spring 2007. 4 Data Model The root bi Processing Comment instruction b The root element bo bo ok ok pu au bli th.... sh or er Addison-Wesley Ahmad Fareed Data Model Example 1 For this simple doc: Some emphasis here. Some more stuff. Might be represented as: root text text text text Data Model Example 2 IDG books Rick Hull Simon North XML complete 1997 Freeman Jeffrey D. Ullman Principles of Database 1998 Element Context Meaning of element can depend upon its context … … Want to search for, e.g. title of book, not title of person XPath exploits sequential and hierarchical context of XML to specify elements by their context (i.e. location in hierarchy) Context All XPath expressions are evaluated in the context of a particular node (location) in the document. That node is called the context node. The context size is the number of children of the context node's parent. For example, if the context node is one of seven children of its parent, the context size is seven. The context position is the child number of the context node relative to its parent. For example, if the context node is the third of seven children of its XPath Goal = Permit access some nodes from document XPath main construct : Axis navigation Navigation step : axis + node-test + predicates Examples descendant::node() child::author attribute::booktitle =“XML” XPath XPath path consists of one or more navigation steps, separated by “/” Navigation step : axis + node-test + predicates Examples /descendant::node() /child::author /descendant::node() /child::author [parent /attribute::booktitle =“XML”] XPath offers shortcuts : no axis means child // ≡ /descendant-or-self::node()/ 11 XPath- Child Axis Navigation author is shorthand for child::author. Examples: aaa -- all the children nodes labeled aaa aaa/bbb -- all the bbb grandchildren of aaa children */bbb all the bbb grandchildren of any child context node 1 aaa 2 ccc 3 aaa 4 5 6 7 bbb aaa bbb ccc Notes: CS561 - Spring 2007. 12. -- the context node XPath- Child Axis Navigation /doc -- all doc children of the root./aaa -- all aaa children of the context node (equivalent to aaa) text() -- all text children of context node node() -- all children of the context node (includes text and attribute nodes).. -- parent of the context node.// -- the context node and all its descendants // -- the root node and all its descendants //text() -- all the text nodes in the document CS561 - Spring 2007. 13 Predicates -- the second child node of the context node chapter -- the fifth chapter child of context node [last()] -- the last child node of the context node chapter[title=“introduction”] -- the chapter children of the context node that have one or more title children whose string-value is “introduction” (string-value is concatenation of all text on descendant text nodes) person[.//firstname = “joe”] -- the person children of the context node thatCS561 have in their descendants - Spring 2007. 14 a Axis navigation So far, our expressions have moved us down by moving to children nodes. Exceptions are :. stay where you are / go to the root // all descendants of the root.// all descendantsCS561 of the context node - Spring 2007. 15 Axis navigation XPath has several axes: ancestor, ancestor-or-self, attribute, child, descendant, descendant-or-self, following, following-sibling, namespace, parent, preceding, preceding-sibling, self Some of these describe single nodes: self, parent Some describe sequences of nodes: All others CS561 - Spring 2007. 16 CS561 - Spring 2007. 17 XPath Navigation Axes ancestor preceding-sibling following-sibling self child attribute preceding following namespace descendant CS561 - Spring 2007. 18 XPath Abbreviated Syntax (nothing) child:: @ attribute:: // /descendant-or-self::node(). self::node().// descendant-or-self::node.. parent::node() (document root) So Far Differences between SQL and XPATH? What are similar query capabilities? What features does SQL have, but not XPATH? What features does XPATH support, but not SQL? Is XPath a full-fledged query language? CS561 - Spring 2007. 19 XPath examples I (1) The following XML document example is the one used on the introduction to XML. Max Toula We will now demonstrate some very simple XPath examples using that document. XPATH Tool XPath examples I (2) Select all pet elements //pet or alternatively //pets/pet or //pets/child::* Select the first pet //pets/pet Select all pets of type dog //pet[@type ="dog"] Select all pets of white color //pet[@color="white"] Select the color of all dogs Data Model The root bi Processing Comment instruction b The root element bo bo ok ok pu au bli th.... sh or er Addison-Wesley Ahmad Fareed Data Model Example 1 For this simple doc: Some emphasis here. Some more stuff. Might be represented as: root text text text text Data Model Example 2 IDG books Rick Hull Simon North XML complete 1997 Freeman Jeffrey D. Ullman Principles of Database 1998 Element Context Meaning of element can depend upon its context … … Want to search for, e.g. title of book, not title of person XPath exploits sequential and hierarchical context of XML to specify elements by their context (i.e. location in hierarchy) XML PATH EXAMPLES Using XPATH /topic Returns the root element. //title Returns any element. Using XPATH is easy. //section/title Returns only the element that is a child of a element. //p Returns any element. //p[@audience='expert'] Returns any element where the @audience attribute is set to expert. Examples A simple example.//p[not(@audience)] Returns any element where An advanced example. the @audience attribute is missing. Another advanced example. //p[not(@audience='admin')] Returns any element where the @audience attribute is not of value admin OR is missing. This screenshot shows the XPATH axes //p[text()='To start this process'] Returns any elements that start with the text string To start this process. //p[contains(.,'button')] Returns any element that contain the text string button somewhere in the text. The End. //image[not(alt)] The Age Of The /Catalog/Album/@artist Understatement Standing Next To Me /Catalog/Album/Track Calm Like You /Catalog/Album[@artist="Kings O Separate and Ever Deadly Leon"] The Chamber /Catalog/Album[@artist="Kings O Only The Truth Leon"]/Track Knocked Up /Catalog/Album/Track[@rating>2] Charmer /Catalog/Album/Track/text() On Call