1 / 216

XPath 2.0 w3/TR/xpath20/ w3/TR/xquery-operators/

XPath 2.0 http://www.w3.org/TR/xpath20/ http://www.w3.org/TR/xquery-operators/. Roger L. Costello 6 March 2010. Set this to XPath 2.0. Using Namespaces in Oxygen.

Télécharger la présentation

XPath 2.0 w3/TR/xpath20/ w3/TR/xquery-operators/

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. XPath 2.0http://www.w3.org/TR/xpath20/http://www.w3.org/TR/xquery-operators/ Roger L. Costello 6 March 2010

  2. Set this to XPath 2.0

  3. Using Namespaces in Oxygen • Suppose in the Oxygen XPath expression evaluator tool you would like to write expressions such as this:current-dateTime() - xs:dateTime('2008-01-14T00:00:00') • How do you tell Oxygen what namespace the "xs" prefix maps to? Here's how: • Go to:Options ► Preferences ► XML ► XSLT-FO-XQuery ► XPathand in the Default prefix-namespace mappings table add a new entry mapping xs to the XML Schema namespace http://www.w3.org/2001/XMLSchema

  4. XML Document <?xml version="1.0" encoding="UTF-8"?> <planets> <planet> <name>Mercury</name> <mass units="(Earth = 1)">.0553</mass> <day units="days">58.65</day> <radius units="miles">1516</radius> <density units="(Earth = 1)">.983</density> <distance units="millions miles">43.4</distance> </planet> <planet> <name>Venus</name> <mass units="(Earth = 1)">.815</mass> <day units="days">116.75</day> <radius units="miles">3716</radius> <density units="(Earth = 1)">.943</density> <distance units="millions miles">66.8</distance> </planet> <planet> <name>Earth</name> <mass units="(Earth = 1)">1</mass> <day units="days">1</day> <radius units="miles">2107</radius> <density units="(Earth = 1)">1</density> <distance units="millions miles">128.4</distance> </planet> </planets> We will use this XML document throughout this tutorial, so spend a minute or two familiarizing yourself with it. It is planets.xml in the example01 folder. Please load it into Oxygen XML. planets.xml

  5. Sequences • Sequences are central to XPath 2.0 • XPath 2.0 operates on sequences, and generates sequences. • A sequence is an ordered collection of nodes and/or atomic values.

  6. Example Sequences • This sequence is composed of three atomic values:(1, 2, 3) • This sequence is also composed of three atomic values:('red', 'white', 'blue') • This XPath expression will generate a sequence composed of three <name> nodes:(//planet/name) See example01 http://www.w3.org/TR/xpath20/#id-sequence-expressions

  7. More Sequence Examples • With the following XPath, a sequence of six nodes are generated; the first three are <mass> nodes, the next three are <name> nodes:(//planet/mass, //planet/name) • This sequence contains node values followed by atomic values:(//planet/name, 1, 2, 3) See example02

  8. Definition of Sequence • A sequence is an ordered collection of zero or more items. • An item is either an atomic value or a node. • An atomic value is a single, non-variable piece of data, e.g. 10, true, 2007, "hello world". (An atomic value is an XML Schema simpleType value) • There are seven kinds of nodes: • element, text, attribute, document, PI, comment, namespace • A sequence containing exactly one item is called a singleton sequence. • A sequence containing zero items is called an empty sequence. http://www.w3.org/TR/xpath20/#dt-item

  9. Sequence Constructor • A sequence is constructed by enclosing an expression in parentheses. • Each item is separated by a comma. • The comma is called the sequence constructor operator.

  10. No Nested Sequences • If you have a sequence (1, 2) and nest it in another sequence ((1, 2), 3)the resulting sequence is flattened to simply(1, 2, 3) • A nested empty sequence is removed(1, (2, 3), (), 4, 5, 6)the resulting sequence is flattened to simply:(1, 2, 3, 4, 5, 6) See example03

  11. Extract Items from a Sequence • You can extract items from a sequence using the […] operator (predicate):(4, 5, 6)[2]returns the singleton sequence:(5) • This XPath expression://planet[2]returns the second planet See example04

  12. The index must be an integer • The predicate value must be an integer (more specifically, it must be an XML Schema integer datatype). (sequence)[index] The index must be an integer

  13. Initializing • Example: suppose an element may or may not have an attribute, discount. If the element has the discount attribute then return its value; otherwise, return 0. (@discount, 0)[1]

  14. Context Item • Dot "." stands for the current context item. • The context item can be a node, e.g. //planet[.]or it can be an atomic value, e.g. (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)[. mod 2 = 0] See example05

  15. count(sequence) • This function returns an integer, representing the number of items in the sequence. See example03.b http://www.w3.org/TR/xquery-operators/#func-count

  16. Why Nested Parentheses? Compare these two: count((1, 2, 3)) count(1, 2, 3) Notice the nested parentheses Why is this one correct and the other one incorrect?

  17. Answer • The count function has only one argument. • This form: count(1, 2, 3)provides three arguments to count, which is incorrect. • This form: count((1, 2, 3))provides one argument to count (the argument is a sequence with three items).

  18. Sequence of Sequences? • There is no such thing as a sequence of sequences! • There's only one sequence; all subsequences get flattened into a single sequence. count((//planet, (1, 2, 3), ('red', 'white', 'blue'))) sequence of sequences?

  19. The value of a non-existent node is the empty sequence, () /Planets/Planet[999] There is no 999th Planet, so the result of evaluating this XPath expression is the empty sequence, denoted by ()

  20. () is not equal to '' • An empty sequence is not equal to a string of length zero. ('a', 'b', (), 'c') is not equal to ('a', 'b', '', 'c') count = 3 count = 4 See example03.a

  21. This predicate [.] eliminates empty strings The value of ('a', '')[.] is just ('a') The value of ('a', 'b', '', 'c')[.] is just ('a', 'b', 'c')

  22. Two built-in functions true() false() http://www.w3.org/TR/xquery-operators/#func-false http://www.w3.org/TR/xquery-operators/#func-true

  23. index-of(sequence, value) • The index-of() function allows you to obtain the position of value in sequence. value sequence index-of((1,3,5,7,9,11), 7) Output: (4) 7 is at the 4th index position. http://www.w3.org/TR/xquery-operators/#func-index-of

  24. Suppose the value occurs at multiple locations in the sequence • index-of returns a sequence of index locations. In the last example the result was a sequence of length 1. multiple 7's in the sequence index-of((1,3,5,7,9,11,7,7), 7) Output: (4, 7, 8) See example05.1

  25. remove(sequence, position) • The remove function enables you to remove a value at a specified position from a sequence. position sequence remove((1,3,5,7,9,11), 4) remove this Output: (1, 3, 5, 9, 11) See example05.2 http://www.w3.org/TR/xquery-operators/#func-remove

  26. The "to" Range Operator • The range operator–to–can be used to generate a sequence of consecutive integers:(1 to 10)returns the sequence:(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) • This expression:(1 to 100)[(. mod 10) = 0]returns the sequence:(10, 20, 30, 40, 50, 60, 70, 80, 90, 100) • This expression:(1, 2, 10 to 14, 34, 99)returns this disjointed sequence:(1, 2, 10, 11, 12, 13, 14, 34, 99) See example06

  27. The operands of "to" must be integers This is not valid: ('a' to 'z') Error message you will get: "Error: Required type of first operand of 'to' is integer; supplied value has type string"

  28. insert-before(sequence, position,value) sequence (note: '2' is missing) position value insert-before((1,3,4,5,6,7,8,9),2,2 insert the value 2 before position 2 Output: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) http://www.w3.org/TR/xquery-operators/#func-insert-before

  29. Appending a value to the end Specify a position greater than the length of the sequence insert-before(1 to 10, count(1 to 10) + 1, 2) Output: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2)

  30. The inserted value can be a sequence sequence of values insert-before((1,3,4,5,6,7,8,9),2,(2,3)) Output: (1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10) See example05.3

  31. Sequence Functions • index-of() returns the index (position) of a value • [idx] returns the value at idx • remove() returns the sequence minus the item whose index (position) is specified • insert-before() returns the sequence plus a new value Do Lab8

  32. Sequences are Ordered • Order matters. • This generates a sequence composed of the <mass> elements followed by the <name> elements:(//planet/mass, //planet/name) See example07

  33. reverse(sequence) • This function reverses the items in sequence. Notice in the first example the items are wrapped in parentheses (thus creating a sequence). See example07.1 http://www.w3.org/TR/xquery-operators/#func-reverse

  34. The for Expression • Use the for expression to loop (iterate) over all items in a sequence. This is its general form:for variable in sequence return expression • Here's an example which iterates over the integers 1-10, multiplying each integer by two:for $i in (1 to 10) return $i * 2returns(2, 4, 6, 8, 10, 12, 14, 16, 18, 20) See example08 http://www.w3.org/TR/xpath20/#id-for-expressions

  35. for Expression Examples • This iterates over each <planet> element, and returns its <radius> element:for $p in /planets/planet return $p/radius • This iterates over each <radius> element, and returns itself (the sequence generated is identical to above):for $r in /planets/planet/radius return $r • This iterates over each letter of the alphabet:for $i in ('a','b','c','d','e','f','g','h','i','j','k','l', 'm','n','o','p','q','r','s','t','u','v','w','x','y','z') return $i See example09

  36. More for Examples • This returns the radius converted to kilometers (it returns numbers, not nodes):for $r in /planets/planet/radius return $r * 1.61 • This applies the avg() function to the sequence of nodes returned by the for expression:avg(for $r in /planets/planet/radius return $r) See example10

  37. Terminology for variable in sequence return expression range variable return expression input sequence The return expression is evaluated once for each item in the input sequence.

  38. Multiple Variables Multiple variables can be used: , returnexpression for variableinsequence

  39. Example of Multiple Variables for $x in (1, 2), $y in (3, 4) return ($x * $y) returns (3, 4, 6, 8) Do Lab9 See example11

  40. The if Expression • The form of the if expression is:if (boolean expression) thenexpression1elseexpression2 • If the boolean expression evaluates to true then the result is expression1, else the result is expression2 • This if expression finds the minimum of two numbers:if (10 &lt; 20) then 10 else 20 • This for loop returns all the positive numbers in the sequence:for $i in (0, -3, 5, 7, -1, 2) return if ($i &gt; 0) then $i else () See example12 http://www.w3.org/TR/xpath20/#id-conditionals

  41. Nested if-then-else if (boolean expr) then expr1 else expr2 These can be an if-then-else

  42. Notes about the if Expression • You must wrap the boolean expression in parentheses. • You must have an "else" part. There is no if-then expression, only an if-then-else Do Lab10

  43. The some Expression • The form of the some expression is:some variable in sequence satisfies boolean expression • The result of the expression is either true or false. • Using the some expression means that at least one item in the sequence satisfies the boolean expression. http://www.w3.org/TR/xpath20/#id-quantified-expressions

  44. Examples of the some Expression • This example determines if there are some (one or more) negative values in the sequence:some $i in (2, 6, -1, 3, 9) satisfies $i &lt; 0 • Note that this produces the same boolean result:(2, 6, -1, 3, 9) &lt; 0 because "<" is a general comparison operator, i.e. it compares each item in the sequence until a match is found. See example13

  45. More Examples of "some" • Is there is some planet that has a radius greater than 2000?some $i in /planets/planet satisfies $i/radius &gt; 2000 • Note that this produces the same boolean result:/planets/planet/radius &gt; 2000 See example14

  46. The every Expression • The form of the every expression is:every variable in sequence satisfies boolean expression • The result of the expression is either true or false. • Using the every expression means that every item in the sequence satisfies the boolean expression. http://www.w3.org/TR/xpath20/#id-quantified-expressions

  47. Examples of the every Expression • This example determines if every item in the sequence is positive:every $i in (2, 6, -1, 3, 9) satisfies $i &gt; 0 • Note that this produces the same boolean result:not((2, 6, -1, 3, 9) &lt;= 0)

  48. Multiple Universal Quantifiers • An XPath expression can have multiple universal quantifiers. , satisfiescondition every variableinsequence See example15

  49. Union Operator • The union operator is used to combine two node sequences (cannot union atomic sequences). • Example: /planets/planet/mass union /planets/planet/radiusproduces the sequence: <mass units="(Earth = 1)">.0553</mass> <radius units="miles">1516</radius> <mass units="(Earth = 1)">.815</mass> <radius units="miles">3716</radius> <mass units="(Earth = 1)">1</mass> <radius units="miles">2107</radius> http://www.w3.org/TR/xpath20/#combining_seq

  50. Equivalent /planets/planet/mass union /planets/planet/radius /planets/planet/mass | /planets/planet/radius The union and | operators are equivalent.

More Related