selectSingleNode Method
Applies the specified pattern-matching operation to this node's context and returns the first matching node.
[Script]
Script Syntax
var objXMLDOMNode = oXMLDOMNode.selectSingleNode(queryString);
Parameters
- queryString
- 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 first node that matches the given pattern-matching operation. If no nodes match the expression, returns a null value.
Example
The following script example creates an IXMLDOMNode object and sets it to the first instance of an AUTHOR node with a BOOK parent. It then displays the text of the node.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
var currNode;
xmlDoc.async = false;
xmlDoc.load("books.xml");
xmlDoc.setProperty("SelectionLanguage", "XPath");
currNode = xmlDoc.selectSingleNode("//book/author");
alert(currNode.text);
[Visual Basic]
Visual Basic Syntax
Set objXMLDOMNode = oXMLDOMNode.selectSingleNode(queryString)
Parameters
- queryString
- 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 first node that matches the given pattern-matching operation. If no nodes match the expression, object value is Nothing.
Example
The following Microsoft® Visual Basic® example creates an IXMLDOMNode object and sets it to the first instance of an AUTHOR node with a BOOK parent. It then displays the text of the node.
Dim xmlDoc As New Msxml2.DOMDocument40
Dim currNode As IXMLDOMNode
xmlDoc.async = False
xmlDoc.Load ("books.xml")
xmlDoc.setProperty "SelectionLanguage", "XPath"
Set currNode = xmlDoc.selectSingleNode("//book/author")
MsgBox currNode.Text
[C/C++]
C/C++ Syntax
HRESULT selectSingleNode(
BSTR queryString,
IXMLDOMNode **resultNode);
Parameters
- queryString [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.
- resultNode [out, retval]
- The first node that is selected by the given pattern-matching operation. If no nodes match the expression, returns a null value.
C/C++ Return Values
- S_OK
- The value returned if successful.
- S_FALSE
- The value returned if there is no match.
- E_INVALIDARG
- The value returned if resultNode is a null value.
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.
This member is an extension of the Worldwide Web Consortium (W3C) Document Object Model (DOM).
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 | selectNodes Method | setProperty Method | XPath Syntax
Applies to: IXMLDOMAttribute | IXMLDOMCDATASection | IXMLDOMCharacterData | IXMLDOMComment | DOMDocument | IXMLDOMDocumentFragment | IXMLDOMDocumentType | IXMLDOMElement | IXMLDOMEntity | IXMLDOMEntityReference | IXMLDOMNode | IXMLDOMNotation | IXMLDOMProcessingInstruction | IXMLDOMText