共用方式為


selectNodes Method

 

Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as IXMLDOMNodeList.

JScript Syntax

var objXMLDOMNodeList = oXMLDOMNode.selectNodes(expression);  

Parameters

expression
A string specifying an XPath expression.

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 <xsl:template> nodes in an XSLT style sheet). It then displays the number of nodes contained in the node list.

Note

You can use hello.xsl in the Hello World! (XSLT) topic topic to run this sample code.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
var objNodeList;
xmlDoc.async = false;
xmlDoc.load("hello.xsl");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   WScript.Echo("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");
   WScript.Echo(objNodeList.length);
}

Output

2

C/C++ Syntax

HRESULT selectNodes(  
    BSTR expression,  
    IXMLDOMNodeList **resultList);  

Parameters

expression[in]
A string specifying an XPath expression.

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.

Return Values

S_OK
The value returned if successful.

E_INVALIDARG
The value returned if the resultList parameter is Null.

Example

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 �msxml6.h�
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(DOMDocument60));

    // 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!
  1. Set up a new empty project in Visual C++ for a Win32 console application.

    For more information, see Set Up My Visual C++ Project.

  2. Copy the XpathSelectNodes.cpp file and paste it in as code for a new Visual C/C++ source file in your project.

  3. Copy the hello.xsl file from the Hello, World! (XSLT) tutorial application and paste and save it as a resource file in the same folder as your project.

  4. Build and execute the project as a console EXE application.

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.

Remarks

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 the selectNodes method would gradually 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 6., 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.

Versioning

Implemented in: MSXML 3.0 and MSXML 6.0

See Also

Using XSLT with the DOM or SAX
selectSingleNode Method
IXMLDOMNodeList
IXMLDOMAttribute
IXMLDOMCDATASection
IXMLDOMCharacterData
IXMLDOMComment
IXMLDOMDocument-DOMDocument
IXMLDOMDocumentFragment
IXMLDOMDocumentType
IXMLDOMElement
IXMLDOMEntity
IXMLDOMEntityReference
IXMLDOMNode
IXMLDOMNotation
IXMLDOMProcessingInstruction
IXMLDOMText