xml - 为什么XPath表达式无法通过.SelectSingleNode()找到我想要的节点?

标签 xml vba dom xpath

这是我用来在Sharepoint列表项的DOM文档中查找某个节点的内部文本的代码:

'parse XML of a list item to get the field value
Private Function getFieldValueFromXml(xml As Variant, fieldName As Variant) As Variant

Dim node As IXMLDOMNode
Dim namespaceAndName As String

'set namespace string for XPath search, using field name
namespaceAndName = "/d:" & fieldName

Set domDoc = New DOMDocument60

'load DOM document with XML
domDoc.LoadXML xml

'set selection namespace for XPath search
domDoc.SetProperty "SelectionNamespaces", "xmlns:d='http://schemas.microsoft.com/ado/2007/08/dataservices'"

'get first node with this namespace (a list item should have only one node with a given field name)
Set node = domDoc.SelectSingleNode(namespaceAndName)

getFieldValueFromXml = node.text

End Function


当我通过ID号检索列表项时,这种方法很好用,但是当我使用$filter来获得它时,如下所示:

https://sps.utility.xyz.com/Sites/ABC/_api/web/lists/GetByTitle('Stations')/items?$filter=Title eq '020'


然后,当我尝试通过调用getFieldValueFromXml(result,"Id")查找值时,找不到节点。这是上述REST调用返回的XML:

<?xml version="1.0"?>
<feed xml:base="https://sps.utility.abc.com/Sites/asdaC/_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">
<id>5a608d14-721b-4076-9ed0-91eccd9b70ec</id>
<title/>
<updated>2017-10-04T22:24:47Z</updated>
<entry m:etag="&quot;42&quot;">
<id>Web/Lists(guid'f9565c6c-334c-4953-a117-5dd9b0cd38a5')/Items(415)</id>
<category term="SP.Data.StationsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<title/>
<updated>2017-10-04T22:24:47Z</updated>
<author>
<name/>
</author>
<content type="application/xml">
<m:properties>
<d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
<d:Id m:type="Edm.Int32">415</d:Id>
<d:ReferenceImages m:type="SP.FieldUrlValue">
<d:Description>some number 131553</d:Description>
<d:Url>http://google.com</d:Url>
</d:ReferenceImages>
<d:CDScope m:type="Edm.Boolean">true</d:CDScope>
<d:ID m:typ
e="Edm.Int32">415</d:ID>
<d:Modified m:type="Edm.DateTime">2017-10-04T20:15:54Z</d:Modified>
<d:Created m:type="Edm.DateTime">2016-03-27T19:21:49Z</d:Created>
<d:AuthorId m:type="Edm.Int32">50</d:AuthorId>
<d:EditorId m:type="Edm.Int32">330</d:EditorId>
<d:OData__UIVersionString>18.0</d:OData__UIVersionString>
<d:Attachments m:type="Edm.Boolean">false</d:Attachments>
<d:GUID m:type="Edm.Guid">f31fcf07-0d81-442e-8621-56d09a8507f6</d:GUID>
</m:properties>
</content>
</entry>
</feed>


getFieldValueFromXml()中,.SelectSingleNode(namespaceAndName) call returns Nothing. How should I modify my code to pick up the Id`的值是多少?

最佳答案

看来您只是在搜寻根目录。

尝试使用类似以下内容搜索后裔:

.SelectSingleNode(".//d:Id")

关于xml - 为什么XPath表达式无法通过.SelectSingleNode()找到我想要的节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46591782/

相关文章:

php - 无法在 PHPStorm 10.0.1 中为 artisan 制作命令行工具

javascript - 使用 jquery(或 javascript)拦截仅点击 "clickable"元素

jquery - 删除任何具有 img 子级的段落标签,但不删除 img

javascript DOM对象继承

mysql - mule xpath 提取值 mysql

ruby - Nokogiri SAX 忽略空字符

excel - 如何计算 Excel 中两个 ISO 8601 日期时间之间的秒数?

excel - 在 VBA 的 UDF 中将单元格作为参数传递的正确方法是什么?

html - xpath:仅提取以开头的类

vba - 使用 VBA 从整个工作表中删除格式