VBScript:具有多个名称空间的XPath查询

时间:2010-07-20 10:35:20

标签: xpath vbscript

我应该使用XPath个查询来访问GetLogisticsOfferDateResult节点? 我附上了我正在使用的vbscript。 我怀疑问题与文档中的多个名称空间有关。但是如何引用XPath中的第二个命名空间?

Dim responseXML
responseXML = '"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/""><s:Body><GetLogisticsOfferDateResponse xmlns=""http://schneider-electric.com/OrderEntryService""><GetLogisticsOfferDateResult>2010-07-20</GetLogisticsOfferDateResult></GetLogisticsOfferDateResponse></s:Body></s:Envelope>"'

Dim responseDoc
Set responseDoc = WScript.CreateObject("MSXML2.DOMDocument.6.0")
responseDoc.loadXML(responseXML)
responseDoc.setProperty "SelectionNamespaces", "xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'"
Dim requestedNode
'This node is not found
'Set requestedNode = responseDoc.selectSingleNode("//s:Envelope//s:Body//GetLogisticsOfferDateResponse//GetLogisticsOfferDateResult")

'This node is found
Set requestedNode = responseDoc.selectSingleNode("//s:Envelope//s:Body")
'This node is found
'Set requestedNode = responseDoc.selectSingleNode("//s:Envelope")

If requestedNode Is Nothing Then
    WScript.Echo "Node not found"
Else
    WScript.Echo requestedNode.text
End If

Set responseDoc = Nothing
Set LODateNode = Nothing

2 个答案:

答案 0 :(得分:7)

原来我的selectionNamespaces设置必须如下:

responseDoc.setProperty "SelectionNamespaces", "xmlns:sc='http://schneider-electric.com/OrderEntryService' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'"

然后XPath查询必须是:

Set requestedNode = responseDoc.selectSingleNode("//s:Envelope//s:Body//sc:GetLogisticsOfferDateResponse//sc:GetLogisticsOfferDateResult")

答案 1 :(得分:1)

您尚未在代码中定义文档的默认命名空间(http://schneider-electric.com/OrderEntryService)。

responseDoc.setProperty "SelectionNamespaces", "'http://schemas.xmlsoap.org/soap/envelope/' 'http://schneider-electric.com/OrderEntryService'"

您需要添加它,或者使用它添加属于它的元素前缀。