当前位置: 首页 > 网络学院 > XML相关教程 > XPath > XPath 坐标轴
We will use the following XML document in the examples below.
我们将在下述案例中引用下面这份XML文档:
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <book> </bookstore> |
An axis defines a node-set relative to the current node.
坐标轴是用于定义与当前节点相关的节点集。
AxisName 方法名称 | Result 结果 |
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 everything in the document that is before the start tag of the current node 选取文档中所有位于当前节点的起始标签之前的元素 |
preceding-sibling | Selects all siblings before the current node 选取位于当前节点之前的所有同类节点 |
self | Selects the current node 选取当前节点 |
A location path can be absolute or relative.
区域路径表达式可以是绝对路径,也可以是相对路径。
An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more steps, each separated by a slash:
绝对路径表达式以(/)开始,相对路径则不是。两种路径表达式都包含一个或多个层次,用 / 分隔:
An absolute location path: /step/step/... A relative location path: step/step/... |
Each step is evaluated against the nodes in the current node-set.
每个层次都对应当前节点集中的一个节点值。
A step consists of:
该层次包括:
The syntax for a location step is:
选取区域路径的语法如下:
axisname::nodetest[predicate] |
Example 案例 | Result 结果 |
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 选取当前节点下的所有text子节点 |
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节点的祖类节点和book节点本身 |
child::*/child::price | Selects all price grandchildren of the current node 选取当前节点下的所有price孙类节点 |