selectNodes Method
Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as IXMLDOMNodeList.
[Script]
Script Syntax
var objXMLDOMNodeList = oXMLDOMNode.selectNodes(expression);
Parameters
- expression
- A string specifying the pattern-matching operation to be applied. This string is an XPath expression only if the setProperty method has set the SelectionLanguage internal property (flag) to
"XPath". Otherwise, this string is an XSL Patterns query.
Return Value
An object. Returns the collection of nodes selected by applying the given pattern-matching operation. If no nodes are selected, returns an empty collection.
Example
The following script example creates an IXMLDOMNodeList object containing the nodes specified by the expression parameter (for example, all the BOOK nodes). It then displays the number of nodes contained in the node list.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
var objNodeList;
xmlDoc.async = false;
xmlDoc.load("books.xml");
xmlDoc.setProperty("SelectionLanguage", "XPath");
objNodeList = xmlDoc.documentElement.selectNodes("//book");
alert(objNodeList.length);
[Visual Basic]
Visual Basic Syntax
Set objXMLDOMNodeList = oXMLDOMNode.selectNodes(expression)
Parameters
- expression
- A string specifying the pattern-matching operation to be applied. This string is an XPath expression only if the setProperty method has set the SelectionLanguage internal property (flag) to
"XPath". Otherwise, this string is an XSL Patterns query.
Return Value
An object. Returns the collection of nodes selected by applying the given pattern-matching operation. If no nodes are selected, returns an empty collection.
Example
The following Microsoft® Visual Basic® example creates an IXMLDOMNodeList object containing the nodes specified by the expression parameter (for example, all the BOOK nodes). It then displays the number of nodes contained in the node list.
Dim xmlDoc As New Msxml2.DOMDocument40
Dim objNodeList As IXMLDOMNodeList
xmlDoc.async = False
xmlDoc.Load ("books.xml")
xmlDoc.setProperty "SelectionLanguage", "XPath"
Set objNodeList = xmlDoc.documentElement.selectNodes("//book")
MsgBox objNodeList.length
[C/C++]
C/C++ Syntax
HRESULT selectNodes(
BSTR expression,
IXMLDOMNodeList **resultList);
Parameters
- expression [in]
- A string specifying the pattern-matching operation to be applied. This string is an XPath expression only if the setProperty method has set the SelectionLanguage internal property (flag) to
"XPath". Otherwise, this string is an XSL Patterns query.
- resultList [out, retval]
- The list of nodes selected by applying the given pattern-matching operation. If no nodes are selected, returns an empty node list.
C/C++ Return Values
- S_OK
- The value returned if successful.
- E_INVALIDARG
- The value returned if resultList is Null.
Remarks
The selectSingleNode method is similar to the selectNodes method, but returns only the first matching node rather than the list of all matching nodes.
IXMLDOMNodeList is live and immediately reflects changes to the nodes that appear in the list.
This member is an extension of the Worldwide Web Consortium (W3C) Document Object Model (DOM).
Note Previously, the Selection object created by calling the selectNodes method would lazily calculate the node-set. If the DOM tree was modified, while the selectNodes call was still actively iterating its contents, the behavior could potentially change the nodes that were selected or returned. In MSXML 4.0, the node-set result is now fully calculated at the time of selection. This ensures that the iteration is simple and predictable. In rare instances, this change might impact legacy code written to accommodate previous behavior.
To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button
in the upper-left corner of the page.
See Also
Using XSLT with DOM or SAX | setProperty Method | selectSingleNode method | IXMLDOMNodeList | XPath Syntax | Patterns
Applies to: IXMLDOMAttribute | IXMLDOMCDATASection | IXMLDOMCharacterData | IXMLDOMComment | DOMDocument | IXMLDOMDocumentFragment | IXMLDOMDocumentType | IXMLDOMElement | IXMLDOMEntity | IXMLDOMEntityReference | IXMLDOMNode | IXMLDOMNotation | IXMLDOMProcessingInstruction | IXMLDOMText