An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I....
Transcript of An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I....
![Page 1: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/1.jpg)
An Introduction to XML and Web TechnologiesAn Introduction to XML and Web Technologies
The XPath LanguageThe XPath Language
Anders Møller & Michael I. Schwartzbach
2006 Addison-Wesley
![Page 2: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/2.jpg)
MessagesMessages
Exam fixed at January 6, 2010 Thank you for the feedback. ... need more, though.
2An Introduction to XML and Web Technologies
![Page 3: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/3.jpg)
3An Introduction to XML and Web Technologies
ObjectivesObjectives
Location steps and paths Typical locations paths Abbreviations General expressions
![Page 4: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/4.jpg)
Exercise: XPathExercise: XPath
90s
![Page 5: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/5.jpg)
LOCATION PATHSLOCATION PATHS“Let’s get lost”
5An Introduction to XML and Web Technologies
![Page 6: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/6.jpg)
6An Introduction to XML and Web Technologies
XPath ExpressionsXPath Expressions
Flexible notation for navigating around trees A basic technology that is widely used
• uniqueness and scope in XML Schema• pattern matching an selection in XSLT• relations in XLink and XPointer• computations on values in XSLT and XQuery
![Page 7: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/7.jpg)
7An Introduction to XML and Web Technologies
Location PathsLocation Paths
A location path evaluates to a sequence of nodes The sequence is sorted in document order The sequence will never contain duplicates
![Page 8: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/8.jpg)
8An Introduction to XML and Web Technologies
Locations StepsLocations Steps
The location path is a sequence of steps A location step consists of
• an axis• a nodetest• some predicates
axis :: nodetest [Exp1] [Exp2] …
![Page 9: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/9.jpg)
In 45 secondsIn 45 secondsfind axis/node test/predicatesfind axis/node test/predicates
in the expression belowin the expression below
/
child::players/
child::player[attribute::handicap<4]
![Page 10: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/10.jpg)
10An Introduction to XML and Web Technologies
Evaluating a Location PathEvaluating a Location Path
A step maps a context node into a sequence This also maps sequences to sequences
• 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
![Page 11: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/11.jpg)
11An Introduction to XML and Web Technologies
An ExampleAn Example
A
BB
C
F
C
E
F F
D
E
F E
F
E F
C
![Page 12: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/12.jpg)
12An Introduction to XML and Web Technologies
An ExampleAn Example
A
BB
C
F
C
E
F F
D
E
F E
F
E F
C
Context node
![Page 13: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/13.jpg)
13An Introduction to XML and Web Technologies
An ExampleAn Example
A
BB
C
F
C
E
F F
D
E
F E
F
E F
C
descendant::C
![Page 14: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/14.jpg)
14An Introduction to XML and Web Technologies
An ExampleAn Example
A
BB
C
F
C
E
F F
D
E
F E
F
E F
C
descendant::C/child::E
![Page 15: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/15.jpg)
15An Introduction to XML and Web Technologies
An ExampleAn Example
A
BB
C
F
C
E
F F
D
E
F E
F
E F
C
descendant::C/child::E/child::F
![Page 16: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/16.jpg)
Exercise: Location Path EvaluationExercise: Location Path Evaluation
90s
![Page 17: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/17.jpg)
17An Introduction to XML and Web Technologies
ContextsContexts
The context of an XPath evaluation consists of• 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
• the initial context node is the root• the initial position and size are 1
![Page 18: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/18.jpg)
18An Introduction to XML and Web Technologies
AxesAxes
An axis is a sequence of nodes The axis is evaluated relative to the context node XPath supports 12 different axes
•attribute•following•preceding•self•descendant-or-self•ancestor-or-self
•child•descendant•parent•ancestor•following-sibling•preceding-sibling
![Page 19: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/19.jpg)
19An Introduction to XML and Web Technologies
Axis DirectionsAxis Directions
Each axis has a direction Forwards means document order:
• child, descendant, following-sibling, following, self, descendant-or-self
Backwards means reverse document order:• parent, ancestor, preceding-sibling, preceding
Stable but depends on the implementation:• attribute
![Page 20: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/20.jpg)
20An Introduction to XML and Web Technologies
The The parentparent Axis Axis
![Page 21: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/21.jpg)
21An Introduction to XML and Web Technologies
The The parentparent Axis Axis
![Page 22: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/22.jpg)
22An Introduction to XML and Web Technologies
The The childchild Axis Axis
![Page 23: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/23.jpg)
23An Introduction to XML and Web Technologies
The The childchild Axis Axis
![Page 24: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/24.jpg)
24An Introduction to XML and Web Technologies
The The descendantdescendant Axis Axis
![Page 25: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/25.jpg)
25An Introduction to XML and Web Technologies
The The descendantdescendant Axis Axis
![Page 26: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/26.jpg)
26An Introduction to XML and Web Technologies
The The ancestorancestor Axis Axis
![Page 27: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/27.jpg)
27An Introduction to XML and Web Technologies
The The ancestorancestor Axis Axis
![Page 28: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/28.jpg)
28An Introduction to XML and Web Technologies
The The following-siblingfollowing-sibling Axis Axis
![Page 29: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/29.jpg)
29An Introduction to XML and Web Technologies
The The following-siblingfollowing-sibling Axis Axis
![Page 30: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/30.jpg)
30An Introduction to XML and Web Technologies
The The preceding-siblingpreceding-sibling Axis Axis
![Page 31: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/31.jpg)
31An Introduction to XML and Web Technologies
The The preceding-siblingpreceding-sibling Axis Axis
![Page 32: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/32.jpg)
32An Introduction to XML and Web Technologies
The The followingfollowing Axis Axis
![Page 33: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/33.jpg)
33An Introduction to XML and Web Technologies
The The followingfollowing Axis Axis
![Page 34: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/34.jpg)
34An Introduction to XML and Web Technologies
The The precedingpreceding Axis Axis
![Page 35: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/35.jpg)
35An Introduction to XML and Web Technologies
The The precedingpreceding Axis Axis
![Page 36: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/36.jpg)
Exercise: Axis in XMLExercise: Axis in XML
90s
![Page 37: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/37.jpg)
37An Introduction to XML and Web Technologies
Node TestsNode Tests
text() comment() processing-instruction() node() * QName *:NCName NCName:*
![Page 38: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/38.jpg)
38An Introduction to XML and Web Technologies
PredicatesPredicates
General XPath expressions Evaluated with the current node as context Result is coerced into a boolean
• 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
![Page 39: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/39.jpg)
Exercise: Node tests and PredicatesExercise: Node tests and Predicates
120s
![Page 40: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/40.jpg)
40An Introduction to XML and Web Technologies
AbbreviationsAbbreviations
/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() . parent::node() ..
![Page 41: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/41.jpg)
Exercise: AbbreviationsExercise: Abbreviations
90s
![Page 42: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/42.jpg)
GENERAL EXPRESSIONSGENERAL EXPRESSIONS“Computare!”
42An Introduction to XML and Web Technologies
![Page 43: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/43.jpg)
43An Introduction to XML and Web Technologies
General ExpressionsGeneral Expressions
Every expression evaluates to a sequence of• atomic values• nodes
Atomic values may be• numbers• booleans• Unicode strings• datatypes defined in XML Schema
Nodes have identity
![Page 44: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/44.jpg)
44An Introduction to XML and Web Technologies
AtomizationAtomization
A sequence may be atomized This results in a sequence of atomic values For element nodes this is the concatenation of all
descendant text nodes For other nodes this is the obvious string
![Page 45: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/45.jpg)
45An Introduction to XML and Web Technologies
Literal ExpressionsLiteral Expressions
423.14156.022E23’XPath is a lot of fun’”XPath is a lot of fun”’The cat said ”Meow!”’”The cat said ””Meow!””””XPath is just so much fun”
![Page 46: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/46.jpg)
46An Introduction to XML and Web Technologies
Arithmetic ExpressionsArithmetic Expressions
+, -, *, div, idiv, mod Operators are generalized to sequences
• if any argument is empty, the result is empty• if all argument are singleton sequences of numbers, the
operation is performed• otherwise, a runtime error occurs
![Page 47: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/47.jpg)
Write an XPath expression computingWrite an XPath expression computing6*8-7+16*8-7+1
30s
![Page 48: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/48.jpg)
48An Introduction to XML and Web Technologies
Variable ReferencesVariable References
$foo$bar:foo
$foo-17 refers to the variable ”foo-17” Possible fixes:
($foo)-17, $foo -17, $foo+-17
![Page 49: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/49.jpg)
49An Introduction to XML and Web Technologies
Sequence ExpressionsSequence Expressions
The ’,’ operator concatenates sequences Integer ranges are constructed with ’to’ Operators: union, intersect, except Sequences are always flattened These expression give the same result:
(1,(2,3,4),((5)),(),(((6,7),8,9))1 to 91,2,3,4,5,6,7,8,9
![Page 50: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/50.jpg)
50An Introduction to XML and Web Technologies
Path ExpressionsPath Expressions
Locations paths are expressions The may start from arbitrary sequences
• evaluate the path for each node• use the given node as context node• context position and size are taken from the sequence• the results are combined in document order
![Page 51: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/51.jpg)
51An Introduction to XML and Web Technologies
Filter ExpressionsFilter Expressions
Predicates generalized to arbitrary sequences The expression ’.’ is the context item The expression:
(10 to 40)[. mod 5 = 0 and position()>20]
has the result:
30, 35, 40
![Page 52: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/52.jpg)
COMPARISONSCOMPARISONS“Should I compare thee ...”
52An Introduction to XML and Web Technologies
![Page 53: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/53.jpg)
53An Introduction to XML and Web Technologies
Value ComparisonValue Comparison
Operators: eq, ne, lt, le, gt, ge Used on atomic values When applied to arbitrary values:
• atomize• if either argument is empty, the result is empty• if either has length >1, the result is false• if incomparable, a runtime error• otherwise, compare the two atomic values
8 eq 4+4(//rcp:ingredient)[1]/@name eq ”beef cube steak”
![Page 54: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/54.jpg)
54An Introduction to XML and Web Technologies
General ComparisonGeneral Comparison
Operators: =, !=, <, <=, >, >= Used on general values:
• atomize• if there exists two values, one from each argument, whose
comparison holds, the result is true• otherwise, the result is false
8 = 4+4(1,2) = (2,4)//rcp:ingredient/@name = ”salt”
![Page 55: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/55.jpg)
55An Introduction to XML and Web Technologies
Node ComparisonNode Comparison
Operators: is, <<, >> Used to compare nodes on identity and order When applied to arbitrary values:
• if either argument is empty, the result is empty• if both are singleton nodes, the nodes are compared• otherwise, a runtime error
(//rcp:recipe)[2] is //rcp:recipe[rcp:title eq ”Ricotta Pie”]/rcp:collection << (//rcp:recipe)[4](//rcp:recipe)[4] >> (//rcp:recipe)[3]
![Page 56: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/56.jpg)
56An Introduction to XML and Web Technologies
Be Careful About ComparisonsBe Careful About Comparisons
((//rcp:ingredient)[40]/@name,(//rcp:ingredient)[40]/@amount) eq((//rcp:ingredient)[53]/@name, (//rcp:ingredient)[53]/@amount)
Yields false, since the arguments are not singletons
((//rcp:ingredient)[40]/@name, (//rcp:ingredient)[40]/@amount) =((//rcp:ingredient)[53]/@name, (//rcp:ingredient)[53]/@amount
Yields true, since the two names are found to be equal
((//rcp:ingredient)[40]/@name, (//rcp:ingredient)[40]/@amount) is((//rcp:ingredient)[53]/@name, (//rcp:ingredient)[53]/@amount)
Yields a runtime error, since the arguments are not singletons
![Page 57: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/57.jpg)
57An Introduction to XML and Web Technologies
Algebraic Axioms for ComparisonsAlgebraic Axioms for Comparisons
xx
xyyx
zxzyyxzxzyyx
•Reflexivity:
•Symmetry:
•Transitivity:
•Anti-symmetry:
•Negation:
yxxyyx
yxyx
![Page 58: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/58.jpg)
58An Introduction to XML and Web Technologies
XPath Violates Most AxiomsXPath Violates Most Axioms
Reflexivity?
()=() ?
Transitivity? (1,2)=(2,3), (2,3)=(3,4); (1,2)=(3,4)?
Anti-symmetry?
(1,4)<=(2,3), (2,3)<=(1,4); (1,2)=(3,4)?
Negation?
(1)!=() ?, (1)=() ?
![Page 59: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/59.jpg)
59An Introduction to XML and Web Technologies
Boolean ExpressionsBoolean Expressions
Operators: and, or Arguments are coerced, false if the value is:
• the boolean false• the empty sequence• the empty string• the number zero
Constants use functions true() and false() Negation uses not(…)
![Page 60: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/60.jpg)
FUNCTIONSFUNCTIONS“Whatever you want, baby”
60An Introduction to XML and Web Technologies
![Page 61: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/61.jpg)
61An Introduction to XML and Web Technologies
FunctionsFunctions
XPath has an extensive function library Default namespace for functions:http://www.w3.org/2006/xpath-functions
106 functions are required More functions with the namespace:
http://www.w3.org/2001/XMLSchema
![Page 62: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/62.jpg)
62An Introduction to XML and Web Technologies
Function InvocationFunction Invocation
Calling a function with 4 arguments:
fn:avg(1,2,3,4)
Calling a function with 1 argument:
fn:avg((1,2,3,4))
![Page 63: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/63.jpg)
63An Introduction to XML and Web Technologies
Arithmetic FunctionsArithmetic Functions
fn:abs(-23.4) = 23.4fn:ceiling(23.4) = 24fn:floor(23.4) = 23fn:round(23.4) = 23fn:round(23.5) = 24
![Page 64: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/64.jpg)
64An Introduction to XML and Web Technologies
Boolean FunctionsBoolean Functions
fn:not(0) = fn:true()fn:not(fn:true()) = fn:false()fn:not("") = fn:true()fn:not((1)) = fn:false()
![Page 65: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/65.jpg)
65An Introduction to XML and Web Technologies
String FunctionsString Functions
fn:concat("X","ML") = "XML"fn:concat("X","ML"," ","book") = "XML book"fn:string-join(("XML","book")," ") = "XML book"fn:string-join(("1","2","3"),"+") = "1+2+3"fn:substring("XML book",5) = "book"fn:substring("XML book",2,4) = "ML b"fn:string-length("XML book") = 8fn:upper-case("XML book") = "XML BOOK"fn:lower-case("XML book") = "xml book"
![Page 66: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/66.jpg)
66An Introduction to XML and Web Technologies
Regexp FunctionsRegexp Functions
fn:contains("XML book","XML") = fn:true()fn:matches("XML book","XM..[a-z]*") = fn:true()fn:matches("XML book",".*Z.*") = fn:false()fn:replace("XML book","XML","Web") = "Web book"fn:replace("XML book","[a-z]","8") = "XML 8888"
![Page 67: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/67.jpg)
67An Introduction to XML and Web Technologies
Cardinality FunctionsCardinality Functions
fn:exists(()) = fn:false()fn:exists((1,2,3,4)) = fn:true()fn:empty(()) = fn:true()fn:empty((1,2,3,4)) = fn:false()fn:count((1,2,3,4)) = 4fn:count(//rcp:recipe) = 5
![Page 68: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/68.jpg)
68An Introduction to XML and Web Technologies
Sequence FunctionsSequence Functions
fn:distinct-values((1, 2, 3, 4, 3, 2)) = (1, 2, 3, 4)fn:insert-before((2, 4, 6, 8), 2, (3, 5)) = (2, 3, 5, 4, 6, 8)fn:remove((2, 4, 6, 8), 3) = (2, 4, 8)fn:reverse((2, 4, 6, 8)) = (8, 6, 4, 2)fn:subsequence((2, 4, 6, 8, 10), 2) = (4, 6, 8, 10)fn:subsequence((2, 4, 6, 8, 10), 2, 3) = (4, 6, 8)
![Page 69: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/69.jpg)
69An Introduction to XML and Web Technologies
Aggregate FunctionsAggregate Functions
fn:avg((2, 3, 4, 5, 6, 7)) = 4.5fn:max((2, 3, 4, 5, 6, 7)) = 7fn:min((2, 3, 4, 5, 6, 7)) = 2fn:sum((2, 3, 4, 5, 6, 7)) = 27
![Page 70: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/70.jpg)
70An Introduction to XML and Web Technologies
Node FunctionsNode Functions
fn:doc("http://www.brics.dk/ixwt/recipes/recipes.xml")
fn:position()fn:last()
![Page 71: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/71.jpg)
71An Introduction to XML and Web Technologies
Coercion FunctionsCoercion Functions
xs:integer("5") = 5xs:integer(7.0) = 7xs:decimal(5) = 5.0xs:decimal("4.3") = 4.3xs:decimal("4") = 4.0xs:double(2) = 2.0E0xs:double(14.3) = 1.43E1xs:boolean(0) = fn:false()xs:boolean("true") = fn:true()xs:string(17) = "17"xs:string(1.43E1) = "14.3"xs:string(fn:true()) = "true"
![Page 72: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/72.jpg)
CONTROL FLOW CONTROL FLOW CONSTRUCTSCONSTRUCTS
“Should I stay or should I go?”
72An Introduction to XML and Web Technologies
![Page 73: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/73.jpg)
73An Introduction to XML and Web Technologies
For ExpressionsFor Expressions
The expressionfor $r in //rcp:recipe
return fn:count($r//rcp:ingredient[fn:not(rcp:ingredient)])
returns the value11, 12, 15, 8, 30
The expressionfor $i in (1 to 5) for $j in (1 to $i) return $j
returns the value1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5
![Page 74: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/74.jpg)
74An Introduction to XML and Web Technologies
Conditional ExpressionsConditional Expressions
fn:avg( for $r in //rcp:ingredient return if ( $r/@unit = "cup" ) then xs:double($r/@amount) * 237 else if ( $r/@unit = "teaspoon" ) then xs:double($r/@amount) * 5 else if ( $r/@unit = "tablespoon" ) then xs:double($r/@amount) * 15 else ())
![Page 75: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/75.jpg)
75An Introduction to XML and Web Technologies
Quantified ExpressionsQuantified Expressions
some $r in //rcp:ingredient satisfies $r/@name eq "sugar"
fn:exists( for $r in //rcp:ingredient return if ($r/@name eq "sugar") then fn:true() else ())
![Page 76: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/76.jpg)
Write an XPath expression whichWrite an XPath expression whichsums the numbers from 1 to 1001sums the numbers from 1 to 1001
45 seconds
Hint: “1 to 1001”, “fn:sum”
![Page 77: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/77.jpg)
Write an XPath expression whichWrite an XPath expression whichsums the sums the oddodd numbers from 1 to 1001 numbers from 1 to 1001
60 seconds
Hint: (1,2,3,4)[. mod 2 = 0] is (2, 4)
![Page 78: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/78.jpg)
XSLT 1.0 VS. 2.0XSLT 1.0 VS. 2.0“No country for old men”
78An Introduction to XML and Web Technologies
![Page 79: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/79.jpg)
79An Introduction to XML and Web Technologies
XPath 1.0 RestrictionsXPath 1.0 Restrictions
Many implementations only support XPath 1.0 Smaller function library Implicit casts of values Some expressions change semantics:
”4” < ”4.0”is false in XPath 1.0 but true in XPath 2.0
![Page 80: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/80.jpg)
APPLICATIONSAPPLICATIONS“Thank you for your interest. We regret to inform you ...”
80An Introduction to XML and Web Technologies
![Page 81: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/81.jpg)
81An Introduction to XML and Web Technologies
XPointerXPointer
A fragment identifier mechanism based on XPath Different ways of pointer to the fourth recipe:
...#xpointer(//recipe[4])
...#xpointer(//rcp:recipe[./rcp:title ='Zuppa Inglese'])
...#element(/1/5)
...#r102
![Page 82: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/82.jpg)
82An Introduction to XML and Web Technologies
XLinkXLink
Generalizing hyperlinks from HTML to XML Allow many-to-many relations Allow third party links Allow arbitrary element names
Not widely used…
![Page 83: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/83.jpg)
83An Introduction to XML and Web Technologies
An XLink LinkAn XLink Link
<mylink xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="extended"> <myresource xlink:type="locator" xlink:href="students.xml#Carl" xlink:label="student"/> <myresource xlink:type="locator" xlink:href="students.xml#Fred" xlink:label="student"/> <myresource xlink:type="locator" xlink:href="teachers.xml#Joe" xlink:label="teacher"/> <myarc xlink:type="arc" xlink:from="student" xlink:to="teacher"/></mylink>
![Page 84: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/84.jpg)
84An Introduction to XML and Web Technologies
A Picture of the XLinkA Picture of the XLink
Carl
Fred Joe
mylink
myresource
myresource
myresource
myarc
myarc
![Page 85: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/85.jpg)
85An Introduction to XML and Web Technologies
Simple LinksSimple Links
<mylink xlink:type="simple" xlink:href="..." xlink:show="..." .../>
<mylink xlink:type="extended"> <myresource xlink:type="resource" xlink:label="local"/> <myresource xlink:type="locator" xlink:label="remote" xlink:href="..."/> <myarc xlink:type="arc" xlink:from="local" xlink:to="remote"
xlink:show="..." .../></mylink>
![Page 86: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/86.jpg)
86An Introduction to XML and Web Technologies
HTML Links in XLinkHTML Links in XLink
<a xlink:type="simple" xlink:href="..." xlink:show="replace" xlink:actuate="onRequest"/>
![Page 87: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/87.jpg)
87An Introduction to XML and Web Technologies
The HLink AlternativeThe HLink Alternative
<hlink namespace="http://www.w3.org/1999/xhtml" element="a" locator="@href" effect="replace" actuate="onRequest" replacement="@target"/>
![Page 88: An Introduction to XML and Web Technologies The XPath Language Anders Møller & Michael I. Schwartzbach 2006 Addison-Wesley.](https://reader036.fdocuments.net/reader036/viewer/2022062518/5697bf7a1a28abf838c82fdb/html5/thumbnails/88.jpg)
88An Introduction to XML and Web Technologies
Essential Online ResourcesEssential Online Resources
http://www.w3.org/TR/xpath/ http://www.w3.org/TR/xpath20/ http://www.w3.org/TR/xlink/ http://www.w3.org/TR/xptr-framework/