XPath 语法

XPath 语法
w3pop.com / 2006-09-21

come from : http://www.w3pop.com/

XPath Nodes(节点) XPath Axes

XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.
XPath 通过路径表达式从XML文档中选取节点或节点设置。可以通过一条语句或相应的步骤选取一个节点。


The XML Example Document
XML文档实例

We will use the following XML document in the examples below.
我们将在接下来的文章中引用这个XML文档。

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>



Selecting Nodes
选取节点

XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below:
XPath通过路径表达式在XML文档中选取节点。可以通过一条语句或相应的步骤选取一个节点。下面列出了最常使用的路径表达式:

Expression
表达式
Description
注释
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
选取属性

Examples
实例

In the table below we have listed some path expressions and the result of the expressions:
在下面的表格中,我们列出了一些路径表达式及其运行的结果:

Path Expression
表达式
Result
结果
bookstoreSelects all the child nodes of the bookstore element
选取bookstore元素的所有子节点
/bookstoreSelects the root element bookstore
选取bookstore元素的根节点

Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!
注意:如果一个路径以(/)开始,那么它必须是表述该元素所在的绝对路径

bookstore/bookSelects all book elements that are children of bookstore
选取bookstore中的所有book子元素
//bookSelects all book elements no matter where they are in the document
选取文档中的所有book元素
bookstore//bookSelects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element
选取文档中所有处于bookstore节点下的book元素
//@langSelects all attributes that are named lang
选取所有lang属性


Predicates
条件

Predicates are used to find a specific node or a node that contains a specific value.
它指定了选取节点的范围。

Predicates are always embedded in square brackets.
通常使用方括号[ ]来指定条件。

Examples
实例

In the table below we have listed some path expressions with predicates and the result of the expressions:
在下面的表格中我们列出了一些指定条件的表达式及其运行结果:

Path Expression
表达式
Result
结果
/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节点下的所有包含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节点下的所有包含price元素大于35.00的book节点下的title元素


Selecting Unknown Nodes
选取未知节点

XPath wildcards can be used to select unknown XML elements.
可以通过 XPath 通配符选取未知的XML元素。

Wildcard
通配符
Description
注释
*Matches any element node
匹配任意的节点元素
@*Matches any attribute node
匹配任意的节点属性
node()Matches any node of any kind
匹配任意种类的节点

Examples
实例

In the table below we have listed some path expressions and the result of the expressions:
在下面的表格中我们列出了一些表达式及其运行结果:

Path Expression
表达式
Result
结果
/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元素


Selecting Several Paths
选取多个路径

By using the | operator in an XPath expression you can select several paths.
你可以同过在表达式中添加 | 来选取多个路径。

Examples
实例

In the table below we have listed some path expressions and the result of the expressions:
在下面的表格中我们列出了一些表达式及其运行结果:

Path Expression
表达式
Result
结果
//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
选取bookstore节点下的book节点中的所有title元素和文档中所有的price元素
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值