我的知识库

知识等于力量

导航

« 网站速度很慢问题解决的方法(TOMCAT性能调整) 在Linux上架设支持JSP+PHP的Web服务器 »

XPath Study

XPath Study XPath Introduction

XPath is a language for finding information in an XML document. XPath is used to navigate through elements and attributes in an XML document.
What You Should Already Know
Before you continue you should have a basic understanding of the following:
  • HTML / XHTML
  • XML / XML Namespaces

If you want to study these subjects first, find the tutorials on our Home page.
What is XPath?
  • XPath is a syntax for defining parts of an XML document
  • XPath uses path expressions to navigate in XML documents
  • XPath contains a library of standard functions
  • XPath is a major element in XSLT
  • XPath is a W3C Standard
XPath Path Expressions
XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system.
XPath Standard Functions
XPath includes over 100 built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more.
XPath is Used in XSLT
XPath is a major element in the XSLT standard. Without XPath knowledge you will not be able to create XSLT documents.
You can read more about XSLT in our XSLT tutorial.
XQuery and XPointer are both built on XPath expressions. XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators.
You can read more about XQuery in our XQuery tutorial.
XPath is a W3C Standard
XPath became a W3C Recommendation 16. November 1999.
XPath was designed to be used by XSLT, XPointer and other XML parsing software.
You can read more about the XPath standard in our W3C tutorial. XPath Nodes

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes.
XPath Terminology Nodes
In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes. XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node).
Look at the following XML document:




  <BR>  <author>J K. Rowling</author> <PRE></PRE><BR>  <year>2005</year><PRE></PRE><BR>  <price>29.99</price><PRE></PRE><BR></book><PRE></PRE><BR></bookstore><PRE></PRE></TD></TR></TBODY></TABLE><BR>Example of nodes in the XML document above: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><bookstore>  (document node)<PRE></PRE><BR><author>J K. Rowling</author>  (element node)<PRE></PRE><BR>></TD></TR></TBODY></TABLE>Atomic values <BR>Atomic values are nodes with no children or parent. <BR>Example of atomic values: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR>J K. Rowling<PRE></PRE><BR>"en"<PRE></PRE></TD></TR></TBODY></TABLE>Items <BR>Items are atomic values or nodes. <DIV class=MsoNormal align=center></DIV>Relationship of Nodes Parent <BR>Each element and attribute has one parent. <BR>In the following example; the book element is the parent of the title, author, year, and price: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><book><PRE></PRE><BR>  <title>Harry Potter

  J K. Rowling

  2005

  29.99

Children
Element nodes may have zero, one or more children.
In the following example; the title, author, year, and price elements are all children of the book element:


  Harry Potter

  J K. Rowling

  2005

  29.99

Siblings
Nodes that have the same parent.
In the following example; the title, author, year, and price elements are all siblings:


  Harry Potter

  J K. Rowling

  2005

  29.99

Ancestors
A node's parent, parent's parent, etc.
In the following example; the ancestors of the title element are the book element and the bookstore element:



  Harry Potter

  J K. Rowling

  2005

  29.99


Descendants
A node's children, children's children, etc.
In the following example; descendants of the bookstore element are the book, title, author, year, and price elements:



  Harry Potter

  J K. Rowling

  2005

  29.99


XPath Syntax

XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.
The XML Example Document
We will use the following XML document in the examples below.




  <BR>  <price>29.99</price><PRE></PRE><BR></book><PRE></PRE><BR><book><PRE></PRE><BR>  <title ><BR>  <price>39.95</price><PRE></PRE><BR></book><PRE></PRE><BR></bookstore><PRE></PRE></TD></TR></TBODY></TABLE><BR>  <DIV class=MsoNormal align=center></DIV>Selecting Nodes <BR>XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><B>Expression</B></TD> <TD vAlign=top><BR><B>Description</B></TD></TR> <TR> <TD vAlign=top><BR><EM>nodename</EM></TD> <TD vAlign=top><BR>Selects all child nodes of the node</TD></TR> <TR> <TD vAlign=top><BR>/</TD> <TD vAlign=top><BR>Selects from the root node</TD></TR> <TR> <TD vAlign=top><BR>//</TD> <TD vAlign=top><BR>Selects nodes in the document from the current node that match the selection no matter where they are </TD></TR> <TR> <TD vAlign=top><BR>.</TD> <TD vAlign=top><BR>Selects the current node</TD></TR> <TR> <TD vAlign=top><BR>..</TD> <TD vAlign=top><BR>Selects the parent of the current node</TD></TR> <TR> <TD vAlign=top><BR>@</TD> <TD vAlign=top><BR>Selects attributes</TD></TR></TBODY></TABLE>Examples <BR>In the table below we have listed some path expressions and the result of the expressions: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><B>Path Expression</B></TD> <TD vAlign=top><BR><B>Result</B></TD></TR> <TR> <TD vAlign=top><BR>bookstore</TD> <TD vAlign=top><BR>Selects all the child nodes of the bookstore element</TD></TR> <TR> <TD vAlign=top><BR>/bookstore</TD> <TD vAlign=top><BR>Selects the root element bookstore <BR><B>Note:</B> If the path starts with a slash ( / ) it always represents an absolute path to an element!</TD></TR> <TR> <TD vAlign=top><BR>bookstore/book</TD> <TD vAlign=top><BR>Selects all book elements that are children of bookstore</TD></TR> <TR> <TD vAlign=top><BR>//book</TD> <TD vAlign=top><BR>Selects all book elements no matter where they are in the document</TD></TR> <TR> <TD vAlign=top><BR>bookstore//book</TD> <TD vAlign=top><BR>Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element</TD></TR> <TR> <TD vAlign=top><BR>//@lang</TD> <TD vAlign=top><BR>Selects all attributes that are named lang</TD></TR></TBODY></TABLE><BR>  <DIV class=MsoNormal align=center></DIV>Predicates <BR>Predicates are used to find a specific node or a node that contains a specific value. <BR>Predicates are always embedded in square brackets. Examples <BR>In the table below we have listed some path expressions with predicates and the result of the expressions: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><B>Path Expression</B></TD> <TD vAlign=top><BR><B>Result</B></TD></TR> <TR> <TD vAlign=top><BR>/bookstore/book[1] </TD> <TD vAlign=top><BR>Selects the first book element that is the child of the bookstore element</TD></TR> <TR> <TD vAlign=top><BR>/bookstore/book[last()]</TD> <TD vAlign=top><BR>Selects the last book element that is the child of the bookstore element</TD></TR> <TR> <TD vAlign=top><BR>/bookstore/book[last()-1]</TD> <TD vAlign=top><BR>Selects the last but one book element that is the child of the bookstore element</TD></TR> <TR> <TD vAlign=top><BR>/bookstore/book[position()<3]</TD> <TD vAlign=top><BR>Selects the first two book elements that are children of the bookstore element</TD></TR> <TR> <TD vAlign=top><BR>//title[@lang]</TD> <TD vAlign=top><BR>Selects all the title elements that have an attribute named lang</TD></TR> <TR> <TD vAlign=top><BR>//title[@> </TD> <TD vAlign=top><BR>Selects all the title elements that have an attribute named lang with a value of 'eng'</TD></TR> <TR> <TD vAlign=top><BR>/bookstore/book[price>35.00]</TD> <TD vAlign=top><BR>Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00</TD></TR> <TR> <TD vAlign=top><BR>/bookstore/book[price>35.00]/title</TD> <TD vAlign=top><BR>Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00</TD></TR></TBODY></TABLE><BR>  <DIV class=MsoNormal align=center></DIV>Selecting Unknown Nodes <BR>XPath wildcards can be used to select unknown XML elements. <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><B>Wildcard</B></TD> <TD vAlign=top><BR><B>Description</B></TD></TR> <TR> <TD vAlign=top><BR>*</TD> <TD vAlign=top><BR>Matches any element node</TD></TR> <TR> <TD vAlign=top><BR>@*</TD> <TD vAlign=top><BR>Matches any attribute node</TD></TR> <TR> <TD vAlign=top><BR>node()</TD> <TD vAlign=top><BR>Matches any node of any kind</TD></TR></TBODY></TABLE>Examples <BR>In the table below we have listed some path expressions and the result of the expressions: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><B>Path Expression</B></TD> <TD vAlign=top><BR><B>Result</B></TD></TR> <TR> <TD vAlign=top><BR>/bookstore/*</TD> <TD vAlign=top><BR>Selects all the child nodes of the bookstore element</TD></TR> <TR> <TD vAlign=top><BR>//*</TD> <TD vAlign=top><BR>Selects all elements in the document</TD></TR> <TR> <TD vAlign=top><BR>//title[@*]</TD> <TD vAlign=top><BR>Selects all title elements which have any attribute</TD></TR></TBODY></TABLE><BR>  <DIV class=MsoNormal align=center></DIV>Selecting Several Paths <BR>By using the | operator in an XPath expression you can select several paths. Examples <BR>In the table below we have listed some path expressions and the result of the expressions: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><B>Path Expression</B></TD> <TD vAlign=top><BR><B>Result</B></TD></TR> <TR> <TD vAlign=top><BR>//book/title | //book/price</TD> <TD vAlign=top><BR>Selects all the title AND price elements of all book elements</TD></TR> <TR> <TD vAlign=top><BR>//title | //price</TD> <TD vAlign=top><BR>Selects all the title AND price elements in the document</TD></TR> <TR> <TD vAlign=top><BR>/bookstore/book/title | //price</TD> <TD vAlign=top><BR>Selects all the title elements of the book element of the bookstore element AND all the price elements in the document</TD></TR></TBODY></TABLE>XPath Axes <BR><v:shape id=_x0000_i1060><v:imagedata src="file:///C:\DOCUME~1\ZHANGY~1.CGO\LOCALS~1\Temp\msohtml1\04\clip_image001.gif" o:href="http://www.w3schools.com/images/btn_previous.gif"></v:imagedata></v:shape><v:shape id=_x0000_i1061><v:imagedata src="file:///C:\DOCUME~1\ZHANGY~1.CGO\LOCALS~1\Temp\msohtml1\04\clip_image002.gif" o:href="http://www.w3schools.com/images/btn_next.gif"></v:imagedata></v:shape> <DIV class=MsoNormal align=center></DIV>The XML Example Document <BR>We will use the following XML document in the examples below. <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><?xml version="1.0" encoding="ISO-8859-1"?><PRE></PRE><BR><bookstore><PRE></PRE><BR><book><PRE></PRE><BR>  <title ><BR>  <price>29.99</price><PRE></PRE><BR></book><PRE></PRE><BR><book><PRE></PRE><BR>  <title ><BR>  <price>39.95</price><PRE></PRE><BR></book><PRE></PRE><BR></bookstore><PRE></PRE></TD></TR></TBODY></TABLE><BR>  <DIV class=MsoNormal align=center></DIV>XPath Axes <BR>An axis defines a node-set relative to the current node. <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><B>AxisName</B></TD> <TD><BR><B>Result</B></TD></TR> <TR> <TD vAlign=top><BR>ancestor</TD> <TD vAlign=top><BR>Selects all ancestors (parent, grandparent, etc.) of the current node</TD></TR> <TR> <TD vAlign=top><BR>ancestor-or-self</TD> <TD vAlign=top><BR>Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself</TD></TR> <TR> <TD vAlign=top><BR>attribute</TD> <TD vAlign=top><BR>Selects all attributes of the current node</TD></TR> <TR> <TD vAlign=top><BR>child</TD> <TD vAlign=top><BR>Selects all children of the current node</TD></TR> <TR> <TD vAlign=top><BR>descendant</TD> <TD vAlign=top><BR>Selects all descendants (children, grandchildren, etc.) of the current node</TD></TR> <TR> <TD vAlign=top><BR>descendant-or-self</TD> <TD vAlign=top><BR>Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself</TD></TR> <TR> <TD vAlign=top><BR>following</TD> <TD vAlign=top><BR>Selects everything in the document after the closing tag of the current node</TD></TR> <TR> <TD vAlign=top><BR>following-sibling</TD> <TD vAlign=top><BR>Selects all siblings after the current node</TD></TR> <TR> <TD vAlign=top><BR>namespace</TD> <TD vAlign=top><BR>Selects all namespace nodes of the current node</TD></TR> <TR> <TD vAlign=top><BR>parent</TD> <TD vAlign=top><BR>Selects the parent of the current node</TD></TR> <TR> <TD vAlign=top><BR>preceding</TD> <TD vAlign=top><BR>Selects everything in the document that is before the start tag of the current node</TD></TR> <TR> <TD vAlign=top><BR>preceding-sibling</TD> <TD vAlign=top><BR>Selects all siblings before the current node</TD></TR> <TR> <TD vAlign=top><BR>self</TD> <TD vAlign=top><BR>Selects the current node</TD></TR></TBODY></TABLE><BR>  <DIV class=MsoNormal align=center></DIV>Location Path Expression <BR>A location path can be absolute or relative. <BR>An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more steps, each separated by a slash: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD vAlign=top><BR>An absolute location path:<PRE></PRE><BR>/step/step/...<PRE></PRE><BR>A relative location path:<PRE></PRE><BR>step/step/...<PRE></PRE></TD></TR></TBODY></TABLE><BR>Each step is evaluated against the nodes in the current node-set. <BR>A step consists of: <UL> <LI>an axis (defines the tree-relationship between the selected nodes and the current node) <LI>a node-test (identifies a node within an axis) <LI>zero or more predicates (to further refine the selected node-set) </LI></UL><BR>The syntax for a location step is: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD vAlign=top><BR>axisname::nodetest[predicate]<PRE></PRE></TD></TR></TBODY></TABLE>Examples <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><B>Example</B></TD> <TD><BR><B>Result</B></TD></TR> <TR> <TD vAlign=top><BR>child::book</TD> <TD vAlign=top><BR>Selects all book nodes that are children of the current node</TD></TR> <TR> <TD vAlign=top><BR>attribute::lang</TD> <TD vAlign=top><BR>Selects the lang attribute of the current node</TD></TR> <TR> <TD vAlign=top><BR>child::*</TD> <TD vAlign=top><BR>Selects all children of the current node</TD></TR> <TR> <TD vAlign=top><BR>attribute::*</TD> <TD vAlign=top><BR>Selects all attributes of the current node</TD></TR> <TR> <TD vAlign=top><BR>child::text()</TD> <TD vAlign=top><BR>Selects all text child nodes of the current node</TD></TR> <TR> <TD vAlign=top><BR>child::node()</TD> <TD vAlign=top><BR>Selects all child nodes of the current node</TD></TR> <TR> <TD vAlign=top><BR>descendant::book</TD> <TD vAlign=top><BR>Selects all book descendants of the current node</TD></TR> <TR> <TD vAlign=top><BR>ancestor::book</TD> <TD vAlign=top><BR>Selects all book ancestors of the current node</TD></TR> <TR> <TD vAlign=top><BR>ancestor-or-self::book</TD> <TD vAlign=top><BR>Selects all book ancestors of the current node - and the current as well if it is a book node</TD></TR> <TR> <TD vAlign=top><BR>child::*/child::price</TD> <TD vAlign=top><BR>Selects all price grandchildren of the current node</TD></TR></TBODY></TABLE>XPath Operators <BR><v:shape id=_x0000_i1062><v:imagedata src="file:///C:\DOCUME~1\ZHANGY~1.CGO\LOCALS~1\Temp\msohtml1\04\clip_image001.gif" o:href="http://www.w3schools.com/images/btn_previous.gif"></v:imagedata></v:shape><v:shape id=_x0000_i1063><v:imagedata src="file:///C:\DOCUME~1\ZHANGY~1.CGO\LOCALS~1\Temp\msohtml1\04\clip_image002.gif" o:href="http://www.w3schools.com/images/btn_next.gif"></v:imagedata></v:shape> <DIV class=MsoNormal align=center></DIV><BR><B>An XPath expression returns either a node-set, a string, a Boolean, or a number.</B> <DIV class=MsoNormal align=center><B></B></DIV>XPath Operators <BR>Below is a list of the operators that can be used in XPath expressions: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><B>Operator</B></TD> <TD><BR><B>Description</B></TD> <TD><BR><B>Example</B></TD> <TD><BR><B>Return value</B></TD></TR> <TR> <TD vAlign=top><BR>|</TD> <TD vAlign=top><BR>Computes two node-sets</TD> <TD vAlign=top><BR>//book | //cd</TD> <TD vAlign=top><BR>Returns a node-set with all book and cd elements</TD></TR> <TR> <TD vAlign=top><BR>+</TD> <TD vAlign=top><BR>Addition</TD> <TD vAlign=top><BR>6 + 4</TD> <TD vAlign=top><BR>10</TD></TR> <TR> <TD vAlign=top><BR>-</TD> <TD vAlign=top><BR>Subtraction</TD> <TD vAlign=top><BR>6 - 4</TD> <TD vAlign=top><BR>2</TD></TR> <TR> <TD vAlign=top><BR>*</TD> <TD vAlign=top><BR>Multiplication</TD> <TD vAlign=top><BR>6 * 4</TD> <TD vAlign=top><BR>24</TD></TR> <TR> <TD vAlign=top><BR>div</TD> <TD vAlign=top><BR>Division</TD> <TD vAlign=top><BR>8 div 4</TD> <TD vAlign=top><BR>2</TD></TR> <TR> <TD vAlign=top><BR>=</TD> <TD vAlign=top><BR>Equal</TD> <TD vAlign=top><BR>price=9.80</TD> <TD vAlign=top><BR>true if price is 9.80<B>false if price is 9.90</B></TD></TR> <TR> <TD vAlign=top><BR>!=</TD> <TD vAlign=top><BR>Not equal</TD> <TD vAlign=top><BR>price!=9.80</TD> <TD vAlign=top><BR>true if price is 9.90<B>false if price is 9.80</B></TD></TR> <TR> <TD vAlign=top><BR>< </TD> <TD vAlign=top><BR>Less than</TD> <TD vAlign=top><BR>price<9.80</TD> <TD vAlign=top><BR>true if price is 9.00<B>false if price is 9.80</B></TD></TR> <TR> <TD vAlign=top><BR><=</TD> <TD vAlign=top><BR>Less than or equal to</TD> <TD vAlign=top><BR>price<=9.80</TD> <TD vAlign=top><BR>true if price is 9.00<B>false if price is 9.90</B></TD></TR> <TR> <TD vAlign=top><BR>> </TD> <TD vAlign=top><BR>Greater than</TD> <TD vAlign=top><BR>price>9.80</TD> <TD vAlign=top><BR>true if price is 9.90<B>false if price is 9.80</B></TD></TR> <TR> <TD vAlign=top><BR>>=</TD> <TD vAlign=top><BR>Greater than or equal to</TD> <TD vAlign=top><BR>price>=9.80</TD> <TD vAlign=top><BR>true if price is 9.90<B>false if price is 9.70</B></TD></TR> <TR> <TD vAlign=top><BR>or</TD> <TD vAlign=top><BR>or</TD> <TD vAlign=top><BR>price=9.80 or price=9.70</TD> <TD vAlign=top><BR>true if price is 9.80<B>false if price is 9.50</B></TD></TR> <TR> <TD vAlign=top><BR>and</TD> <TD vAlign=top><BR>and </TD> <TD vAlign=top><BR>price>9.00 and price<9.90</TD> <TD vAlign=top><BR>true if price is 9.80<B>false if price is 8.50</B></TD></TR> <TR> <TD vAlign=top><BR>mod</TD> <TD vAlign=top><BR>Modulus (division remainder)</TD> <TD vAlign=top><BR>5 mod 2</TD> <TD vAlign=top><BR>1</TD></TR></TBODY></TABLE>XPath Examples <BR><v:shape id=_x0000_i1064><v:imagedata src="file:///C:\DOCUME~1\ZHANGY~1.CGO\LOCALS~1\Temp\msohtml1\04\clip_image001.gif" o:href="http://www.w3schools.com/images/btn_previous.gif"></v:imagedata></v:shape><v:shape id=_x0000_i1065><v:imagedata src="file:///C:\DOCUME~1\ZHANGY~1.CGO\LOCALS~1\Temp\msohtml1\04\clip_image002.gif" o:href="http://www.w3schools.com/images/btn_next.gif"></v:imagedata></v:shape> <DIV class=MsoNormal align=center></DIV><BR><B>Let's try to learn some basic XPath syntax by looking at some examples.</B> <DIV class=MsoNormal align=center><B></B></DIV>The XML Example Document <BR>We will use the following XML document in the examples below. <BR>"books.xml": <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR><?xml version="1.0" encoding="ISO-8859-1"?><PRE></PRE><BR><bookstore><PRE></PRE><BR><book category="COOKING"><PRE></PRE><BR>  <title ><BR>  <author>Giada De Laurentiis</author><PRE></PRE><BR>  <year>2005</year><PRE></PRE><BR>  <price>30.00</price><PRE></PRE><BR></book><PRE></PRE><BR><book category="CHILDREN"><PRE></PRE><BR>  <title ><BR>  <author>J K. Rowling</author><PRE></PRE><BR>  <year>2005</year><PRE></PRE><BR>  <price>29.99</price><PRE></PRE><BR></book><PRE></PRE><BR><book category="WEB"><PRE></PRE><BR>  <title ><BR>  <author>James McGovern</author><PRE></PRE><BR>  <author>Per Bothner</author><PRE></PRE><BR>  <author>Kurt Cagle</author><PRE></PRE><BR>  <author>James Linn</author><PRE></PRE><BR>  <author>Vaidyanathan Nagarajan</author><PRE></PRE><BR>  <year>2003</year><PRE></PRE><BR>  <price>49.99</price><PRE></PRE><BR></book><PRE></PRE><BR><book category="WEB"><PRE></PRE><BR>  <title ><BR>  <author>Erik T. Ray</author><PRE></PRE><BR>  <year>2003</year><PRE></PRE><BR>  <price>39.95</price><PRE></PRE><BR></book><PRE></PRE><BR></bookstore><PRE></PRE></TD></TR></TBODY></TABLE><BR>View the "books.xml" file in your browser. <DIV class=MsoNormal align=center></DIV>Selecting Nodes <BR>We will use the Microsoft XMLDOM object to load the XML document and the selectNodes() function to select nodes from the XML document: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR>set xmlDoc=CreateObject("Microsoft.XMLDOM")<PRE></PRE><BR>xmlDoc.async="false"<PRE></PRE><BR>xmlDoc.load("books.xml")<PRE></PRE><BR>xmlDoc.selectNodes(<EM>path expression</EM>)<PRE></PRE></TD></TR></TBODY></TABLE><BR>  <DIV class=MsoNormal align=center></DIV>Select all book Nodes <BR>The following example selects all the book nodes under the bookstore element: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR>xmlDoc.selectNodes("/bookstore/book")<PRE></PRE></TD></TR></TBODY></TABLE><BR>If you have IE 5 or higher you can try it yourself. <DIV class=MsoNormal align=center></DIV>Select the First book Node <BR>The following example selects only the first book node under the bookstore element: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR>xmlDoc.selectNodes("/bookstore/book[0]")<PRE></PRE></TD></TR></TBODY></TABLE><BR>If you have IE 5 or higher you can try it yourself. <BR><B>Note:</B> IE 5 and 6 has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!! <BR><B>Note:</B> This is corrected in IE 6 SP2! <DIV class=MsoNormal align=center></DIV>Select the prices <BR>The following example selects the text from all the price nodes: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR>xmlDoc.selectNodes("/bookstore/book/price/text()") <PRE></PRE></TD></TR></TBODY></TABLE><BR>If you have IE 5 or higher you can try it yourself. <DIV class=MsoNormal align=center></DIV>Selecting price Nodes with Price>35 <BR>The following example selects all the price nodes with a price higher than 35: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR>xmlDoc.selectNodes("/bookstore/book[price>35]/price") <PRE></PRE></TD></TR></TBODY></TABLE><BR>If you have IE 5 or higher you can try it yourself. <DIV class=MsoNormal align=center></DIV>Selecting title Nodes with Price>35 <BR>The following example selects all the title nodes with a price higher than 35: <BR> <TABLE class=MsoNormalTable border=1> <TBODY> <TR> <TD><BR>xmlDoc.selectNodes("/bookstore/book[price>35]/title") <PRE></PRE></TD></TR></TBODY></TABLE><BR>If you have IE 5 or higher you can try it yourself. <BR></div> </div> </div> <div id="divSidebar"> <div class="function" id="divSearchPanel"> <h3>Search</h3> <ul> <li> <form method="post" action="http://www.duduwolf.com/wikicmd.asp?act=Search"> <input type="text" name="edtSearch" id="edtSearch" size="12" /> <input type="submit" value="提交" name="btnPost" id="btnPost" /> </form> </li> </ul> </div> <div class="function" id="divHots"> <h3>热门文章</h3> <ul> <!-- #include file="../include/comments.asp" --> </ul> </div> <div class="function" id="divPrevious"> <h3>最新文章</h3> <ul> <!-- #include file="../include/previous.asp" --> </ul> </div> </div> <div id="divBottom"> <h3 id="BlogPowerBy">Powered By <a href="http:/www.duduwolf.com/" title="duduwolf's wiki">duduwolf's wiki 1.0</a></h3> <h2 id="BlogCopyRight">Copyright 1999-2006 duduwolf.com Some Rights Reserved.<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript">_uacct = "UA-95152-1";urchinTracker();</script><script src="http://www.duduwolf.com/script/astrack.js" type="text/javascript"></script><script language='javascript' type='text/javascript' src='http://js.users.51.la/350481.js'></script><noscript><a href='http://www.51.la/?350481' target='_blank'><img alt='我要啦免费统计' src='http://img.users.51.la/350481.asp' style='border:none' /></a></noscript></h2> </div> </div> </body> </html>