the the xpath xpath language language
play

The The XPath XPath Language Language Abbreviations General - PowerPoint PPT Presentation

Objectives Objectives Location steps and paths An Introduction to XML and Web Technologies An Introduction to XML and Web Technologies Typical locations paths The The XPath XPath Language Language Abbreviations General


  1. Objectives Objectives � Location steps and paths An Introduction to XML and Web Technologies An Introduction to XML and Web Technologies � Typical locations paths The The XPath XPath Language Language � Abbreviations � General expressions Anders Møller & Michael I. Schwartzbach  2006 Addison-Wesley An Introduction to XML and Web Technologies 2 XPath Expressions Expressions Location Paths Paths XPath Location � Flexible notation for navigating around trees � A location path evaluates to a sequence of nodes � A basic technology that is widely used � The sequence is sorted in document order • uniqueness and scope in XML Schema � The sequence will never contain duplicates • pattern matching an selection in XSLT • relations in XLink and XPointer • computations on values in XSLT and XQuery An Introduction to XML and Web Technologies 3 An Introduction to XML and Web Technologies 4 1

  2. Locations Steps Evaluating a Location a Location Path Path Locations Steps Evaluating � The location path is a sequence of steps � A step maps a context node into a sequence � A location step consists of � This also maps sequences to sequences • an axis • each node is used as context node • a nodetest • and is replaced with the result of applying the step • some predicates � The path then applies each step in turn axis :: nodetest [ Exp 1 ] [ Exp 2 ] … An Introduction to XML and Web Technologies 5 An Introduction to XML and Web Technologies 6 An Example Example An Example Example An An A A B B B B C C D C C D E F C E E F C E F F F E E F F F F E E F F F Context node An Introduction to XML and Web Technologies 7 An Introduction to XML and Web Technologies 8 2

  3. An Example Example An Example Example An An A A B B B B C C D C C D E F C E E F C E F F F E E F F F F E E F F F descendant::C descendant::C/child::E An Introduction to XML and Web Technologies 9 An Introduction to XML and Web Technologies 10 An Example Example Contexts An Contexts A � The context of an XPath evaluation consists of B B • a context node (a node in an XML tree) • a context position and size (two nonnegative integers) C C D • a set of variable bindings • a function library E F C E • a set of namespace declarations F F F E E F � The application determines the initial context � If the path starts with ‘/’ then F • the initial context node is the root descendant::C/child::E/child::F • the initial position and size are 1 An Introduction to XML and Web Technologies 11 An Introduction to XML and Web Technologies 12 3

  4. Axes Axis Directions Directions Axes Axis � An axis is a sequence of nodes � Each axis has a direction � The axis is evaluated relative to the context node � Forwards means document order: � XPath supports 12 different axes • child, descendant, following-sibling, following, self, descendant-or-self •child •attribute � Backwards means reverse document order: •descendant •following • parent, ancestor, preceding-sibling, •parent •preceding preceding •ancestor •self � Stable but depends on the implementation: •following-sibling •descendant-or-self •preceding-sibling •ancestor-or-self • attribute An Introduction to XML and Web Technologies 13 An Introduction to XML and Web Technologies 14 The parent parent Axis Axis The child child Axis Axis The The parent parent child child An Introduction to XML and Web Technologies 15 An Introduction to XML and Web Technologies 16 4

  5. The descendant descendant Axis Axis The ancestor ancestor Axis Axis The The descendant descendant ancestor ancestor An Introduction to XML and Web Technologies 17 An Introduction to XML and Web Technologies 18 The following sibling Axis Axis The preceding sibling Axis Axis The The following- following following -sibling sibling sibling preceding preceding preceding- -sibling sibling sibling An Introduction to XML and Web Technologies 19 An Introduction to XML and Web Technologies 20 5

  6. The following following Axis Axis The preceding preceding Axis Axis The The following following preceding preceding An Introduction to XML and Web Technologies 21 An Introduction to XML and Web Technologies 22 Node Tests Predicates Node Tests Predicates � text() � General XPath expressions � comment() � Evaluated with the current node as context � processing-instruction() � Result is coerced into a boolean � node() • 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 � QName � *: NCName � NCName :* An Introduction to XML and Web Technologies 23 An Introduction to XML and Web Technologies 24 6

  7. Abbreviations General Expressions Expressions Abbreviations General /child::rcp:collection/child::rcp:recipe � Every expression evaluates to a sequence of /child::rcp:ingredient • atomic values /rcp:collection/rcp:recipe/rcp:ingredient • nodes � Atomic values may be /child::rcp:collection/child::rcp:recipe • numbers /child::rcp:ingredient/attribute::amount • booleans /rcp:collection/rcp:recipe/rcp:ingredient/@amount • Unicode strings • datatypes defined in XML Schema /descendant-or-self::node()/ // � Nodes have identity self::node() . parent::node() .. An Introduction to XML and Web Technologies 25 An Introduction to XML and Web Technologies 26 Atomization Literal Expressions Expressions Atomization Literal � A sequence may be atomized 42 � This results in a sequence of atomic values 3.1415 � For element nodes this is the concatenation of all 6.022E23 descendant text nodes ’XPath is a lot of fun’ � For other nodes this is the obvious string ”XPath is a lot of fun” ’The cat said ”Meow!”’ ”The cat said ””Meow!””” ”XPath is just so much fun” An Introduction to XML and Web Technologies 27 An Introduction to XML and Web Technologies 28 7

  8. Arithmetic Expressions Expressions Variable References Arithmetic Variable References � +, -, *, div, idiv, mod $foo � Operators are generalized to sequences $bar:foo • if any argument is empty, the result is empty • if all argument are singleton sequences of numbers, � $foo-17 refers to the variable ” foo-17 ” the operation is performed � Possible fixes: • otherwise, a runtime error occurs ($foo)-17 , $foo -17 , $foo+-17 An Introduction to XML and Web Technologies 29 An Introduction to XML and Web Technologies 30 Sequence Expressions Expressions Path Expressions Expressions Sequence Path � The ’ , ’ operator concatenates sequences � Locations paths are expressions � Integer ranges are constructed with ’ to ’ � The may start from arbitrary sequences � Operators: union , intersect , except • evaluate the path for each node • use the given node as context node � Sequences are always flattened • context position and size are taken from the sequence � These expression give the same result: • the results are combined in document order (1,(2,3,4),((5)),(),(((6,7),8,9)) 1 to 9 1,2,3,4,5,6,7,8,9 An Introduction to XML and Web Technologies 31 An Introduction to XML and Web Technologies 32 8

  9. Filter Expressions Expressions Value Comparison Comparison Filter Value � Predicates generalized to arbitrary sequences � Operators: eq , ne , lt , le , gt , ge � The expression ’ . ’ is the context item � Used on atomic values � The expression: � When applied to arbitrary values: • atomize (10 to 40)[. mod 5 = 0 and position()>20] • if either argument is empty, the result is empty has the result: • if either has length >1, the result is false • if incomparable, a runtime error 30, 35, 40 • otherwise, compare the two atomic values 8 eq 4+4 (//rcp:ingredient)[1]/@name eq ”beef cube steak” An Introduction to XML and Web Technologies 33 An Introduction to XML and Web Technologies 34 General Comparison Comparison Node Comparison Comparison General Node � Operators: = , != , < , <= , > , >= � Operators: is , << , >> � Used on general values: � Used to compare nodes on identity and order • atomize � When applied to arbitrary values: • if there exists two values, one from each argument, whose • if either argument is empty, the result is empty comparison holds, the result is true • if both are singleton nodes, the nodes are compared • otherwise, the result is false • otherwise, a runtime error 8 = 4+4 (//rcp:recipe)[2] is (1,2) = (2,4) //rcp:recipe[rcp:title eq ”Ricotta Pie”] //rcp:ingredient/@name = ”salt” /rcp:collection << (//rcp:recipe)[4] (//rcp:recipe)[4] >> (//rcp:recipe)[3] An Introduction to XML and Web Technologies 35 An Introduction to XML and Web Technologies 36 9

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend