|
XML Path Language (XPath)
Part:
1
2
3
(Continued from previous part...)
Build-in Functions
Commonly used build-in functions:
- boolean(number): Returns true, if the number is not a zero.
- boolean(string): Returns true, if the length of the string is great than zero.
- boolean(node_set): Returns true, if the set is not empty.
- concat(string, string, ...): Returns the concatenation of all given string objects.
- contains(string_1, string_2): Returns true if the first string object contains the second string object.
- count(node_set): Returns the number of nodes in the given node set object.
- last(): Returns the context size of the evaluation context.
- name(): Returns the qualified name of the context node.
- name(node_set): Returns the qualified name of the first node in the given node set object.
- not(boolean): Returns true, if the given boolean object is false.
- position(): Returns the context position of the evaluation context.
- string(): Returns the string value of the context node.
- string(boolean): Returns true or false based on the given boolean object.
- string(number): Returns the string presentation of the given number object.
- string(node_set): Returns the string value of the first node in the given node set object.
Expressions and Location Paths
Expression: A sequence of data objects, operators, grouping parenthesis and function calls.
Examples of expressions:
( 6 + 2 ) * 3 div 4
Location Path: An expression resulting a node set.
The semantics for a location path is:
LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
AbsoluteLocationPath ::= '/' | '/'RelativeLocationPath
RelativeLocationPath ::= LocationStep
| RelativeLocationPath'/'LocationStep
LocationStep ::= AxisName'::'NodeTest | LocationStep'['Predicate']'
| LocationStep'|'LocationStep
AxisName ::= 'ancestor' | 'ancestor-or-self' | 'attribute' | 'child'
| 'descendant' | 'descendant-or-self' | 'following'
| 'following-sibling' | 'namespace' | 'parent' | 'preceding'
| 'preceding-sibling' | 'self'
NodeTest ::= NameTest | 'node()' | 'text()' | 'comment()'
| 'processing-instruction()'
| 'processing-instruction(' StingLiteral ')'
NameTest ::= '*' | NameSpacePrefix':*' | NameSpacePrefix':'NodeName
NodeName ::= Any node name in the XML structure
StringLiteral ::= A sequence of valid characters enclosed in quotes
Predicate ::= A boolean expression | AxisName'::'NodeTest
Note that there are two operators introduced in here:
- '/': Location step delimiter.
- '|': Location step "or" operator.
Evaluation rules on location path:
- If a location path starts '/', it's an absolute path. The '/' changes the
context node to the root element.
- If a location step is followed by another location step with '/' in between,
each node in the node set produced by the first step will be used as the context node
for the second step. The final node set will be the union of all node sets produced by
apply the second step on each node of the first step.
- If a location step is followed by another location step with '|' in between,
the final node set will be the union of the two node sets produced by the first location
path and second location path.
- Axis defines how the node test should be applied. For example, 'child' defines that
the node test being applied on the child element nodes of the context node. 'attribute' defines
that the node test being applied on the attributes of the context node.
- Axis also affects the order of nodes in the resulting node set. Forward axis produces
node set with nodes ordered as they are in the XML structure. Reverse axis produces
node set with nodes reversely ordered as they are in the XML structure.
- '*' represents any node name. Text nodes are not part of '*', since text nodes
have no names.
- If predicate is specified, each node in the node set produced before this predicate
will be used as the context node for the predicate to validate. If the validation fails,
that node will be removed from the node set.
- If a predicate results a number, it will be converted to true, if
the number is equal to the context position.
- If a predicate results a string, it will be converted to true, if the string's length is great than zero.
- If a predicate results a node set, it will be converted to true, if the set not empty.
There is a number of abbreviations for the axis name and node test parts of the location step:
. self::node()
.. parent::node()
name child::name
@name attribute::name
// /descendant-or-self::node()/
Let's look at some examples of location paths:
- "name" matches any child element nodes named "name".
- "./name" matches any child element nodes named "name".
- "name1/name2" matches any grand child element nodes named "name2" inside any child
element nodes named "name1".
- ".//name" matches any element nodes named "name" in the current sub-tree.
- "*" matches any child element nodes.
- "*/*" matches any grand child element nodes in the current sub-tree.
- "name1|name2" matches any child element nodes named "name1" or "name2".
- "node()" matches any child nodes. This is a super-set of "*".
- "next()" matches any child text nodes. This is a sub-set of "node()".
- "name[1]" matches the first child element node named "name".
- "name[position()>1]" matches any child element nodes named "name", except the first one.
- "name1[name2]" matches any child element nodes named "name1", who has at least one
child element node named "name2".
- "name1[name2='text']" matches any child element nodes named "name1", who has at least one
child element node named "name2" with context equal to "text".
- "@name" matches any attribute nodes named "name".
- "@*" matches any attribute nodes.
- "name1[@name2]" matches any child element nodes named "name1", who has at least one
attribute node named "name2".
- "name1[@name2='value']" matches any child element nodes named "name1", who has at least one
attribute node named "name2" with value equal to "value".
(Continued on next part...)
Part:
1
2
3
|