6

I am trying to get the number of specific nodes in an XML file using the XPath count function, however, this keeps returning an error "An exception of type 'msxml3.dll: Expression does not return a DOM node."

How do I get the return value from an XPath count using VBScript and MSXML DOM

Dim oXML    
Dim homeId
Dim awayId
Dim homeGoals
Dim awayGoals
Set oXML = Server.CreateObject("Microsoft.XMLDOM")

oXML.async = false
oXML.SetProperty "SelectionLanguage", "XPath"
oXML.SetProperty "ServerHTTPRequest", True
oXML.validateOnParse = False
oXML.resolveExternals = False

fileName = "http://server:8090/data/results/m12345.xml")
oXML.load (fileName)

homeId = oXML.SelectSingleNode("/SoccerMatch/Team[@homeOrAway='Home']/@id").text
awayId = oXML.SelectSingleNode("/SoccerMatch/Team[@homeOrAway='Away']/@id").text
Set homeGoals = oXML.SelectSingleNode("count(/SoccerMatch/Goals/Goal[@teamId="&homeId&"])")
Set awayGoals = oXML.SelectSingleNode("count(/SoccerMatch/Goals/Goal[@teamId="&awayId&"])")

1 Answer 1

12

You can only use XPaths that return Nodes in MSXML, other XPath functions can only be used in predicates that ultimately result in a selection of nodes.

Use:-

homeGoals = oXML.SelectNodes("/SoccerMatch/Goals/Goal[@teamId="&homeId&"]").length
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was looking for a way to check for XPaths using VBScript that doesn't throw errors for paths not found. This works. :-)

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.