XPath Operators Basic

An XPath expression returns either a node-set, a string, a Boolean, or a number.

一个 XPath 表达式可以返回一个节点集,一个字符串,一个布尔值,或一个数值

 

XPath Operators

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

下面是一些可以用在XPath表达式中的操作符的清单:

Operator Description Example Return value
|

Computes two node-sets

返回两个节点集

//book | //cd

Returns a node-set with all book and cd elements

返回一个所有book 元素和cd元素的节点集

+ Addition加 6 + 4 10
- Subtraction减 6 - 4 2
* Multiplication乘

6 * 4

24
div Division除 8 div 4 2
= Equal等于 price=9.80 true if price is 9.80
false if price is 9.90
!= Not equal不等于 price!=9.80 true if price is 9.90
false if price is 9.80
< Less than小于 price<9.80 true if price is 9.00
false if price is 9.80
<= Less than or equal to小于等于 price<=9.80 true if price is 9.00
false if price is 9.90
> Greater than大于 price>9.80 true if price is 9.90
false if price is 9.80
>=

Greater than or equal to大于等于

price>=9.80 true if price is 9.90
false if price is 9.70
or or或 price=9.80 or price=9.70 true if price is 9.80
false if price is 9.50
and and 且 price>9.00 and price<9.90 true if price is 9.80
false if price is 8.50
mod Modulus (division remainder)取余 5 mod 2

 

Examples

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":

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

 

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:

我们通过使用Microsoft  的XMLDOM对象来加载XML文档和selectNodes() 函数来从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")

 

Select the First book Node选择第一个book节点

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

xmlDoc.selectNodes("/bookstore/book[0]")

 

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

:W3C标准为使用[1]来选择第一个

A Workaround! 解决办法!

To solve the [0] and [1] problem in IE5+, you can set the SelectionLanguage to XPath.

为了解决这个问题,你可以通过为XPath设置SelectionLanguage

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

xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.selectNodes("/bookstore/book[1]")

 

 

Select the prices选择price元素

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

xmlDoc.selectNodes("/bookstore/book/price/text()") 

 

Selecting price Nodes with Price>35选择price值大于35的元素

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

xmlDoc.selectNodes("/bookstore/book[price>35]/price") 
posted @ 2008-08-31 01:30  Scott Xu(南方小鬼)  阅读(356)  评论(0编辑  收藏  举报
乘客