再学习xpath----不推荐firepath工具取值
1.选择nodes
nodename :通过nodename来定位,如 bookstore--选择了所有的bookstore 节点.
/ : 从根节点开始定位,如/bookstore--选择根节点bookstore;bookstore/book--选择了bookstore下的所有book子节点. 注意:/开头代表了绝对路径。
// : 从当前节点开始选择文档中所有匹配的节点,如//book--选择了所有的book节点,无论它们处于什么位置;bookstore//book--选择bookstore下的所有子孙book节点。
. : 当前节点
.. : 当前节点的父节点
@ :选择属性,如选择所有name为lang的 属性
2. 使用条件搜索
/bookstore/book[1] : 选择bookstore子节点的第一个book 节点
注意:IE5之后起点为[0],当时根据W3C标准,应该是从[1]开始
/bookstore/book[last()] : 选择bookstore下最后一个book子节点
/bookstore/book[last()-1] : Selects the last but one book element that is the child of the bookstore element
/bookstore/book[position()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]/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
3. 匹配搜索
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind
/bookstore/* :Selects all the child nodes of the bookstore element
//* :Selects all elements in the document
//title[@*] :Selects all title elements which have any attribute
4. 选择多个
//book/title | //book/price :Selects all the title AND price elements of all book elements
//title | //price :Selects all the title AND price elements in the document
/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
5. 使用XPath Axes (相对node)
ancestor :Selects all ancestors (parent, grandparent, etc.) of the current node
ancestor-or-self :Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself
attribute :Selects all attributes of the current node
child :Selects all children of the current node
descendant :Selects all descendants (children, grandchildren, etc.) of the current node
descendant-or-self :Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself
following :Selects everything in the document after the closing tag of the current node
following-sibling :Selects all siblings after the current node
namespace :Selects all namespace nodes of the current node
parent :Selects the parent of the current node
preceding :Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes
preceding-sibling :Selects all siblings before the current node
self :Selects the current node
定位path表达式
基本的step:
1. axis:定义所选节点和目前节点间的相对树形关系
2. node-test:识别axis中的一个节点
3. zero or more predicates:使用条件搜索
结构:axisname::nodetest[predicate]
child::book :Selects all book nodes that are children of the current node
attribute::lang :Selects the lang attribute of the current node
child::* :Selects all element children of the current node
attribute::* :Selects all attributes of the current node
child::text() Selects all text node children of the current node
child::node() Selects all children of the current node
descendant::book Selects all book descendants of the current node
ancestor::book Selects all book ancestors of the current node
ancestor-or-self::book Selects all book ancestors of the current node - and the current as well if it is a book node
child::*/child::price Selects all price grandchildren of the current node
6. XPATH运算
| :Computes two node-sets
//book | //cd --- Returns a node-set with all book and cd elements
+,-,*,div : 加减乘除
=,!= :等和,不等
<,<=,>,>=: 小于,小于等于,大于大于等于
or, and: 与 ,或
mod: 取模
7. xpath内建功能库
给链接 暂省略:http://www.w3schools.com/xpath/xpath_functions.asp