XPath in IE

Microsoft's implementation of XPath is a part of MSXML 3.0 and later. If you are using any

version of Windows XP, or have IE 6.0 or higher installed, then your computer has this

capability. If not, you will need to download and install the latest MSXML package.

Microsoft chose to implement two methods that select nodes based on XPath expressions. The first, selectSingleNode(), returns the first node within its context that matches the

expression.

 For example:

var oFirstAuthor = oXmlDom.documentElement.selectSingleNode("book/author");

This code returns the first <author/> element that is a child of a <book/> element in the

context of documentElement. The result of this is the following node:

<author>Nicholas C. Zakas, Jeremy McPeak, Joe Fawcett</author>

The second method in Microsoft's XPath implementation is selectNodes(). This method

returns a NodeList, a collection of all nodes that match the pattern in the XPath expression:

var cAuthors = oXmlDom.documentElement.selectNodes("book/author");

As you may have guessed, all <author/> elements with a parent of <book/> in the context of

the document element are returned. If the pattern cannot be matched in the document, a

NodeList is still returned but it has a length of 0. It is a good idea to check the length of a

returned NodeList before attempting to use it:

var cAuthors = oXmlDom.documentElement.selectNodes("book/author");

if (cAuthors.length > 0) {

 //Do something

}