VBScript, MSXML and Namespaces

こ雲淡風輕ζ 提交于 2021-02-08 15:32:01

问题


Given the following XML:

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetMsisdnResponse xmlns="http://my.domain.com/">
            <GetMsisdnResult>
                <RedirectUrl>http://my.domain.com/cw/DoIdentification.do2?sessionid=71de6551fc13e6625194</RedirectUrl>
            </GetMsisdnResult>
        </GetMsisdnResponse>
    </soap:Body>
</soap:Envelope>

I am trying to access the RedirectUrl element using XPath in VBScript:

set xml = CreateObject("MSXML2.DOMDocument")
xml.async = false
xml.validateOnParse = false
xml.resolveExternals = false
xml.setProperty "SelectionLanguage", "XPath"
xml.setProperty "SelectionNamespaces", "xmlns:s='http://my.domain.com/' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"

err.clear
on error resume next
xml.loadXML (xmlhttp.responseText)
if (err.number = 0) then

    redirectUrl = xml.selectSingleNode("/soap:Envelope/soap:Body/s:GetMsisdnResponse/s:GetMsisdnResult/s:RedirectUrl").text
end if

but it is failing to find the RedirectUrl node, therefore is nothing when I try to get the .text property. What am I doing wrong


回答1:


You are using the wrong namespace declaration.

In your XML you have

http://www.w3.org/2003/05/soap-envelope

but in your Script, you use

http://schemas.xmlsoap.org/soap/envelope/

This works for me:

xml.setProperty "SelectionNamespaces", "xmlns:s='http://my.domain.com/' xmlns:soap='http://www.w3.org/2003/05/soap-envelope'"

' ...

Set redirectUrl = xml.selectSingleNode("/soap:Envelope/soap:Body/s:GetMsisdnResponse/s:GetMsisdnResult/s:RedirectUrl")

On a different note - I'd try to keep the lines that are affected by an On Error Resume Next statement at an absolute minimum. Ideally, it is in effect for a single critical line only (or you wrap the critical section in a Sub). This makes debugging a lot easier.

For example, you are missing a Set statement in Set redirectUrl = .... This will fail silently when On Error Resume Next is on.

Try

' this is better than loadXML(xmlHttp.responseText)
xmlDocument.load(xmlHttp.responseStream)

If (xmlDocument.parseError.errorCode <> 0) Then
  ' react to the parsing error
End If

Xpath = "/soap:Envelope/soap:Body/s:GetMsisdnResponse/s:GetMsisdnResult/s:RedirectUrl"
Set redirectUrl = xml.selectSingleNode(Xpath)

If redirectUrl Is Nothing Then
  ' nothing found
Else
  ' do something
End If

See - no On Error Resume Next necessary.




回答2:


Also note that the namespace is case-sensitive, but that at least some MSXML force it to lower case.

So if you declare xml.setProperty "SelectionNamespaces", "xmlns:SSS='http://my.domain.com/'"

and try xml.selectSingleNode("/SSS:Envelope") it may fail.

You would need to use xml.selectSingleNode("/sss:Envelope").

Or better to make your namespaces lower case.



来源:https://stackoverflow.com/questions/1199739/vbscript-msxml-and-namespaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!