SphericalApproximator
SphericalApproximator

Reputation: 111

How to use XPath in VBA

My XML kind of looks like the snipped below.

I want to:
Select all "id" which are in the node "Issue".
Not select any ids which are elements of subnodes.

My approach xml.SelectNodes("/config/entry/content/Issue/id") returns an empty list.

Here is the XML

<config> 
    <id>3423</id>
    <Title>Project Overview</Title>
    <entry>
        <id>3423</id>
        <Title>Project Overview</Title>
        <content>
            <Issue>
                <id>3423</id>
                <etc...>....<etc...>
                <hasMappedReleases>
                    <id>4365<id>
                    <etc...>....<etc...>
                </hasMappedReleases>
            </Issue>
        </content>
    </entry>
    <entry>
          ....
          ....

</config>       

Upvotes: 1

Views: 1868

Answers (1)

andy meissner
andy meissner

Reputation: 1322

Your solution should work. The problem should be in another place. I tried with this xml sample:

<config> 
    <id>3423</id>
    <Title>Project Overview</Title>
    <entry>
        <id>3423</id>
        <Title>Project Overview</Title>
        <content>
            <Issue>
                <id>3423</id>
                <hasMappedReleases>
                    <id>4365</id>
                </hasMappedReleases>
            </Issue>
        </content>
    </entry>
    <entry>
        <id>3424</id>
        <Title>Project Overview</Title>
        <content>
            <Issue>
                <id>3424</id>
                <hasMappedReleases>
                    <id>4367</id>
                </hasMappedReleases>
            </Issue>
        </content>
    </entry>
</config> 

and this code:

Sub Main()
    Dim xDoc As XmlDocument
    Dim result As XmlNodeList
    xDoc = New XmlDocument
    xDoc.Load("test.xml")
    result = xDoc.SelectNodes("/config/entry/content/Issue/id")
    Print(result.Count)
End Sub

and it printed me count = 2

Here is a good XPath reference: https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256086(v=vs.100)?redirectedfrom=MSDN

Upvotes: 3

Related Questions