Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as IXMLDOMNodeList.
var objXMLDOMNodeList = oXMLDOMNode.selectNodes(expression);
An object. Returns the collection of nodes selected by applying the given pattern-matching operation. If no nodes are selected, returns an empty collection.
The following script example creates an IXMLDOMNodeList object containing the nodes specified by the expression parameter (for example, all the <xsl:template> nodes in an XSLT style sheet). It then displays the number of nodes contained in the node list.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var objNodeList;
xmlDoc.async = false;
xmlDoc.load("hello.xsl");
if (xmlDoc.parseError.errorCode <> 0) {
var myErr = xmlDoc.parseError;
alert("You have error " + myErr.reason);
} else {
xmlDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
xmlDoc.setProperty("SelectionLanguage", "XPath");
objNodeList = xmlDoc.documentElement.selectNodes("//xsl:template");
alert(objNodeList.length);
}
Set objXMLDOMNodeList = oXMLDOMNode.selectNodes(expression)
An object. Returns the collection of nodes selected by applying the given pattern-matching operation. If no nodes are selected, returns an empty collection.
The following Microsoft® Visual Basic® example creates an IXMLDOMNodeList object containing the nodes specified by the expression parameter (for example, all the <xsl:template> nodes in an XSLT style sheet). It then displays the number of nodes contained in the node list.
Dim xmlDoc As New Msxml2.DOMDocument50
Dim objNodeList As IXMLDOMNodeList
xmlDoc.async = False
xmlDoc.Load "hello.xsl"
If (xmlDoc.parseError.errorCode <> 0) Then
Dim myErr
Set myErr = xmlDoc.parseError
MsgBox("You have error " & myErr.reason)
Else
xmlDoc.setProperty "SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"
xmlDoc.setProperty "SelectionLanguage", "XPath"
Set objNodeList = xmlDoc.documentElement.selectNodes("//xsl:template")
MsgBox objNodeList.length
End If
HRESULT selectNodes(
BSTR expression,
IXMLDOMNodeList **resultList);
resultList parameter is Null.The following Microsoft® Visual C++ example creates an IXMLDOMNodeList object containing the nodes specified by the expression parameter (for example, all the <xsl:template> nodes in an XSLT stylesheet). It then displays the number of nodes contained in the node list.
XpathSelectNodes.cpp
#include <stdio.h>
#import <msxml5.dll>
using namespace MSXML2;
void dump_com_error(_com_error &e);
int main(int argc, char* argv[])
{
CoInitialize(NULL);
try{
IXMLDOMDocument2Ptr pXMLDoc = NULL;
HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument50));
// Set parser property settings
pXMLDoc->async = VARIANT_FALSE;
// Load the sample XML file
hr = pXMLDoc->load("hello.xsl");
// If document does not load report the parse error
if(hr!=VARIANT_TRUE)
{
IXMLDOMParseErrorPtr pError;
pError = pXMLDoc->parseError;
_bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline())
+ _bstr_t("\n")+ _bstr_t(pError->Getreason());
MessageBox(NULL,parseError, "Parse Error",MB_OK);
return 0;
}
// Otherwise, build node list using SelectNodes
// and returns its length as console output
else
pXMLDoc->setProperty("SelectionLanguage", "XPath");
// Set the selection namespace URI if the nodes
// you wish to select later use a namespace prefix
pXMLDoc->setProperty("SelectionNamespaces",
"xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
IXMLDOMElementPtr pXMLDocElement = NULL;
pXMLDocElement = pXMLDoc->documentElement;
IXMLDOMNodeListPtr pXMLDomNodeList = NULL;
pXMLDomNodeList = pXMLDocElement->selectNodes("//xsl:template");
int count = 0;
count = pXMLDomNodeList->length;
printf("The number of <xsl:template> nodes is %i.\n", count);
}
catch(_com_error &e)
{
dump_com_error(e);
}
return 0;
}
void dump_com_error(_com_error &e)
{
printf("Error\n");
printf("\a\tCode = %08lx\n", e.Error());
printf("\a\tCode meaning = %s", e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
}
Try It!
For more information, see Set Up My Visual C++ Project.
Results and Output
When run successfully using the sample code and files, the application will return as standard console output the following:
The number of <xsl:template> nodes is 2.
For more information about using the selectNodes method with namespaces, see the setProperty Method topic.
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 World Wide Web Consortium (W3C) Document Object Model (DOM).
Note Previously, in MSXML 3.0 and earlier versions, the selection object created by calling theselectNodesmethod would gradually calculate the node-set. If the DOM tree was modified, while theselectNodescall was still actively iterating its contents, the behavior could potentially change the nodes that were selected or returned. In MSXML 4.0 and later, the node-set result is 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.
Using XSLT with DOM or SAX | selectSingleNode method | IXMLDOMNodeList | XPath Syntax | Patterns
Applies to: IXMLDOMAttribute | IXMLDOMCDATASection | IXMLDOMCharacterData | IXMLDOMComment | DOMDocument | IXMLDOMDocumentFragment | IXMLDOMDocumentType | IXMLDOMElement | IXMLDOMEntity | IXMLDOMEntityReference | IXMLDOMNode | IXMLDOMNotation | IXMLDOMProcessingInstruction | IXMLDOMText