2010-07-20 69 views
1

什么XPath我应该使用查询来访问GetLogisticsOfferDateResult节点? 我附加了我正在使用的vbscript。 我怀疑问题与文档中的多个名称空间有关。但是,如何引用XPath中的第二个命名空间?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 

回答

6

原来我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

你有没有在你的代码中定义的文件(http://schneider-electric.com/OrderEntryService)的默认命名空间。

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

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

+0

OK我已经编辑下面一行在我的VBScript阅读: responseDoc.setProperty“SelectionNamespaces”,“xmlns ='http://schneider-electric.com/OrderEntryService/'xmlns:s ='http://schemas.xmlsoap.org/soap/envelope/'” 对以下行的调用不会返回节点: Set requestedNode = responseDoc.selectSingleNode(“// s:Envelope // s:Body // GetLogisticsOfferDateResponse // GetLogisticsOfferDateResult”) – 2010-07-20 11:03:01

相关问题