Kaz
Kaz

Reputation: 1332

Selecting node in XML using XPath in VBA

I now have code which I think should work, but isn't. It returns nothing. I'm absolutely new to XML so it could just be a syntax problem.

http://i.imgur.com/YatU8N3.png?1

Code:

Public Sub CreateXmlDom()

    Dim xDoc As MSXML2.DOMDocument
    Dim xmlNameSpaces As String

    Dim xmlNodeList As MSXML2.IXMLDOMNodeList

    Dim strFullFilename As String
        strFullFilename = "S:\Investments\Data\Companies House\Monthly Companies House Downloads\Accounts_Monthly_Data-April2014\Prod224_0005_00011771_20131231.html"

    Set xDoc = New MSXML2.DOMDocument

        With xDoc

        If .Load(strFullFilename) Then
            .setProperty "SelectionLanguage", "XPath"
            .setProperty "SelectionNamespaces", "xmlns:ns5=""http://www.xbrl.org/uk/gaap/core/2009-09-01"""

            Set xmlNodeList = .SelectNodes("//ns5:CashBankInHand")
        End If

        End With

End Sub

Element from an XML document I am trying to select:

enter image description here

From the doc header:

enter image description here

Document full text

Document download

Upvotes: 0

Views: 812

Answers (1)

Kaz
Kaz

Reputation: 1332

So, it turns out it was a combined syntax / "didn't quite understand XML heirarchy yet" issue.

So, firstly, this isn't a Node, it's not even an attribute, it's just a value:

"ns5:CashBankInHand"

It was contained in a Node <ix:nonFraction ...> so the first part of my XPath string should be:

("//ix:nonFraction")

Then, name is an attribute so the actual query should be:

("//ix:nonFraction [@name = ""ns5:CashBankInHand""]")

Which works perfectly.

Upvotes: 1

Related Questions