VBScript SelectSingleNode Method
last modified April 9, 2025
The SelectSingleNode
method in VBScript is part of the XML DOM
(Document Object Model). It searches for the first node that matches a specified
XPath expression. This method is essential for parsing and extracting data from
XML documents. It returns a single node object or Nothing if no match is found.
SelectSingleNode
enables precise XML data extraction with XPath
queries. It's commonly used in configuration file processing and web services.
This tutorial covers SelectSingleNode
with practical examples to
demonstrate its usage in various scenarios.
SelectSingleNode Method Overview
The SelectSingleNode
method takes an XPath expression as its
parameter. It searches within the current node's descendants for a match. The
method is available on any XML DOM node object in VBScript scripting.
Key features include XPath 1.0 expression support and namespace-aware queries. It returns Nothing if no matching node is found. Understanding this method helps create robust XML processing scripts. Proper error handling is recommended when working with the results.
Basic XML Node Selection
This example demonstrates the simplest use of SelectSingleNode
to
find a node by its name. It shows loading an XML string and selecting a specific
element. The selected node's text content is then displayed.
Set xmlDoc = CreateObject("MSXML2.DOMDocument") xmlDoc.async = False xmlDoc.loadXML "<root><person><name>John Doe</name><age>30</age></person></root>" Set nameNode = xmlDoc.SelectSingleNode("//name") WScript.Echo nameNode.text ' Output: John Doe Set xmlDoc = Nothing
The script creates an XML DOM document and loads a simple XML string. It then
uses SelectSingleNode
with "//name" XPath to find the name element.
The node's text content is displayed. This shows basic XML navigation.
Selecting with Absolute Path
This example shows using an absolute XPath path to select a specific node. Absolute paths start from the document root. This approach is useful when you know the exact structure of your XML document.
Set xmlDoc = CreateObject("MSXML2.DOMDocument") xmlDoc.async = False xmlDoc.loadXML "<bookstore><book><title>VBScript Guide</title><price>29.99</price></book></bookstore>" Set priceNode = xmlDoc.SelectSingleNode("/bookstore/book/price") WScript.Echo priceNode.text ' Output: 29.99 Set xmlDoc = Nothing
The script loads a bookstore XML structure. It uses an absolute path "/bookstore/book/price" to directly access the price element. This method is precise but requires knowledge of the full document structure.
Selecting with Attribute Condition
SelectSingleNode
can select nodes based on attribute values using
XPath predicates. This example demonstrates finding a node where an attribute
matches a specific value. It's useful for filtering XML data.
Set xmlDoc = CreateObject("MSXML2.DOMDocument") xmlDoc.async = False xml = "<users><user id='101'>Alice</user><user id='102'>Bob</user></users>" xmlDoc.loadXML xml Set userNode = xmlDoc.SelectSingleNode("//user[@id='102']") WScript.Echo userNode.text ' Output: Bob Set xmlDoc = Nothing
The script loads user data with ID attributes. The XPath "//user[@id='102']" finds the user node where id equals 102. This demonstrates attribute-based selection, a powerful XML querying technique.
Selecting with Namespace
This example shows how to handle XML documents with namespaces when using
SelectSingleNode
. Namespaces require special handling in XPath
queries. The example demonstrates registering and using namespace prefixes.
Set xmlDoc = CreateObject("MSXML2.DOMDocument") xmlDoc.async = False xmlDoc.setProperty "SelectionNamespaces", "xmlns:ns='http://example.com'" xml = "<ns:root xmlns:ns='http://example.com'><ns:item>Data</ns:item></ns:root>" xmlDoc.loadXML xml Set itemNode = xmlDoc.SelectSingleNode("//ns:item") WScript.Echo itemNode.text ' Output: Data Set xmlDoc = Nothing
The script first sets the SelectionNamespaces property to define a prefix for the namespace. It then uses this prefix in the XPath query "//ns:item" to select the namespaced node. This is essential for working with namespace-aware XML documents.
Selecting with Complex XPath
This example demonstrates a more complex XPath expression with
SelectSingleNode
. It combines multiple conditions to precisely
select a specific node. This shows the full power of XPath in VBScript.
Set xmlDoc = CreateObject("MSXML2.DOMDocument") xmlDoc.async = False xml = "<products><product category='electronics'><name>TV</name><price>499</price></product>" & _ "<product category='furniture'><name>Chair</name><price>99</price></product></products>" xmlDoc.loadXML xml Set node = xmlDoc.SelectSingleNode("//product[price>100 and @category='electronics']/name") WScript.Echo node.text ' Output: TV Set xmlDoc = Nothing
The script loads product data and uses a complex XPath query. The expression finds electronics products priced over 100 and selects their name. This demonstrates combining attribute tests and value comparisons in XPath.
Source
In this article, we have explored the SelectSingleNode
method in
VBScript, covering its usage and practical applications. From simple node
selection to complex XPath queries, these examples demonstrate powerful XML
processing capabilities. With this knowledge, you can effectively parse and
extract data from XML documents in your VBScript projects.
Author
List all VBScript tutorials.