2009-07-29 22 views
4

次のXMLを考える:私はVBScriptでXPathを使用してRedirectUrl要素にアクセスしようとしていますのVBScript、MSXMLと名前空間

<?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> 

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 

をしかし、RedirectUrlノードを見つけるために失敗しています、したがって、.textプロパティを取得しようとすると何も表示されません。何が間違っているのですか

+0

@Xetiusなぜなら、xmlhttpを使用しているので、DOMDocumentを返すxmlhttp ReceivedXMLプロパティを取得しないのですか? (サーバーはContent-Typeヘッダーにtext/xmlなどのxml MIMEタイプを指定する必要があります)。 – AnthonyWJones

答えて

9

あなたは間違った名前空間宣言を使用しています。あなたのXMLで

あなたは、

が、あなたのスクリプトで

http://www.w3.org/2003/05/soap-envelope
を持っています。これは、私の作品

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

使用:別のノートオン

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") 

- 私は0123の影響を受ける行を維持しようとします。の明細書はで、絶対的なです。理想的には、単一のクリティカルラインのみで有効です(または、Subにクリティカルセクションをラップします)。これにより、デバッグがより簡単になります。

たとえば、Set redirectUrl = ...Setステートメントがありません。 On Error Resume Nextがオンの場合、これは黙って失敗します。必要なしOn Error Resume Next -

' 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 

参照してみてください。

+1

ちょうど仕事 - ありがとう。残りのコードも整理してください。 – Xetius

3

また、名前空間では大文字と小文字が区別されますが、少なくとも一部のMSXMLでは小文字にすることに注意してください。だから、

あなたはxml.setProperty "SelectionNamespaces", "xmlns:SSS='http://my.domain.com/'"

を宣言し、それが失敗することがありxml.selectSingleNode("/SSS:Envelope")をしようとした場合。

xml.selectSingleNode("/sss:Envelope")を使用する必要があります。

または、名前空間を小文字にすることをお勧めします。