常用Xpath对照表
常用的XPATH |
||
表达式 |
说明 |
|
// |
表示返回XML文档中的所有符合查找准则的元素,而忽略文档中元素的位置级别 |
|
/ |
表示绝对路径 |
|
News/Links/name |
表示相对路径 |
|
./ |
表示当前节点 |
|
../ |
表示父节点 |
|
* |
表所所有元素 |
|
or |
或 |
|
And |
与 |
|
其它表达式 |
=,!=,<,>,>=,<= |
|
Text() |
文本 |
|
Node() |
节点 |
|
number last() |
last 函数返回一个数字,该数字等于从表达式求值上下文中的上下文大小(即返回节点个数) |
|
number position() |
position函数返回一个数字,该数字等于从表达式求值上下文中的上下文位置(即当前位置) |
|
number count(node-set) |
count 函数返回在参数node-set中节点的个数。 |
|
node-set id(object) |
||
string string(object?) |
string 函数将对象依下列各项转换成字符 |
|
string concat(string, string, string*) |
concat 函数返回它的参数的联结。 |
|
boolean starts-with(string, string) |
如果第一个字符串参数以第二个字符串参数起头,starts-with 函数返回真,否则,返回假。 |
|
boolean contains(string, string) |
如果第一个字符串参数包含第二个字符串参数,contains 函数返回真,否则,返回假。 |
|
string substring-before(string,string) |
substring-before 函数返回第一个字符串参数在第二个参数首次出现之前的子字符串,或者,如果第一个字符串参数不包含第二个字符串参数则返回空字符串。例如,substring-before("1999/04/01","/") 返回 1999。 |
|
string substring-after(string,string) |
substring-after 函数返回第一个字符串参数在第二个参数首次出现之后的子字符串,或者,如果第一个字符串参数不包含第二个字符串参数则返回空字符串。例如, substring-after("1999/04/01","/") 返回 04/01,而substring-after("1999/04/01","19") 返回 99/04/01。 |
|
string substring( string , number ,number? ) |
substring 函数返回第一个字符串参数从第二个参数所指定的位置开始,以第三个参数为长度的子字符串。例如, substring("12345",2,3) 返回"234"。如果没有第三个参数,则返回从第二个参数所指定的位置开始直到结束。例如, substring("12345",2) 返回"2345"。 |
|
number string-length( string? ) |
string-length 返回字符串中字符的个数。如果参数省略,则缺省为一个上下文节点再转换成字符串,也就是上下文节点的 字符串值。 |
|
string normalize-space(string?) |
normalize-space 函数返回空白符规范化 后的参数字符串,该规范化是清除领头及结尾的空白字以及用一个空白符替换连续的空白符。空白符字符与 XML 中的产生式 S 所允许的是一样的。如果参数省略,则缺省为一个上下文节点再转换成字符串,也就是上下文节点的 字符串值。 |
|
string translate(string, string,string) |
translate 函数返回第一个参数的字符串,其中有在第二个参数中出现的字符都被在第三个参数中相对应位置的字符所替换。例如,translate("bar","abc","ABC") 返回字符串 BAr。如果,第二个参数里的字符在第三个参数的相对应位置没有字符(因为第二个参数里的字符串比第三个参数的字符串长),那么,第一个参数里的那个字符将被移去。例如,translate("--aaa--","abc-","ABC") 返回 "AAA"。如果在第二个参数里的字符出现超过一次,那么,第一次的出现决定替换的字符。如果第三个参数里的字符串比第二个参数的字符串长, 那么,多余的字符将被忽略。 |
|
boolean not(boolean) |
如果参数为真 not 函数返回真,否则返回假。 |
|
boolean true() |
true 函数返回真。 |
|
boolean false() |
The false 函数返回假。 |
|
number number(object?) |
number 函数参数依下列各项转换成数字 |
|
number sum(node-set) |
对于在参数节点集合的每个节点,sum 函数返回节点字符串值转换成数字后的和。 |
|
number floor(number) |
floor 函数返回不大于参数的整数的最大数 (最接近于正无穷大) |
|
number ceiling(number) |
ceiling 函数返回不小于参数的整数的最小数 (最接近于负无穷大 |
|
number round(number) |
round 函数返回最接近于参数的整数。 |
|
//* |
获得所有节点 |
|
../* |
获得当前节点的父节点的所有节点 |
|
//Links |
获得所有的Links节点 |
|
//Links[name=”网易”] |
获得子节点name的文本为“网易”的所有Links节点 |
|
//Links[@id=”1”] |
获得属性ID=1的所有Links节点 |
|
//*[name=”新浪”] |
获得子节点name的文本为“新浪”的所有节点 |
|
//*[@id=”1”] |
获得属性ID=1的所有节点 |
|
//*[@id] |
获得存在属生ID的所有节点点 |
|
//*[name] |
获得存在子节点name的所有节点 |
|
//Links[序号] |
获得返回的N个Links节点中的第序号个节点 |
|
//Links[1 or 2] |
获得返回的N个Links节点的中第一个和第二个节点 |
|
//*[name=”网易” and @id=”1”] |
获得所有子点节name的文本为“网易” 且自己的属生id=“1”的节点 |
|
//text() |
选择所有的正文节点(即叶子节点) |
|
//Links[position()=last()] |
获得返回的N个Links节点中的最后一个节点 等同于//Links[last()] |
|
//*[contains(name,”新”)] |
获得子节点name的文本中包含“新”的所有节点 |
|
//Links[1] |
获得返回的N个Links节点中的第一个节点 |
|
//Links[1]/name[1] |
获得第一个Links的第一个name节点 |
|
//Links//name |
获得所有Links节点下的所有name节点 |
|
//*[@id>1] |
获得属性ID>1的所有节点 |
|
//*[number(@id)+1>1] |
获得属生ID的值加1>1的所有节点 |
|
//*[number(text())>1] |
获得文本节点的文本值大于1的所有节点 |
|
//*[(number(text()) mode 2)=1] |
获得文本节点的文本值为基数的所有节点 |
例子:
表达式 | 描述 |
nodename | Selects all child nodes of the node[选择所有目前节的子节] |
/ | Selects from the root node[从根节进行选择] |
// | Selects nodes in the document from the current node that match the selection no matter where they are [选择文档中相吻合的节而不管其在文档的何处] |
. | Selects the current node[选择当前节] |
.. | Selects the parent of the current node[当前节的父节] |
@ | Selects attributes[选择属性] |
路径表达式 | 结果 |
bookstore | Selects all the child nodes of the bookstore element[选择所有bookstore元素的子节] |
/bookstore | Selects the root element bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! [选择了bookstore的根元素。注意:如果路径的开始为(/)那此路径一定是到该元素的绝对路径] |
bookstore/book | Selects all book elements that are children of bookstore[选择了所有在bookstore的子元素book元素所包含的所有元素(其实就为bookstore里book元素所包含的元素)] |
//book | Selects all book elements no matter where they are in the document[选择所有为book元素的内容而不管book元素处于何处(有不同的父也没关系)] |
bookstore//book | Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element[在bookstore元素内所有含有book元素的元素内容(只要book元素的祖元素为bookstore元素那都符合条件)] |
//@lang | Selects all attributes that are named lang[选择所有属性名为lang的属性] |
路径表达式 | 结果 |
/bookstore/book[1] | Selects the first book element that is the child of the bookstore element[选择了bookstore里的第一个book元素] |
/bookstore/book[last()] | Selects the last book element that is the child of the bookstore element[选择bookstore里最后一个book元素] |
/bookstore/book[last()-1] | Selects the last but one book element that is the child of the bookstore element[bookstore中倒数第二个book元素] |
/bookstore/book[position()<3] | Selects the first two book elements that are children of the bookstore element[在bookstore中前两个book元素] |
//title[@lang] | Selects all the title elements that have an attribute named lang[选择所有含有lang属性的title元素] |
//title[@lang='eng'] | Selects all the title elements that have an attribute named lang with a value of 'eng'[选择所有含有lang属性并且值为eng的title元素] |
/bookstore/book[price>35.00] | Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00[选择所有bookstore中book元素里price元素内容大于35.00的book元素] |
/bookstore/book[price>35.00]/title | Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00[选择bookstore中book的子元素title,并且其兄弟元素price的内容得大于35.00] |
路径表达式 | 结果 |
/bookstore/* | Selects all the child nodes of the bookstore element[选择所有bookstore的子节] |
//* | Selects all elements in the document[选择所有文档中的元素] |
//title[@*] | Selects all title elements which have any attribute[选择元素为title并且其含有属性] |
路径表达 | 结果 |
//book/title | //book/price | Selects all the title AND price elements of all book elements[选择所有book里title和price元素] |
//title | //price | Selects all the title AND price elements in the document[选择所有title和price元素] |
/bookstore/book/title | //price | Selects all the title elements of the book element of the bookstore element AND all the price elements in the document[选择所有book里的title元素和所有price元素] |
Example | 结果 |
child::book | Selects all book nodes that are children of the current node[选择当前节点下所有为book的子节点] |
attribute::lang | Selects the lang attribute of the current node[选择当前节点下所有属性为lang的内容] |
child::* | Selects all children of the current node[选择当前节下所有的子节] |
attribute::* | Selects all attributes of the current node[选择当前节所有的属性] |
child::text() | Selects all text child nodes of the current node[选择当前节点所有子节点的文字] |
child::node() | Selects all child nodes of the current node[选择所有当前节点的子节点] |
descendant::book | Selects all book descendants of the current node[选择当前节点所有为book的孙节点] |
ancestor::book | Selects all book ancestors of the current node[选择所有当前祖节点为book的节点] |
ancestor-or-self::book | Selects all book ancestors of the current node - and the current as well if it is a book node[当前节点和其祖节点为book的节点] |
child::*/child::price | Selects all price grandchildren of the current node[当前节点所有含price的孙子节点] |
例子:
/html/bodytable[@class='margin_left']/tbody/tr/td[contains(strong/text(),"姓名")]/p
选择 td节点文本中含有字符串“姓名”的字节点p
也可以到:http://www.w3school.com.cn/xpath/xpath_syntax.asp
去学习一下。