VBScript: Get attribute value in XML node if child node value meets parameter



I want to get the attribute value "Name" based on a filtered child node value "Location = 'Local'". The source XML looks like this:



<?xml version="1.0"?>
<Root>
<User Name="user1">
<Element CreationDate="2014-11-16 18:05:44" ID="0a962d93-64e8-4caa-8a4d-e6e9b7df5af4" Name="ClienName1">
<AdditionalInfo>
<Client />
<Number />
<MasterMachine>LocalMachine1</MasterMachine>
<Location>Local</Location>
<Owner>Owner1</Owner>
</AdditionalInfo>
</Element>
<Element CreationDate="2014-11-21 16:08:53" ID="65ddadf1-f7e4-467c-aedf-3d6ba5c35d1d" Name="ClienName2">
<AdditionalInfo>
<Client />
<Number />
<MasterMachine>LocalMachine1</MasterMachine>
<Location>Local</Location>
<Owner>Owner1</Owner>
</AdditionalInfo>
</Element>
<Element CreationDate="2014-11-24 10:00:13" ID="469479c7-a249-4bf4-a486-fc09b13ba145" Name="ClienName3">
<AdditionalInfo>
<Client />
<Number />
<MasterMachine>ServerMachine1</MasterMachine>
<Location>Server</Location>
<Owner>Owner2</Owner>
</AdditionalInfo>
</Element>
</User>
</Root>


What I have so far is:



Set objXML = CreateObject("MSXML2.DOMDocument")
objXML.setProperty "SelectionLanguage", "XPath"
objXML.Load XML
Set objElementNode = objXML.selectNodes("//Root/User/Element/@Name")
Set objLocationNode = objXML.selectNodes("//Root/User/Element/@Name/AdditionalInfo [Location='Local']/Location")

For Each elementName In objElementNode
For Each location In objLocationNode
strMessage = elementName.text &" "& location.text &vbCr
Next
Next
MsgBox strMessage


Clearly I have no clue in this realm. This is so far based on snippets I've seen online without an understanding how to traverse nodes. Explanations will greatly help for future reference! And thanks in advance.


No comments:

Post a Comment