An axis defines a node-set relative to the current node.
轴定义了相对于当前节的节集

The XML Example Document

XML举例文档

We will use the following XML document in the examples below.
我么将使用该XML文档进行下面的举例说明


Harry Potter 29.99


Learning XML 39.95


XPath Axes

XPath轴

轴名结果
ancestorSelects all ancestors (parent, grandparent, etc.) of the current node[选择了当前节的所有祖(父,祖父,等等)]
ancestor-or-selfSelects all ancestors (parent, grandparent, etc.) of the current node and the current node itself[选择当前节的所有祖并且还有当前节自己]
attributeSelects all attributes of the current node[选择所有当前节的属性]
childSelects all children of the current node[选择所有当前节的子]
descendantSelects all descendants (children, grandchildren, etc.) of the current node[选择所有当前节的孙(子,孙子,等等)]
descendant-or-selfSelects all descendants (children, grandchildren, etc.) of the current node and the current node itself[选择当前节的所有孙以及它本身]
followingSelects everything in the document after the closing tag of the current node[选择所有在关闭当前节标签后的所有内容]
following-siblingSelects all siblings after the current node[选择所有当前节后的兄]
namespaceSelects all namespace nodes of the current node[选择所有当前节的命名空间]
parentSelects the parent of the current node[选择当前节的父]
precedingSelects everything in the document that is before the start tag of the current node[选择当前节之前的所有内容]
preceding-siblingSelects all siblings before the current node[选择所有当前节之前的兄]
selfSelects 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)[0个或多个谓语可以来更好的选择节]

The syntax for a location step is:
定位的语法

axisname::nodetest[predicate]

Examples

实例

Example结果
child::bookSelects all book nodes that are children of the current node[选择当前节点下所有为book的子节点]
attribute::langSelects 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::bookSelects all book descendants of the current node[选择当前节点所有为book的孙节点]
ancestor::bookSelects all book ancestors of the current node[选择所有当前祖节点为book的节点]
ancestor-or-self::bookSelects all book ancestors of the current node – and the current as well if it is a book node[当前节点和其祖节点为book的节点]
child::*/child::priceSelects all price grandchildren of the current node[当前节点所有含price的孙子节点]
An XPath expression returns either anode-set, a string, a Boolean, or a number.


XPath Operators

Below is a list of the operators that can be used in XPath expressions:

OperatorDescriptionExampleReturn value|Computes two node-sets//book | //cdReturns a node-set with all book and cd elements+Addition6 + 410–Subtraction6 – 42*Multiplication6 * 4

24divDivision8 div 42=Equalprice=9.80true if price is 9.80
false if price is 9.90!=Not equalprice!=9.80true if price is 9.90
false if price is 9.80<Less thanprice<9.80true if price is 9.00
false if price is 9.80<=Less than or equal toprice<=9.80true if price is 9.00
false if price is 9.90>Greater thanprice>9.80true if price is 9.90
false if price is 9.80>=Greater than or equal toprice>=9.80true if price is 9.90
false if price is 9.70ororprice=9.80 or price=9.70true if price is 9.80
false if price is 9.50andandprice>9.00 and price<9.90true if price is 9.80
false if price is 8.50modModulus (division remainder)5 mod 21
Let’s try to learn some basic XPath syntax by looking at some examples.
让我们来尝试通过观察一些实例来学习基础的XPath语法


The XML Example Document

We will use the following XML document in the examples below.
我们将使用下面这个XML文档来进行实例

“books.xml”:


Everyday Italian
Giada De Laurentiis
2005 30.00


Harry Potter
J K. Rowling
2005 29.99


XQuery Kick Start
James McGovern
Per Bothner
Kurt Cagle
James Linn
Vaidyanathan Nagarajan
2003 49.99


Learning XML
Erik T. Ray
2003 39.95

View the “books.xml” file in your browser.


Selecting Nodes

选择节点

We will use the Microsoft XMLDOM object to load the XML document and the selectNodes() function to select nodes from the XML document:
我们使用了XMLDOM对象来加载XML文档并用selectNode()函数来进行XML文档上节点的选择:

set xmlDoc=CreateObject(“Microsoft.XMLDOM”)
xmlDoc.async=”false”
xmlDoc.load(“books.xml”)

xmlDoc.selectNodes(path expression)


Select all book Nodes

选择所有book节点

The following example selects all the book nodes under the bookstore element:
下面这个实例就会选择所有bookstore元素以下的book节点:

xmlDoc.selectNodes(“/bookstore/book”)

如果你有IE5以上的版本你可以自己来做一下.


Select the First book Node

选择第一个book节点

The following example selects only the first book node under the bookstore element:

xmlDoc.selectNodes(“/bookstore/book[0]”)

If you have IE 5 or higher you can try it yourself.

Note: IE 5 and 6 has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!


Select the prices

选择prices

The following example selects the text from all the price nodes:

xmlDoc.selectNodes(“/bookstore/book/price/text()”)

If you have IE 5 or higher you can try it yourself.


Selecting price Nodes with Price>35

选择price大于35的price节点

The following example selects all the price nodes with a price higher than 35:

xmlDoc.selectNodes(“/bookstore/book
[price>35]/price”)

If you have IE 5 or higher you can try it yourself.


Selecting title Nodes with Price>35

选择Price大于35的title节点

The following example selects all the title nodes with a price higher than 35:

xmlDoc.selectNodes(“/bookstore/book[price>35]/title”)

If you have IE 5 or higher you can try it yourself.