ZetCode

VBScript SelectNodes Method

last modified April 9, 2025

The SelectNodes method in VBScript is part of the XML DOM (Document Object Model). It allows querying XML documents using XPath expressions to select matching nodes. This powerful method enables efficient XML data extraction and manipulation in VBScript applications.

SelectNodes returns a node list containing all matching elements. It's commonly used for parsing configuration files, web services responses, and data exchange formats. This tutorial covers SelectNodes with practical examples to demonstrate its usage.

SelectNodes Method Overview

The SelectNodes method takes an XPath expression as its parameter. It searches the XML document and returns all nodes matching the expression. The method is available on any XML DOM node object in VBScript.

Key features include complex query capabilities through XPath syntax. It can search by element name, attribute values, or hierarchical relationships. SelectNodes is case-sensitive and requires proper XML namespace handling when applicable.

Basic Node Selection

This example demonstrates the simplest use of SelectNodes to select all elements with a specific tag name. It loads an XML document from a string and queries for all "book" elements. The results are then processed in a loop.

basic_selectnodes.vbs
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<library><book><title>VBScript Guide</title></book>" & _
               "<book><title>XML Mastery</title></book></library>"

Set books = xmlDoc.SelectNodes("//book")
For Each book In books
    WScript.Echo book.SelectSingleNode("title").text
Next

Set xmlDoc = Nothing

The script creates an XML DOM document and loads sample XML data. The XPath expression "//book" selects all book elements in the document. The loop then extracts and displays each book's title. This shows basic node selection and navigation.

Selecting Nodes by Attribute

This example shows how to select nodes based on attribute values using SelectNodes. The XPath expression filters elements with specific attribute conditions. This is useful for finding specific data in XML documents.

attribute_selection.vbs
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<users><user role='admin'>Alice</user>" & _
               "<user role='user'>Bob</user><user role='admin'>Charlie</user></users>"

Set admins = xmlDoc.SelectNodes("//user[@role='admin']")
For Each admin In admins
    WScript.Echo admin.text & " is an admin"
Next

Set xmlDoc = Nothing

The script loads XML with user elements having role attributes. The XPath "//user[@role='admin']" selects only users with admin role. The square brackets enclose the attribute condition. This demonstrates attribute-based filtering.

Hierarchical Node Selection

This example demonstrates selecting nodes based on their hierarchical position in the XML document. The XPath expression navigates through parent-child relationships to find specific nodes. This shows structured document traversal.

hierarchical_selection.vbs
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<departments><dept name='IT'>" & _
               "<employee>John</employee><employee>Sarah</employee></dept>" & _
               "<dept name='HR'><employee>Mike</employee></dept></departments>"

Set itEmployees = xmlDoc.SelectNodes("//dept[@name='IT']/employee")
For Each emp In itEmployees
    WScript.Echo "IT employee: " & emp.text
Next

Set xmlDoc = Nothing

The script selects only employees within the IT department. The XPath expression first finds the dept element with name='IT', then selects its employee children. This two-step navigation shows hierarchical querying capabilities.

Selecting Multiple Node Types

This example shows how to select different types of nodes with a single XPath expression. The pipe character (|) acts as a union operator in XPath. This allows combining multiple queries into one result set.

multiple_types.vbs
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.loadXML "<config><setting name='timeout'>30</setting>" & _
               "<log level='debug'/><server port='8080'/></config>"

Set nodes = xmlDoc.SelectNodes("//setting | //log | //server")
For Each node In nodes
    If node.nodeName = "setting" Then
        WScript.Echo "Setting: " & node.text
    Else
        WScript.Echo "Element: " & node.nodeName
    End If
Next

Set xmlDoc = Nothing

The script selects setting, log, and server elements with one query. The XPath "//setting | //log | //server" combines three separate queries. The loop then handles each node type differently based on its nodeName property.

Using Namespaces with SelectNodes

This example demonstrates SelectNodes with XML namespaces, which requires special handling. Namespace prefixes must be declared and used in the XPath expression. This shows how to work with namespace-qualified XML.

namespace_selection.vbs
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
xmlDoc.setProperty "SelectionNamespaces", "xmlns:ns='http://example.com'"
xmlDoc.loadXML "<ns:data xmlns:ns='http://example.com'>" & _
               "<ns:item>Value1</ns:item><ns:item>Value2</ns:item></ns:data>"

Set items = xmlDoc.SelectNodes("//ns:item")
For Each item In items
    WScript.Echo item.text
Next

Set xmlDoc = Nothing

The script first sets the SelectionNamespaces property to declare the namespace prefix. The XML document uses the ns prefix for all elements. The XPath expression must include this prefix to match nodes. This ensures proper namespace-aware querying.

Source

XML DOM Documentation

In this article, we have explored the SelectNodes method in VBScript, covering its usage and practical applications. From basic node selection to complex namespace handling, these examples demonstrate powerful XML processing. With this knowledge, you can effectively parse and manipulate XML data in your VBScript applications.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all VBScript tutorials.