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