Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick Syntaxy Question: XPath & 'ancestor'

Status
Not open for further replies.

nspreitzer

Programmer
Sep 10, 2008
3
US
One MILLION dollars to the person who can answer this for me.

Using VBA, I wrote code similar to what is below. That last line is the one in question. If my XPath query is written "ancestor::*", the code works correctly. However, when I replace the astrix with a specific element name, as I have below, the code returns an empty node list. I have tried several different element names which are ancestors to the initial node [ndeTopLevel], but the result is always the same.

I'm using msxml version 6.0.

Any ideas?

Sub Argh()

Dim xmldoc As DOMDocument
Dim ndeTopLevel As IXMLDOMNode
Dim nlstClasses As IXMLDOMNodeList

Set xmldoc = New DOMDocument

xmldoc.Load "c:\blahblahblah.xml"
xmldoc.setProperty "SelectionLanguage", "XPath"

Set ndeTopLevel = xmldoc.FirstChild.FirstChild.FirstChild
Set nlst = ndeTopLevel.selectNodes("ancestor::Group")

End Sub
 
[0] If you have to declare ndeTopLevel and the missing dim for nlst (I hope it is not meant to be nlstClasses by typos), it should be done properly.
[tt]
Dim xmldoc As DOMDocument
[red]'[/red]Dim ndeTopLevel As IXMLDOMNode
Dim ndeTopLevel As [red]IXMLDOMElement[/red]
Dim nlstClasses As IXMLDOMNodeList [blue]'???[/blue]
[blue]Dim nlst As IXMLDOMSelection[/blue]
[/tt]
[1] Then you've get to the ndeTopLevel properly
>Set ndeTopLevel = xmldoc.FirstChild.FirstChild.FirstChild
[tt]Set ndeTopLevel = xmldoc.[red]DocuemntElement.[/red]FirstChild.FirstChild.FirstChild[/tt]
supposing your xml design has sufficient depth to allow it to make sense. (Otherwise, it is no good by its own.)

[2] After those ascertained positive, it gets its way properly for nlst.
 
Typos fixed below. Problem remains.

Sub Argh()

Dim xmldoc As DOMDocument
Dim ndeTopLevel As IXMLDOMNode
Dim nlst As IXMLDOMNodeList

Set xmldoc = New DOMDocument

xmldoc.Load "c:\blahblahblah.xml"
xmldoc.setProperty "SelectionLanguage", "XPath"

'this does grab a nested element.
Set ndeTopLevel = xmldoc.FirstChild.FirstChild.FirstChild
Set nlst = ndeTopLevel.selectNodes("ancestor::Group")

End Sub
 
I won't comment on pieces of supplementary info not disclosed. However, with the strict info avail, the corrections I made stand and relevant. But, you know, do whatever you are pleased.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top