Professional Documents
Culture Documents
The Xpath Language
The Xpath Language
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
Locations Steps Evaluating a Location Path
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
An Introduction to XML and Web Technologies 5 An Introduction to XML and Web Technologies 6
An Example An Example
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
An Example An Example
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 Contexts
A
The context of an XPath evaluation consists of
B B • a context node (a node in an XML tree)
C C D • a context position and size (two nonnegative integers)
• a set of variable bindings
E F C E • a function library
• a set of namespace declarations
F F F E E F
The application determines the initial context
F If the path starts with ‘/’ then
• 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
Axes Axis Directions
An Introduction to XML and Web Technologies 13 An Introduction to XML and Web Technologies 14
An Introduction to XML and Web Technologies 15 An Introduction to XML and Web Technologies 16
4
The descendant Axis The ancestor Axis
An Introduction to XML and Web Technologies 17 An Introduction to XML and Web Technologies 18
The following-
following-sibling Axis The preceding-
preceding-sibling Axis
An Introduction to XML and Web Technologies 19 An Introduction to XML and Web Technologies 20
5
The following Axis The preceding Axis
An Introduction to XML and Web Technologies 21 An Introduction to XML and Web Technologies 22
An Introduction to XML and Web Technologies 23 An Introduction to XML and Web Technologies 24
6
Abbreviations General Expressions
/child::rcp:collection/child::rcp:recipe
/child::rcp:ingredient Every expression evaluates to a sequence of
• atomic values
/rcp:collection/rcp:recipe/rcp:ingredient
• nodes
/child::rcp:collection/child::rcp:recipe Atomic values may be
/child::rcp:ingredient/attribute::amount • numbers
• booleans
/rcp:collection/rcp:recipe/rcp:ingredient/@amount
• Unicode strings
/descendant-or-self::node()/ // • datatypes defined in XML Schema
Nodes have identity
self::node() . parent::node() ..
An Introduction to XML and Web Technologies 25 An Introduction to XML and Web Technologies 26
7
Arithmetic Expressions Variable References
An Introduction to XML and Web Technologies 29 An Introduction to XML and Web Technologies 30
An Introduction to XML and Web Technologies 31 An Introduction to XML and Web Technologies 32
8
Filter Expressions Value Comparison
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:
(10 to 40)[. mod 5 = 0 and position()>20] • atomize
• if either argument is empty, the result is empty
has the result: • if either has length >1, the result is false
30, 35, 40 • if incomparable, a runtime error
• 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
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
Be Careful About Comparisons Algebraic Axioms for Comparisons
Yields false, since the arguments are not singletons •Symmetry: x= y⇒ y=x
((//rcp:ingredient)[40]/@name, (//rcp:ingredient)[40]/@amount) =
((//rcp:ingredient)[53]/@name, (//rcp:ingredient)[53]/@amount
•Transitivity: x= y∧ y = z⇒ x= z
x< y∧ y< z⇒ x< z
Yields true, since the two names are found to be equal
((//rcp:ingredient)[40]/@name, (//rcp:ingredient)[40]/@amount) is
•Anti-symmetry: x≤ y∧ y≤ x⇒ x= y
((//rcp:ingredient)[53]/@name, (//rcp:ingredient)[53]/@amount)
An Introduction to XML and Web Technologies 37 An Introduction to XML and Web Technologies 38
An Introduction to XML and Web Technologies 39 An Introduction to XML and Web Technologies 40
10
Functions Function Invocation
An Introduction to XML and Web Technologies 41 An Introduction to XML and Web Technologies 42
An Introduction to XML and Web Technologies 43 An Introduction to XML and Web Technologies 44
11
String Functions Regexp Functions
An Introduction to XML and Web Technologies 45 An Introduction to XML and Web Technologies 46
An Introduction to XML and Web Technologies 47 An Introduction to XML and Web Technologies 48
12
Aggregate Functions Node Functions
An Introduction to XML and Web Technologies 49 An Introduction to XML and Web Technologies 50
xs:integer("5") = 5
The expression
xs:integer(7.0) = 7
xs:decimal(5) = 5.0 for $r in //rcp:recipe
return fn:count($r//rcp:ingredient[fn:not(rcp:ingredient)])
xs:decimal("4.3") = 4.3
xs:decimal("4") = 4.0 returns the value
xs:double(2) = 2.0E0
11, 12, 15, 8, 30
xs:double(14.3) = 1.43E1
xs:boolean(0) = fn:false() The expression
xs:boolean("true") = fn:true() for $i in (1 to 5)
xs:string(17) = "17" for $j in (1 to $i)
xs:string(1.43E1) = "14.3" return $j
xs:string(fn:true()) = "true" returns the value
1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5
An Introduction to XML and Web Technologies 51 An Introduction to XML and Web Technologies 52
13
Conditional Expressions Quantified Expressions
An Introduction to XML and Web Technologies 53 An Introduction to XML and Web Technologies 54
Many implementations only support XPath 1.0 A fragment identifier mechanism based on XPath
Smaller function library Different ways of pointer to the fourth recipe:
Implicit casts of values
...#xpointer(//recipe[4])
Some expressions change semantics: ...#xpointer(//rcp:recipe[./rcp:title ='Zuppa Inglese'])
”4” < ”4.0” ...#element(/1/5)
...#r102
is false in XPath 1.0 but true in XPath 2.0
An Introduction to XML and Web Technologies 55 An Introduction to XML and Web Technologies 56
14
XLink An XLink Link
<mylink xmlns:xlink="http://www.w3.org/1999/xlink"
Generalizing hyperlinks from HTML to XML xlink:type="extended">
<myresource xlink:type="locator"
Allow many-to-many relations xlink:href="students.xml#Carl" xlink:label="student"/>
<myresource xlink:type="locator"
Allow third party links xlink:href="students.xml#Fred" xlink:label="student"/>
<myresource xlink:type="locator"
Allow arbitrary element names xlink:href="teachers.xml#Joe" xlink:label="teacher"/>
<myarc xlink:type="arc"
xlink:from="student" xlink:to="teacher"/>
</mylink>
An Introduction to XML and Web Technologies 57 An Introduction to XML and Web Technologies 58
myresource myarc
<mylink xlink:type="extended">
<myresource xlink:type="resource"
mylink xlink:label="local"/>
myresource <myresource xlink:type="locator"
myresource
xlink:label="remote" xlink:href="..."/>
<myarc xlink:type="arc"
xlink:from="local" xlink:to="remote" xlink:show="..."
Fred Joe .../>
</mylink>
myarc
An Introduction to XML and Web Technologies 59 An Introduction to XML and Web Technologies 60
15
HTML Links in XLink The HLink Alternative
An Introduction to XML and Web Technologies 61 An Introduction to XML and Web Technologies 62
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/
16