java的xpath语法

常用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(stringstringstring*)

concat 函数返回它的参数的联结。

 

boolean starts-with(stringstring)

如果第一个字符串参数以第二个字符串参数起头,starts-with 函数返回真,否则,返回假。

 

boolean contains(stringstring)

如果第一个字符串参数包含第二个字符串参数,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 substringstring , number ,number? )

substring 函数返回第一个字符串参数从第二个参数所指定的位置开始,以第三个参数为长度的子字符串。例如, substring("12345",2,3) 返回"234"。如果没有第三个参数,则返回从第二个参数所指定的位置开始直到结束。例如, substring("12345",2) 返回"2345"。

 

number string-lengthstring? )

string-length 返回字符串中字符的个数。如果参数省略,则缺省为一个上下文节点再转换成字符串,也就是上下文节点的 字符串值。

 

string normalize-space(string?)

normalize-space 函数返回空白符规范化 后的参数字符串,该规范化是清除领头及结尾的空白字以及用一个空白符替换连续的空白符。空白符字符与XML 中的产生式 S 所允许的是一样的。如果参数省略,则缺省为一个上下文节点再转换成字符串,也就是上下文节点的 字符串值。

 

string translate(stringstring,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]

获得文本节点的文本值为基数的所有节点

 


 

例子:
表达式描述
nodenameSelects 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[选择属性]



 


路径表达式结果
bookstoreSelects all the child nodes of the bookstore element[选择所有bookstore元素的子节]
/bookstoreSelects the root element bookstore

Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!

[选择了bookstore的根元素。注意:如果路径的开始为(/)那此路径一定是到该元素的绝对路径]
bookstore/bookSelects all book elements that are children of bookstore[选择了所有在bookstore的子元素book元素所包含的所有元素(其实就为bookstore里book元素所包含的元素)]
//bookSelects all book elements no matter where they are in the document[选择所有为book元素的内容而不管book元素处于何处(有不同的父也没关系)]
bookstore//bookSelects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element[在bookstore元素内所有含有book元素的元素内容(只要book元素的祖元素为bookstore元素那都符合条件)]
//@langSelects 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]/titleSelects 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/priceSelects all the title AND price elements of all book elements[选择所有book里title和price元素]
//title | //priceSelects all the title AND price elements in the document[选择所有title和price元素]
/bookstore/book/title | //priceSelects 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::bookSelects all book nodes that are children of the current node[选择当前节点下所有为book的子节点]
attribute::langSelects 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::bookSelects all book descendants of the current node[选择当前节点所有为book的孙节点]
ancestor::bookSelects all book ancestors of the current node[选择所有当前祖节点为book的节点]
ancestor-or-self::bookSelects all book ancestors of the current node - and the current as well if it is a book node[当前节点和其祖节点为book的节点]
child::*/child::priceSelects all price grandchildren of the current node[当前节点所有含price的孙子节点]


例子:

/html/bodytable[@class='margin_left']/tbody/tr/td[contains(strong/text(),"姓名")]/p 
选择 td节点文本中含有字符串“姓名”的字节点p
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JsoupXpath 是一款纯Java开发的使用xpath解析html的解析器,xpath语法分析与执行完全独立,html的DOM树生成借助Jsoup,故命名为JsoupXpath.为了在java里也享受xpath的强大与方便但又苦于找不到一款足够强大的xpath解析器,故开发了JsoupXpath。JsoupXpath的实现逻辑清晰,扩展方便,支持几乎全部常用的xpath语法.http://www.cnblogs.com/ 为例 "//a/@href"; "//div[@id='paging_block']/div/a[text()='Next >']/@href"; "//div[@id='paging_block']/div/a[text()*='Next']/@href"; "//h1/text()"; "//h1/allText()"; "//h1//text()"; "//div/a"; "//div[@id='post_list']/div[position()1000]/div/h3/allText()"; //轴支持 "//div[@id='post_list']/div[self::div/div/div/span[@class='article_view']/a/num()>1000]/div/h3/allText()"; "//div[@id='post_list']/div[2]/div/p/preceding-sibling::h3/allText()"; "//div[@id='post_list']/div[2]/div/p/preceding-sibling::h3/allText()|//div[@id='post_list']/div[1]/div/h3/allText()"; 在这里暂不列出框架间的对比了,但我相信,你们用了会发现JsoupXpath就是目前市面上最强大的的Xpath解析器。 快速开始 如果不方便使用maven,可以直接使用lib下的依赖包跑起来试试,如方便可直接使用如下dependency(已经上传至中央maven库,最新版本0.1.1):    cn.wanghaomiao    JsoupXpath    0.1.1 依赖配置好后,就可以使用如下例子进行体验了!String xpath="//div[@id='post_list']/div[./div/div/span[@class='article_view']/a/num()>1000]/div/h3/allText()";String doc = "..."; JXDocument jxDocument = new JXDocument(doc); List<Object> rs = jxDocument.sel(xpath); for (Object o:rs){     if (o instanceof Element){             int index = ((Element) o).siblingIndex();             System.out.println(index);     }     System.out.println(o.toString()); } 其他可以参考 cn.wanghaomiao.example包下的例子 语法 支持标准xpath语法(支持谓语嵌套),支持全部常用函数,支持全部常用轴,去掉了一些标准里面华而不实的函数和轴,下面会具体介绍。语法可以参考http://www.w3school.com.cn/xpath/index.asp 关于使用Xpath的一些注意事项 非常不建议直接粘贴Firefox或chrome里生成的Xpa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值