当前位置: 首页 > 网络学院 > XML相关教程 > XPath > XPath 坐标轴

XPath
XPath 介绍
XPath 节点
XPath 语法
XPath 坐标轴
XPath 操作符
XPath 实例
XPath 摘要
XPath 函数

XPath 坐标轴


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 591 ::
收藏到网摘: n/a

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>

 


 

XPath Axes
XPath 坐标轴(Axes)

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
选取当前节点

 


 

Location Path Expression
区域路径表达式

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:
该层次包括:

  • an axis (defines the tree-relationship between the selected nodes and the current node)
  • 坐标轴(定义目标节点与当前节点间的关系结构)
  • a node-test (identifies a node within an axis)
  • 节点测试(在坐标轴中确认某节点)
  • zero or more predicates (to further refine the selected node-set)
  • 零条件或多条件(进一步提取指定的节点集)

The syntax for a location step is:
选取区域路径的语法如下:

axisname::nodetest[predicate]

Examples
案例

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孙类节点

评论 (0) All

登陆 | 还没注册?