0

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
HandsUp
  • 13
  • 1
  • 3
  • possible duplicate of [XPath : Get nodes where child node contains an attribute](http://stackoverflow.com/questions/1457638/xpath-get-nodes-where-child-node-contains-an-attribute) – Ansgar Wiechers Nov 25 '14 at 20:03
  • @Ansgar - Not quite, the attribute does not restrict the resultset. – Ekkehard.Horner Nov 25 '14 at 20:11
  • @Ekkehard.Horner In his case the expression would have to compare a text node instead of an attribute (`//User[Element/AdditionalInfo/Location/text() = 'Local']`), but other than that it's pretty much a duplicate AFAICS. – Ansgar Wiechers Nov 25 '14 at 21:54

1 Answers1

0

Search for AdditionalInfo having a child Location with value Local and get the Name attribute of its parent:

In code:

  Dim oFS      : Set oFS      = CreateObject("Scripting.FileSystemObject")
  Dim sFSpec   : sFSpec       = goFS.GetAbsolutePathName("..\testdata\xml\27135249.xml")
  Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument")
  objMSXML.setProperty "SelectionLanguage", "XPath"
  objMSXML.async = False
  objMSXML.load sFSpec

  If 0 = objMSXML.parseError Then
     Dim sXPath : sXPath = "Root/User/Element/AdditionalInfo[Location=""Local""]"
     Dim ndlX : Set ndlX = objMSXML.selectNodes(sXPath)
     If 0 = ndlX.length Then
        WScript.Echo sXPath, "failed"
     Else
        Dim ndX
        For Each ndX In ndlX
            WScript.Echo "Name:", ndX.parentNode.GetAttribute("Name")
        Next
     End If
  Else
     WScript.Echo objMSXML.parseError.reason
  End If
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96