Instead you need to bind a prefix to the namespace URI and use that prefix in the XPath (ancestor::ns1:root). Dim xmlDoc As New MSXML2.DOMDocument60 Dim xmldata As MSXML2.IXMLDOMNodeList, paging As MSXML2.IXMLDOMNode Dim xmlpagesElement As MSXML2.IXMLDOMElement xmlDoc.SetProperty 'SelectionLanguage', 'XPath' xmlDoc.
#Xpath xmldoc api vba code#
However, when I replace the astrix with a specific element name, as I have below, the code returns an empty node list. If my XPath query is written 'ancestor::', the code works correctly. Set nodeList = bar.selectNodes("ancestor::ns1:root")Īs you can see, for the second XML sample where all elements are in the namespace the XPath ancestor::root does not select any node. Using VBA, I wrote code similar to what is below.
It allows you to extract any number of nodes (0 or more) which match the specified XPath query. XPath is a query language used for selecting XML nodes in an XML document. ChildNodes/.FirstChild/NextChild properties we can also use XPath. Set bar = ĭoc.setProperty "SelectionNamespaces", "xmlns:ns1=''" Instead of traversing the elements/nodes in your XML using the.
WScript.Echo "Found " & nodeList.length & " node(s)." In the following statement, The way I understand XPath so far should recursively look in the xml file below for everyCovering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
#Xpath xmldoc api vba free#
Setting the starting point for the XPath query sets the context node, which is the starting point for the XPath query. W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Set nodeList = bar.selectNodes("ancestor::root") The examples above start the XPath query at the document element. Set doc = CreateObject("Msxml2.DOMDocument.6.0")