网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

XPath 语法

http://www.diybl.com/ 2008-5-3  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】

摘自:http://www.w3pop.com/learn/view/p/1/o/0/doc/xpath_syntax/

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
注释
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
选取属性

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元素的所有子节点
/bookstore Selects the root element bookstore
选取以bookstore元素为根目录的点

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

bookstore/book Selects all book elements that are children of bookstore
选取bookstore中的所有book子元素
//book Selects all book elements no matter where they are in the document
选取文档中的所有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元素
//@lang Selects 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]
欢迎光临DIY部落,点击这里查看更多文章教程   【点击打包该文章】
[1] [2]
如果图片或页面不能正常显示请点击这里 站内搜索:   
上一篇文章:Real Player rmoc3260.dll Exp
下一篇文章:通用Inline Hook代码

文章评论

请您留言