The XPath Language 1 XPath Expressions l l

  • Slides: 36
Download presentation
The XPath Language 1

The XPath Language 1

XPath Expressions l l Flexible notation for navigating around trees A basic technology that

XPath Expressions l l Flexible notation for navigating around trees A basic technology that is widely used l l uniqueness and scope in XML Schema pattern matching an selection in XSLT relations in XLink and XPointer computations on values in XSLT and XQuery 2

Location Paths l l l A location path evaluates to a sequence of nodes

Location Paths l l l A location path evaluates to a sequence of nodes The sequence is sorted in document order The sequence will never contain duplicates 3

Locations Steps l l The location path is a sequence of steps A location

Locations Steps l l The location path is a sequence of steps A location step consists of l l l an axis a nodetest some predicates axis : : nodetest [ Exp 1] [ Exp 2] … 4

Evaluating a Location Path l l A step maps a context node into a

Evaluating a Location Path l l A step maps a context node into a sequence This also maps sequences to sequences l l l each node is used as context node and is replaced with the result of applying the step The path then applies each step in turn 5

An Example A B C C E F F B D C F F

An Example A B C C E F F B D C F F E E E F F 6

An Example l Context node A B C C E F F B D

An Example l Context node A B C C E F F B D C F F E E E F F 7

An Example l descendant: : C A B C C E F F B

An Example l descendant: : C A B C C E F F B D C F F E E E F F 8

An Example l descendant: : C/child: : E A B C C E F

An Example l descendant: : C/child: : E A B C C E F F B D C F F E E E F F 9

An Example l descendant: : C/child: : E/child: : F A B C C

An Example l descendant: : C/child: : E/child: : F A B C C E F F B D C F F E E E F F 10

Contexts l The context of an XPath evaluation consists of l l l l

Contexts l The context of an XPath evaluation consists of l l l l a context node (a node in an XML tree) a context position and size (two nonnegative integers) a set of variable bindings a function library a set of namespace declarations The application determines the initial context If the path starts with ‘/’ then l l the initial context node is the root the initial position and size are 1 11

Axes l l An axis is a sequence of nodes located relative to the

Axes l l An axis is a sequence of nodes located relative to the context node XPath supports 12 different axes l child l descendant l parent l ancestor l following-sibling l preceding-sibling l attribute l following l preceding l self l descendant-or-self l ancestor-or-self 12

Axis Directions l l Each axis has a direction Forwards means document order: l

Axis Directions l l Each axis has a direction Forwards means document order: l l Backwards means reverse document order: l l child, descendant, following-sibling, following, self, descendant-or-self parent, ancestor, preceding-sibling, preceding Stable but depends on the implementation: l attribute 13

The parent Axis 14

The parent Axis 14

The child Axis 15

The child Axis 15

The descendant Axis 16

The descendant Axis 16

The ancestor Axis 17

The ancestor Axis 17

The following-sibling Axis 18

The following-sibling Axis 18

The preceding-sibling Axis 19

The preceding-sibling Axis 19

The following Axis 20

The following Axis 20

The preceding Axis 21

The preceding Axis 21

Node Tests l l l l text() comment() processing-instruction() node() * l selects all

Node Tests l l l l text() comment() processing-instruction() node() * l selects all nodes QName l selects the nodes with the qualified name *: NCName l selects the nodes with the given name in any namespace (nocolon name) NCName: * 22

Example: Selecting Attribute Nodes <wdg: widget type="gadget" xmlns: wdg="http: //www. widget. inc"> <wdg: head

Example: Selecting Attribute Nodes <wdg: widget type="gadget" xmlns: wdg="http: //www. widget. inc"> <wdg: head size="medium"/> <wdg: big><wdg: subwidget ref="gizmo"/></wdg: big> <wdg: info xmlns: xhtml="http: //www. w 3. org/TR/xhtml 1"> <xhtml: head> <xhtml: title>Description of gadget</xhtml: title> </xhtml: head> <xhtml: body> <xhtml: h 1>Gadget</xhtml: h 1> A gadget contains a big gizmo </xhtml: body> </wdg: info> </wdg: widget> /child: : wdg: widget/child: : wdg: big/child: : wdg: subwidget/attribute: : ref 23

Predicates l l l General XPath expressions Evaluated with the current node as context

Predicates l l l General XPath expressions Evaluated with the current node as context Result is coerced into a boolean l l l a number yields true if it equals the context position a string yields true if it is not empty a sequence yields true if it is not empty 24

Examples l l The recipes contain sugar as an ingredient: /descendant: : rcp: recipe[descendant:

Examples l l The recipes contain sugar as an ingredient: /descendant: : rcp: recipe[descendant: : rcp: ingredient[attribu te: : name='sugar']] The ingredients whose name is sugar: /descendant: : rcp: recipe/descendant: : rcp: ingredient[attribu te: : name='sugar'] The third ingredient: /descendant: : rcp: ingredient[position()=3][position()=1] Empty sequence: /descendant: : rcp: ingredient[position()=1][position()=3] 25

Typical Location Paths l l l The title of each recipe: /descendant: : rcp:

Typical Location Paths l l l The title of each recipe: /descendant: : rcp: recipe/child: : rcp: title The name of each ingredient: /descendant: : rcp: recipe/descendent: : rcp: ingredient /attribute: : name All character data in the collection: /descendant: : rcp: */child: : text() 26

Predicate Examples l l l Test existence of an atribute: [attribute: : amount] Test

Predicate Examples l l l Test existence of an atribute: [attribute: : amount] Test equality of an attribute value: [attribute: : name='flour'] Test inequality of an attribute value : [attribute: : name!='flour'] Test two things at once: [attribute: : amount<0. 5 and attribute: : unit='cup'] Test position of the context node: [position()=2] Probe the subtree below: [descendant: : rcp: ingredient] 27

Abbreviations l l /child: : rcp: collection/child: : rcp: recipe/child: : rcp: ingredient /rcp:

Abbreviations l l /child: : rcp: collection/child: : rcp: recipe/child: : rcp: ingredient /rcp: collection/rcp: recipe/rcp: ingredient /child: : rcp: collection/child: : rcp: recipe/child: : rcp: ingredient/ attribute: : amount /rcp: collection/rcp: recipe/rcp: ingredient/@amount /descendant-or-self: : node()/ // self: : node() . l parent: : node() . . 28

Examples l l l The amounts of flour being used: //rcp: ingredient[@name="flour"]/@amount The ingredients

Examples l l l The amounts of flour being used: //rcp: ingredient[@name="flour"]/@amount The ingredients of which half a cup are used: //rcp: ingredient[@amount='0. 5' and @unit='cup']/@name The second step in preparing stock for Cailles en Sarcophages: //rcp: ingredient[@name="stock"]/rcp: preparation/step[po sition()=2]/text() 29

XPath visualization l l l Using IE 6, it is easy to experiment with

XPath visualization l l l Using IE 6, it is easy to experiment with XPath expressions. The XPath Visualizer provides an interactive XPath evaluator that additionally visualizes the resulting node set. (http: //www. vbxml. com/xpathvisualizer/) This tool is implemented as an ordinary HTML page that makes heavy use of XSLT and Java. Script. 30

More Examples l l l para selects the para element children of the context

More Examples l l l para selects the para element children of the context node * selects all element children of the context node text() selects all text node children of the context node @name selects the name attribute of the context node @* selects all the attributes of the context node para[1] selects the first para child of the context node 31

More Examples (cont. ) l l para[fn: last()] selects the last para child of

More Examples (cont. ) l l para[fn: last()] selects the last para child of the context node */para selects all para grandchildren of the context node /book/chapter[5]/section[2] selects the second section of the fifth chapter of the book whose parent is the document node that contains the context node chapter//para selects the para element descendants of the chapter element children of the context node 32

More Examples (cont. ) l l l //para selects all the para descendants of

More Examples (cont. ) l l l //para selects all the para descendants of the root document node and thus selects all para elements in the same document as the context node //@version selects all the version attribute nodes that are in the same document as the context node //list/member selects all the member elements in the same document as the context node that have a list parent 33

More Examples (cont. ) l l l . //para selects the para element descendants

More Examples (cont. ) l l l . //para selects the para element descendants of the context node. . selects the parent of the context node. . /@lang selects the lang attribute of the parent of the context node para[@type="warning"] selects all para children of the context node that have a type attribute with value warning para[@type="warning"][5] selects the fifth para child of the context node that has a type attribute with value warning 34

More Examples (cont. ) l l l para[5][@type="warning"] selects the fifth para child of

More Examples (cont. ) l l l para[5][@type="warning"] selects the fifth para child of the context node if that child has a type attribute with value warning chapter[title="Introduction"] selects the chapter children of the context node that have one or more title children whose typed value is equal to the string Introduction chapter[title] selects the chapter children of the context node that have one or more title children 35

More Examples (cont. ) l l l employee[@secretary and @assistant] selects all the employee

More Examples (cont. ) l l l employee[@secretary and @assistant] selects all the employee children of the context node that have both a secretary attribute and an assistant attribute book/(chapter|appendix)/section selects every section element that has a parent that is either a chapter or an appendix element, that in turn is a child of a book element that is a child of the context node. If E is any expression that returns a sequence of nodes, then the expression E/. returns the same nodes in document order, with duplicates eliminated based on node identity. 36