当前位置: 首页 > 网络学院 > XML相关教程 > XPath > XPath 实例
Let's try to learn some basic XPath syntax by looking at some examples.
让我们通过一些案例来学习一些关于XPath的基础语法。
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"> <book category="CHILDREN"> <book category="WEB"> <book category="WEB"> </bookstore> |
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 object 加载XML文档,用selectNodes()函数从XML文档中选择节点:
set xmlDoc=CreateObject("Microsoft.XMLDOM") xmlDoc.selectNodes(path expression) |
The following example selects all the book nodes under the bookstore element:
下述案例将演示如何选择booksotre元素中的所有book子节点:
xmlDoc.selectNodes("/bookstore/book") |
如果你的浏览器是IE5.0以上的版本,那么你可以自己尝试一下!
The following example selects only the first book node under the bookstore element:
下述案例只选择了bookstore元素中的第一个book节点:
xmlDoc.selectNodes("/bookstore/book[0]") |
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]!!
注意:IE5和IE6把[0]作为第一节点,但是,W3C标准规定的第一个节点是[1]!!
Note: This is corrected in IE 6 SP2!
注意:IE6 SP2 已经更正了上述问题!
The following example selects the text from all the price nodes:
下述案例i将演示如何从所有的price节点选择文本:
xmlDoc.selectNodes("/bookstore/book/price/text()") |
The following example selects all the price nodes with a price higher than 35:
下述案例将演示如何选择price值大于35的price节点:
xmlDoc.selectNodes("/bookstore/book[price>35]/price") |
The following example selects all the title nodes with a price higher than 35:
下述案例将演示如何选择price属性值大于35的所有title节点:
xmlDoc.selectNodes("/bookstore/book[price>35]/title") |