6.4 Expressions


Expressions in XPath include the sorts of structures, operators, and data types found in most computer languages, albeit with a few differences. This section gives an overview of these distinguishing features. Consult the XPath Recommendation [XPath] for the full details. Note that location paths can appear in expressions, and expressions can appear in location paths as predicates (see Section 6.3.4).

Table 6-3. XPath Abbreviations
Abbreviation Description
Examples
//

Equivalent to the location step

 /descendant-or-self::node()/ 

Because this path is defined as ending with a slash, it cannot occur at the end of a location path. Because it is defined as starting with a slash, when it occurs at the beginning of a location path, that path is absolute.

 //self::node() 

An abbreviated absolute location path for all of the elements, comments, and processing instructions in a document.

 child::foo//self::bar 

All "bar" elements that are descendants of a "foo" element child of the context node.

@

Equivalent to the axis

 attribute:: //@* 

All attribute nodes in a document.

 @foo 

The "foo" attribute of the context node.

..

Equivalent to

 parent::node() ../child::comment() 

All comment children of the context node's parent.

 //self::foo/.. 

All parents of "foo" elements in the current document.

.

Equivalent to

 self::node() 

Sometimes useful in conjunction with //.

 .// 

All elements, comments, and processing instructions under the context element.

 //. 

All elements, comments, and processing instructions in the document.

default axis is child
 child:: 

Can be omitted.

 foo//bar 

All "bar" elements that are descendants of a "foo" element child of the context node.

//foo/bar

All "bar" elements that are children of "foo" elements in the document.

The evaluation of XPath expressions occurs in a context. This context consists of a node, position, and set of variable bindings. Section 6.3 discussed nodes and positions as they apply to location paths. XPath does not support looping or assignment constructs, although you can achieve a limited repetition effect by using an axis in a location step. While the context node and position can be different at different places in evaluating an XPath expression, particularly inside a location path, the context variable bindings do not change during the evaluation of an expression.

Location paths always produce a node-set, which can be empty.

6.4.1 Function Calls

The syntax for a function call has the typical format: the function name followed by zero or more comma-separated arguments (parameters) in parentheses. For example:

 example(parameter1, parameter2) 

When the type of a particular parameter is fixed as "string," "number," or "boolean," and a different type of parameter is supplied, the type is converted as if by calling the "string( )," "number( )," or "boolean( )" function, respectively. An error occurs if a parameter of any other type is supplied when the program expects a "node-set" type parameter.

Section 6.5 provides a full list of the core library functions. Particular applications of XPath may supplement this library. Node tests (listed in Section 6.3.3) that look like function calls are considered to be special instances of the XPath syntax, not separate functions.

6.4.2 Operators

This section lists all of the operators available in XPath expressions except for the operators that are part of the syntax of location paths, such as the slash and double colons. If the values presented to an operator are of the wrong type, they are converted, or else generate an error. Note that some operators, such as the left angle bracket ("<"), must be encoded as a character reference or the like ("&lt;") if they occur in an XML attribute value or element content.

( ) Operators have precedence and association direction as described in [XPath] but can be overridden or clarified by explicit grouping via parentheses.

Node-Set Operators

| The vertical bar character produces the union of the node-sets appearing before and after it.

Boolean Operators

Boolean operators appear syntactically between two operands.

or The logical "or" of the Boolean values appearing before and after it. When the left operand is true, the right operand is not evaluated.

and The logical "and" of the Boolean values appearing before and after it. When the left operand is false, the right operand is not evaluated.

For the comparison operators shown below, if both operands are node-sets, then the comparison is true if either of the following conditions applies:

  • If one operand contains any node whose string value compares as true with the string value of any node in the other operand.

  • If one operand is a node-set and the other is a string, number, or boolean, then the comparison is true if any node in the node-set compares as true with the other operand after being converted by the "string( )," "number( )," or "boolean( )" function, respectively.

= Tests for equality between its operands. If neither operand is a node-set, both are converted to the same type as follows:

  • If either is a Boolean value, the other operand is converted to a Boolean value.

  • If neither is a Boolean value, but one operand is a number, the other is converted to a number.

  • If neither is a Boolean value or a number, then both operands are converted to strings.

!= Tests for inequality between its operands. Use the same rules as for "=" if neither operand is a node-set.

For the following comparison operators, if neither is a node-set, then both are converted to numbers and compared according to [IEEE 754].

< True if the left operand is less than the right operand.

> True if the left operand is greater than the right operand.

<= True if the left operand is less than or equal to the right operand.

>= True if the left operand is greater than or equal to the right operand.

Arithmetic Operators

Double-precision 64-bit floating-point numbers use the [IEEE 754] system of binary floating-point arithmetic. It includes positive and negative infinity, positive and negative zero, and "Not-a-Number" (NaN). The numeric operators, if necessary, convert operands as if by calling the "number( )" function.

div The left operand divided by the right operand. (A slash ("/") cannot be used for division because it separates location path steps.)

mod The remainder of the left operand divided by the right operand with truncation, not rounding.

+ The sum of the left and right operands. The unary plus is not supported.

- The left operand minus the right operand. It may be used as a unary operator with only a right operand that produces the same result as if the left operand had been zero. Because XML permits you to use this character inside names, it may be necessary to insert white space before a minus sign: "x-y" is a single token and "x- y" is two tokens separated by a space, whereas "x -y" and "x - y" are x minus y.



Secure XML(c) The New Syntax for Signatures and Encryption
Secure XML: The New Syntax for Signatures and Encryption
ISBN: 0201756056
EAN: 2147483647
Year: 2005
Pages: 186

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net