XPath实例

XPath实例
翻译:Linyupark / 2006-03-24

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:
我们使用了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") 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值