|
Path supports numerical, equality, relational, and Boolean expressions.
Numerical Expressions
Numerical expressions are used to perform arithmetic operations on numbers.
Operator
Description
Example
Result
+
Addition
6 + 4
10
-
Subtraction
6 - 4
2
*
Multiplication
6 * 4
24
div
Division
8 div 4
2
mod
Modulus (division remainder)
5 mod 2
1
Note: XPath always converts each operand to a number before performing an arithmetic expression.
Equality Expressions
Equality expressions are used to test the equality between two values.
Operator
Description
Example
Result
=
Like (equal)
price=9.80
true (if price is 9.80)
!=
Not like (not equal)
price!=9.80
false
Testing Against a Node-Set
If the test value is tested for equality against a node-set, the result is true if the node-set contains any node with a value that matches the test value.
If the test value is tested for not equal against a node-set, the result is true if the node-set contains any node with a value that is different from the test value.
The result is that the node-set can be equal and not equal at the same time !!!
Relational Expressions
Relational expressions are used to compare two values.
Operator
Description
Example
Result
<
Less than
price<9.80
false (if price is 9.80)
<=
Less or equal
price<=9.80
true
>
Greater than
price>9.80
false
>=
Greater or equal
price>=9.80
true
Note: XPath always converts each operand to a number before performing the evaluation.
Boolean Expressions
Boolean expressions are used to compare two values.
Operator
Description
Example
Result
or
or
price=9.80 or price=9.70
true (if price is 9.80)
and
and
price<=9.80 and price=9.70
false
XPath Functions
XPath contains a function library for converting data.
XPath Function Library
The XPath function library contains a set of core functions for converting and translating data.
Node Set Functions
Name
Description
Syntax
count()
Returns the number of nodes in a node-set
number=count(node-set)
id()
Selects elements by their unique ID
node-set=id(value)
last()
Returns the position number of the last node in the processed node list
number=last()
local-name()
Returns the local part of a node. A node usually consists of a prefix, a colon, followed by the local name
string=local-name(node)
name()
Returns the name of a node
string=name(node)
namespace-uri()
Returns the namespace URI of a specified node
uri=namespace-uri(node)
position()
Returns the position in the node list of the node that is currently being processed
number=position()
String Functions
Name
Description
Syntax & Example
concat()
Returns the concatenation of all its arguments
string=concat(val1, val2, ..)
Example: concat(''''The'''','''' '''',''''XML'''') Result: ''''The XML''''
contains()
Returns true if the second string is contained within the first string, otherwise it returns false
bool=contains(val,substr)
Example: contains(''''XML'''',''''X'''') Result: true
normalize-space()
Removes leading and trailing spaces from a string, and replaces all internal sequences of white with one white space
string=normalize-space(string)
Example: normalize-space('''' The XML '''') Result: ''''The XML''''
starts-with()
Returns true if the first string starts with the second string, otherwise it returns false
bool=starts-with(string,substr)
Example: starts-with(''''XML'''',''''X'''') Result: true
string()
Converts the value argument to a string
string(value)
Example: string(314) Result: ''''314''''
string-length()
Returns the number of characters in a string
number=string-length(string)
Example: string-length(''''Beatles'''') Result: 7
substring()
Returns a part of the string in the string argument
string=substring(string,start,length)
Example: substring(''''Beatles'''',1,4) Result: ''''Beat''''
substring-after()
Returns the part of the string in the string argument that occurs after the substring in the substr argument
string=substring-after(string,substr)
Example: substring-after(''''12/10'''',''''/'''') Result: ''''10''''
substring-before()
Returns the part of the string in the string argument that occurs before the substring in the substr argument
string=substring-before(string,substr)
Example: substring-before(''''12/10'''',''''/'''') Result: ''''12''''
translate()
Performs a character by character replacement. It looks in the value argument for characters contained in string1, and replaces each character for the one in the same position in the string2
string=translate(value,string1,string2)
Examples: translate(''''12:30'''',''''30'''',''''45'''') Result: ''''12:45''''
translate(''''12:30'''',''''03'''',''''54'''') Result: ''''12:45''''
translate(''''12:30'''',''''0123'''',''''abcd'''') Result: ''''bc:da''''
Number Functions
Name
Description
Syntax & Example
ceiling()
Returns the smallest integer that is not less than the number argument
number=ceiling(number)
Example: ceiling(3.14) Result: 4
floor()
Returns the largest integer that is not greater than the number argument
number=floor(number)
Example: floor(3.14) Result: 3
number()
Converts the value argument to a number
number=number(value)
Example: number(''''100'''') Result: 100
round()
Rounds the number argument to the nearest integer
integer=round(number)
Example: round(3.14) Result: 3
sum()
Returns the total value of a set of numeric values in a node-set
number=sum(nodeset)
Example: sum(/cd/price)
Boolean Functions
Name
Description
Syntax & Example
boolean()
Converts the value argument to Boolean and returns true or false
bool=boolean(value)
false()
Returns false
false()
Example: number(false()) Result: 0
lang()
Returns true if the language argument matches the language of the xsl:lang element, otherwise it returns false
bool=lang(language)
not()
Returns true if the condition argument is false, and false if the condition argument is true
bool=not(condition)
Example: not(false())
true()
Returns true
true()
Example: number(true()) Result: 1
XPath Examples
We will use the CD catalog from our XML tutorial to demonstrate some XPath examples.
The CD catalog
If you have studied our XML tutorial, you will remember this XML document:
(A fraction of the CD catalog)
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
&l上一页 [1] [2] [3] [4] 下一页 [系统软件]给IE增加dom3xpath支持 [VB.NET程序]VBCOM TUTORIAL(3) [VB.NET程序]VBCOM TUTORIAL(2) [VB.NET程序]VBCOM TUTORIAL(1) [VB.NET程序]Microsoft Agent Tutorial Chapter 2 [VB.NET程序]Microsoft Agent Tutorial Chapter 1 [Web开发]XSLT轻松入门第四章:XPath的语法 [Web开发]XPath 2.0 的新特性 [Web开发]JDOM/XPATH编程指南 [Web开发].NET中的xpath
|