`

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]

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

 

 

 

例子:

表达式 描述
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

去学习一下。

分享到:
评论

相关推荐

    Java中使用xpath获取xml中的数据

    使用xpath读取xml中的数据

    java xml xpath介绍

    java xml xpath介绍,XPath 使用路径表达式来选取 XML 文档中的节点或节点集。节点是通过沿着路径 (path) 或者步 (steps) 来选取的。

    Java开源的xpath解析器Jsoupxpath.zip

    为了在java里也享受xpath的强大与方便但又苦于找不到一款足够强大的xpath解析器,故开发了JsoupXpath。JsoupXpath的实现逻辑清晰,扩展方便,支持几乎全部常用的xpath语法.http://www.cnblogs.com/ 为例 "//a/@href...

    Java 语言的 XPath API

    如果需要从 XML 文档中提取信息,最快捷、最简单的办法就是在 Java:trade_mark: 程序中嵌入 XPath 表达式。Java 5 推出了 javax.xml.xpath 包,这是一个用于 XPath 文档查询的独立于 XML 对象模型的库。查询语言和...

    java 写的xpath生成工具

    自己写的一个xpath生成工具,现在只实现了本地html和xml文件的xpath生成,工具要求JDK1.5以上版本运行

    Java版本的XPath方式解析jar和源代码

    Java版本的XPath方式解析, 通常用具java开发,或者android项目开发,压缩包有源代码和jar文件,可直接使用

    xpath--java框架技术

    xpath--java框架技术

    xpath.jarxpath.jar

    xpath.jar

    JsonPath使用类似XPATH的语法从JSON提取数据

    JsonPath:使用类似XPATH的语法从JSON提取数据

    java xpath demo

    采用java自身JDK编写的 xpath demo 覆盖的使用场景比较多。至少是一个学习 xpath的好例子。不管你信不信,反正我信了。

    java使用xpath解析xml示例分享

    XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力,下面是一小示例,需要的朋友可以参考下

    jaxen:Java的Jaxen XPath引擎

    杰克森 Java的Jaxen XPath引擎Jaxen是用Java编写的开源XPath库。 它适用于许多不同的对象模型,包括DOM,XOM,dom4j和JDOM。 还可以编写将非XML树(例如已编译的Java字节码或Java bean)视为XML的适配器,从而使您也...

    JsoupXpath

    整理JsoupXpath( https://github.com/zhegexiaohuozi/JsoupXpath)是一款纯Java开发的使用xpath解析提取html内容的解析器,xpath语法分析与执行...JsoupXpath的实现逻辑清晰,扩展方便, 支持几乎全部常用的xpath语法。

    XPath教程(描述XPath的用法)

    XPath:描述了XPath的用法,XPath多应用于XML文件的解析,这是一个非常棒的文档!

    xpath的jar包

    java开元jar包 xpath的jar包 4个版本解压使用 xpath的jar包

    Java 语言的 XPath API.docx

    XPath 表达式比繁琐的...如果需要从 XML 文档中提取信息,最快捷、最简单的办法就是在 Java™ 程序中嵌入 XPath 表达式。Java 5 推出了 javax.xml.xpath 包,这是一个用于 XPath 文档查询的独立于 XML 对象模型的库。

    dom4j和xpath必备jar包

    dom4j和xpath必备jar包对xml文档进行解析的必须jar包!

    JAVA通过XPath解析XML性能比较详解

    本篇文章主要介绍了JAVA通过XPath解析XML性能比较详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    JS中常用的xpath特性

    JS中常用的xpath特性 //选择所有dir元素的值等于xx的元素 Dom.documentElement.selectNodes("server/dir[text()='xx']") //选择server元素下的所有dir元素: Dom.documentElement.selectNodes("server/dir...

    XPath and XSLT

    XML XPath XSL XSLTXML XPath XSL XSLTXML XPath XSL XSLTXML XPath XSL XSLTXML XPath XSL XSLT

Global site tag (gtag.js) - Google Analytics