VBScript lastChild Property
last modified April 9, 2025
The lastChild property in VBScript is part of the Document Object
Model (DOM). It returns the last child node of a specified node. This property
is read-only and commonly used in XML document traversal. It helps navigate
hierarchical node structures efficiently.
lastChild returns Nothing if the node has no children. It's useful
for accessing the final element in a node collection. This tutorial covers
lastChild with practical examples to demonstrate its usage in
various scenarios.
lastChild Property Overview
The lastChild property belongs to the DOM Node interface. It
provides access to a node's last child without using childNodes collection. The
property is available when working with XML documents in VBScript.
Key features include direct access to the last node and null return for empty nodes. It works with all node types including elements and text nodes. Understanding this property helps create efficient XML processing scripts.
Basic lastChild Access
This example demonstrates the simplest use of lastChild to access
the last child of an XML element. It shows how to load an XML document and
navigate to the last child node. The example outputs the node name and value.
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.loadXML "<root><item>First</item><item>Second</item><item>Last</item></root>"
Set lastNode = xmlDoc.documentElement.lastChild
WScript.Echo "Node name: " & lastNode.nodeName & ", Value: " & lastNode.text
Set xmlDoc = Nothing
The script creates an XML document with three items. lastChild
accesses the final "item" node. The output shows the node name ("item") and its
text content ("Last"). This demonstrates basic last node access.
Checking for No Children
This example shows how to handle cases where a node has no children.
lastChild returns Nothing when there are no child nodes. The script
demonstrates proper null checking before accessing node properties.
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.loadXML "<root><emptyNode/></root>"
Set emptyNode = xmlDoc.documentElement.firstChild
Set lastChild = emptyNode.lastChild
If lastChild Is Nothing Then
WScript.Echo "No child nodes found"
Else
WScript.Echo "Last child: " & lastChild.nodeName
End If
Set xmlDoc = Nothing
The script checks if lastChild returns Nothing for an empty node.
This is important to avoid runtime errors when accessing node properties. The
example outputs "No child nodes found" as expected.
Accessing Nested Last Child
This example demonstrates accessing the last child in a nested XML structure. It
shows how to chain lastChild calls to navigate deep hierarchies.
The script retrieves the innermost last node.
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.loadXML "<root><group><item>A</item><item>B</item></group><group><item>C</item></group></root>"
' Get last group, then its last item
Set lastGroup = xmlDoc.documentElement.lastChild
Set lastItem = lastGroup.lastChild
WScript.Echo "Last item in last group: " & lastItem.text
Set xmlDoc = Nothing
The script first gets the last "group" node, then its last "item" node. This two-step navigation is common in XML processing. The output shows "C" as the last item in the last group.
Comparing firstChild and lastChild
This example compares firstChild and lastChild
properties. It demonstrates accessing both ends of a node collection. The script
shows the difference between these navigation properties.
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.loadXML "<colors><color>Red</color><color>Green</color><color>Blue</color></colors>"
Set first = xmlDoc.documentElement.firstChild
Set last = xmlDoc.documentElement.lastChild
WScript.Echo "First color: " & first.text
WScript.Echo "Last color: " & last.text
Set xmlDoc = Nothing
The script loads an XML document with color values. It outputs both the first and last color nodes. This demonstrates how to access opposite ends of a node collection using these properties.
Processing Mixed Content Nodes
This example shows lastChild behavior with mixed content nodes. XML
elements can contain both elements and text nodes. The script demonstrates
identifying the actual last child in such cases.
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.loadXML "<book>Introduction<chapter>1</chapter><chapter>2</chapter></book>"
Set lastChild = xmlDoc.documentElement.lastChild
WScript.Echo "Last child node type: " & lastChild.nodeType
WScript.Echo "Last child value: " & lastChild.text
Set xmlDoc = Nothing
The XML contains both text ("Introduction") and element nodes. The script shows
that lastChild correctly returns the last chapter node. Node type 1
indicates an element node, and its text value is displayed.
Source
In this article, we have explored the lastChild property in
VBScript, covering its usage and practical applications. From basic node access
to complex nested structures, these examples demonstrate efficient XML
navigation. With this knowledge, you can enhance your XML processing scripts
with robust node traversal.
Author
List all VBScript tutorials.