XPath Axes Basic

The XML Example Document

We will use the following XML document in the examples below.

<?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

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

选择当前节点的所有文本子节点

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

posted @ 2008-08-31 01:06  Scott Xu(南方小鬼)  阅读(234)  评论(0编辑  收藏  举报
乘客