XPath Examples

本文通过实例介绍了XPath的基本语法,展示了如何使用XPath选择XML文档中的节点,包括选取所有书籍节点、特定条件下的节点等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XPath Examples


Let's try to learn some basic XPath syntax by looking at some examples.


The XML Example Document

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

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

set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("books.xml")
xmlDoc.selectNodes(path expression)


Select all book Nodes

The following example selects all the book nodes under the bookstore element:

xmlDoc.selectNodes("/bookstore/book")

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


Select the First book Node

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: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!

A Workaround!

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

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

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

Try it yourself


Select the 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

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

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.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值