XPath Expressions


XPath expressions select a part of data from an XML document using a location path, which notifies XPath about the path that needs to be followed in an XML document. An XPath processor returns one of the following objects after evaluating an XPath expression:

  • Nodeset: Represents an unordered collection of nodes.

  • Boolean: Represents either TRUE or FALSE.

  • Number: Represents a floating-point number.

  • String: Represents a sequence of Unicode characters .

Structure of an XPath Expression

A DOM or SAX parser parses an XPath expression by separating the character string into tokens, such as identifiers and numbers . The DOM or SAX parser then parses the resulting tokens. This process is called the tokenization. You can form tokens from a sequence of characters. XPath expressions are formed from these tokens. You can identify various tokens in an XML document using lexical structures, which defines the syntactic structure of an XPath expression. For example, the lexical structure for the node type token in an XPath expression is:

 Node Type::=commenttextprocessinginstruction 

The above structure shows that the type of node specified in an XPath expression can be a comment node, text node, or a processing instruction node.

XPath Functions for Nodeset

A nodeset is a collection of nodes. A nodeset is formed when the operator combines two or more location steps. The following XPath functions operate on nodesets:

  • count() : Accepts a nodeset as its argument and returns the total number of nodes present in the nodeset.

  • id() : Returns a nodeset that contains nodes with the given ID attribute.

  • local- name () : Returns the local part of the name of a node.

  • last() : Returns the value of the context size. In the XPath data model, you can determine the context size by counting the number of nodes that represent the same element in an XML document.

  • name() : Returns a QName representing the name of a node.

  • position() : Returns the value of the context position, which is the position of the context node in the nodeset selected prior to the predicate that the XPath processor evaluates .

  • namespace-uri() : Returns a string value that represents the namespace URI in the expanded name of the node.

Types of XPath Expressions

In addition to location path expressions, XPath supports string, numerical, equality, relational, and boolean expressions. You can use these expressions if you do not want to retrieve a nodeset from an XPath data model. You can use these expressions in XPath predicates.

You can use numerical expressions to perform arithmetic operations on numbers in an XML document. XPath converts each operand to a number before performing arithmetic evaluation.

Table 5-1 describes various numerical operators that you can use with numerical expressions:

Table 5-1: XPath Numerical Operators

Numerical operator

Description

Example

Output

+

Addition

2+1

3

-

Subtraction

2-1

1

*

Multiplication

2*1

2

Div

Division

6 div 2

3

Mod

Modulus

6 mod 4

2

The following numerical XPath expression returns all descendant nodes whose price value is divisible by 2:

 //[price mod 2 ==0] 

You can use boolean expressions to compare two element nodes in an XML document. XPath converts each operand in the boolean expression to a boolean value before evaluating it. The boolean operators that you can use with an XPath expression are:

  • Or: Performs the OR operation.

  • And: Performs the AND operation.

For example, the following XPath expression selects all child <books> element nodes that have the title and price child elements:

 /books[title and price] 

The following XPath expression selects all child <books> elements that have either the <bookcode> child element node with a value greater than 20 or the <price> child element node with a value less than 500:

 /books[(bookcode>20) or (price<500)] 

A string is defined as a sequence of one or more characters. You can also use string expressions in XPath predicates. For example, the following XPath expression selects all <title> child elements that have the name attribute value, Operating System, with the <books> element node as its parent:

 /books/title[@name=Operating System] 

FunctionCall XPath Expression

An XPath processor evaluates a FunctionCall XPath expression using a function necessary for evaluating an expression. FunctionName identifies the name of the function that evaluates the expression. All XPath functions are available in the XPath function library. The XPath processor calls a function to evaluate an XPath expression. The processor then evaluates the function arguments by converting each argument to the data type that the function accepts. The XPath processor passes these converted arguments to the called function. The value that the called function returns represents the result of the FunctionCall expression. The called function displays an error if the number of arguments passed to the called function is incorrect or if arguments are not converted to the type required by the called function.

The syntax for the XPath FunctionCall expression is:

 FunctionCall=FunctionName (arg1, arg2, ..., argn) 

In the above syntax, the function, FunctionName, operates on n number of arguments.

If the XPath processor calls a string function in an expression, arguments are converted to the string type. Similarly, if the processor calls a number function, arguments are converted to the number type.

Note  

You cannot convert an argument, which is not a nodeset type, to a nodeset.

XPath includes various functions to perform string operations, such as changing string characters from uppercase to lowercase. Various string functions are:

  • string() : Converts its arguments to a string value. For example, it converts boolean arguments to string values, TRUE or FALSE.

  • string-length () : Returns the total number of characters in a string.

  • starts-with () : Determines whether or not a string starts with another string.

  • substring() : Returns a portion of a string determined by the numeric arguments that describe the substring. For example, the function, substring(˜Tokyo , 1, 3) returns the substring, Tok. The numeric value, 1, in this function indicates the starting location and 3 indicates the number of characters in the substring.

  • concat() : Combines two or more string arguments in the specified sequence. For example, the function, concat(˜The , ˜world) returns the value, The world.

  • contains() : Checks whether or not one string value contains another string value as its substring. For example, the function, contains(˜priority , ˜prior) returns the value TRUE because the string, priority, contains prior as its substring.

  • normalize-space() : Removes the leading and trailing whitespace from a string. It also converts the irrelevant whitespaces within a string by converting the sequence of whitespaces to a single space string. For example, the function, normalize-space(˜Global Systems) returns the string ˜Global Systems .

  • substring-after() : Returns that portion of a string, which appears after the first occurrence of the specified substring. For example, the function substring-after(˜unauthorized , ˜un) returns the string, authorized, because this string appears after the specified substring, un.

  • substring-before () : Returns that portion of a string, which appears before the first occurrence of the specified substring. For example, the substring-before(˜predict , ˜dict) function returns the string, pre.

  • translate() : Accepts a string and two sequences of characters as its arguments. It then replaces the characters of the string by replacing characters in the first character sequence with equivalent characters in the second character sequence. For example, the function translate(SQL, LMNRSPQR, lmnrspqr) provides the output, sql.

XPath also consists of functions that operate on numbers in an XML document. A number represents a floating-point number. It can have any double precision 64-bit format Institute of Electrical and Electronics Engineers (IEEE) 754 value. The number functions available in XPath are:

  • number() : Converts the arguments to a number. For example, this function converts the boolean argument TRUE to the value 1 and FALSE to 0.

  • round() : Returns the integer value that is the closest to the numeric value of the argument.

  • floor() : Returns the largest integer value that is either less than or equal to the argument of the function. This function rounds off a noninteger value to the immediate lower integer value.

  • sum() : Adds a series of numbers present as a nodeset. This function converts each node in the nodeset to its corresponding string value and then, converts these string values into corresponding numeric values. This function adds all numeric values and generates the result.

  • ceiling() : Returns the smallest integer value that is greater than or equal to the function argument. It rounds off a noninteger value to the immediate higher integer value.

Note  

The round(), floor(), and ceiling() number functions accept only a single argument.

A boolean object type contain either of the two values, TRUE or FALSE. Various boolean functions in XPath are:

  • boolean() : Converts its argument to a boolean value. For example, if the argument is a number greater than 0, the boolean() function evaluates it to TRUE. Otherwise, it evaluates the number to FALSE. Similarly, if the argument is a string and the string length is 0, the boolean() function returns the boolean value FALSE. Otherwise, it returns the boolean value TRUE.

  • false() : Returns the boolean value FALSE. While working with boolean values, you can use the false() function in an XPath expression.

  • true() : Returns the boolean value TRUE. While working with boolean values, you can use the true() function in an XPath expression.

  • lang() : Returns a boolean value, which checks if the language of the context node is similar to the language supplied as an argument.

  • not() : Returns the negation of its argument. For example, if the argument is TRUE, the not() function returns the value, FALSE.

Note  

The language of the context node is defined by inserting the xml:lang attribute in an XML document.




Integrating PHP and XML 2004
Integrating PHP and XML 2004
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 51

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