1

Hi I have written a program in VBScript, now I want to replace For Each loop with a For loop.

For Each node In objMSXML.selectNodes(sXPath)                   
    Set li = document.createElement("li")
    li.innerText = i & " " & node.text
    ul.appendChild li
    i = i +1
Next

I have not been able to figure it out as I also need to know the number of nodes returned from Xpath.

2
  • Why do you want to use a For instead of a For Each? If it's because you want to workout the number of nodes you don't need to the IXMLDOMNodeList is a collection so just use the length property like this NumOfNodes = objMSXML.selectNodes(sXPath).length to retrieve the number of nodes. Commented Mar 17, 2016 at 7:22
  • This is a duplicate anyway - XPath count in VBScript - See How to Ask Commented Mar 17, 2016 at 9:29

2 Answers 2

3

Following on from my initial comment.

The selectNodes() method returns a IXMLDOMNodeList object which is a NodeList collection.

As with any collection it contains members common to collection objects

Properties

  • length - Number of Nodes in the collection

Methods

  • item - Access to individual Nodes within the collection

For full list of member properties and methods see IXMLDOMNodeList Members

There should be no logical reason why you need to change your For Each to a For loop as you can just use the length property to retrieve the number of Nodes in the collection.

Option Explicit

Dim node, nodes, li, i, sXPath, NumberOfNodes
Set nodes = objMSXML.selectNodes(sXPath)
'Retrieve number of Nodes
NumberOfNodes = nodes.length
For Each node In nodes                   
    Set li = document.createElement("li")
    li.innerText = i & " " & node.text
    ul.appendChild li
    i = i +1
Next

Useful Links

Sign up to request clarification or add additional context in comments.

1 Comment

What was the reason for un-accepting? Does the solution not work is there a problem with the code?
2

The node list returned by .SelectNodes() is a collection with a .length property that can be traversed using a zero-based integer index:

Option Explicit

Dim oFS    : Set oFS  = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec   = oFS.GetAbsolutePathName("..\data\33921005.xml")
Dim oXML   : Set oXML = CreateObject("Msxml2.DOMDocument.6.0")
oXML.load sFSpec
If 0 = oXML.parseError Then
   Dim ndlName : Set ndlName = oXML.selectNodes("/Envelope/Body/Request/individual/hasName/*")
   Dim ndName
   For Each ndName In ndlName
       WScript.Echo ndName.tagName
   Next
   Dim iNd
   For iNd = 0 To ndlName.length - 1
       WScript.Echo iNd, ndlName(iNd).tagName
   Next
Else
   WScript.Echo oXML.parseError.reason
End If

output:

cscript 36053711.vbs
firstName
lastName
0 firstName
1 lastName

2 Comments

We know.
What's with If 0 = oXML.parseError Then maybe it's just me but doing like that is triggering my OCD, is there some benefit to that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.