I have an XML document that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<entry xml:base="https://sps.utility.abc123.com/sites/xyz/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" m:etag=""5"">
<id>Web/Lists(guid'c920cb93-a31a-49a0-a4d6-ac1cb8fb0658')/Items(2)</id>
<category term="SP.Data.REST_x0020_Test_x0020_ListListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" href="Web/Lists(guid'c920cb93-a31a-49a0-a4d6-ac1cb8fb0658')/Items(2)" />
<title />
<updated>2017-09-20T16:01:19Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Title>item 2 has a new value.</d:Title>
</m:properties>
</content>
</entry>
I want to get the value at the <d:Title>
node, which in this case is item 2 has a new value.
When I use the following:
Private Function getFieldValueFromXml(xml As Variant, fieldName As String)
Dim node As IXMLDOMNode
Dim namespaceAndName As String
Dim domDoc as MSXML2.DomDocument60
namespaceAndName = "d:" & fieldName
Set domDoc = New DOMDocument60
Set node = domDoc.SelectSingleNode(namespaceAndName)
getFieldValueFromXML = node.Text
End Function
The SelectSingleNode()
call throws this error:
Reference to undeclared namespace prefix: 'd'.
This also happens if I try to first set node with m
as follows:
Set node = domDoc.SelectSingleNode("m:properties")
How can I get at the inner text of <d:Title>
here?
I needed to set the DOM document's SelectionNamespaces
property before calling SelectSingleNode()
, as follows:
domDoc.SetProperty "SelectionNamespaces", "xmlns:d='http://schemas.microsoft.com/ado/2007/08/dataservices'"