1

OK、私は私の知恵の終わりにいます。これは完全に些細なことのように思えますが、1時間経っても、それを機能させることはできません。

キャンペーンモニターAPIからタイムゾーンのリストを取得しようとしています。残念ながら、これを行う必要のあるページは従来のASP / Javascriptで記述されているため、APIラッパーだけを使用することはできません。

私は次のようにリクエストを行っています:

var request = Server.CreateObject("Msxml2.ServerXMLHTTP");

request.open("GET", apiurl + "/User.GetTimezones?ApiKey=" + apikey, false);
request.send();

次のように、正しいXMLがサーバーから返されます。

<anyType d1p1:type="ArrayOfString" xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://api.createsend.com/api/"> 
    <string>(GMT) Casablanca</string> 
    <string>(GMT) Coordinated Universal Time</string> 
    <string>(GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London</string> 
    <string>(GMT) Monrovia, Reykjavik</string> 
    <string>(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna</string> 
    <string>(GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague</string> 
    <string>(GMT+01:00) Brussels, Copenhagen, Madrid, Paris</string> 
    (...and so on - I've truncated for the purpose of this question)
</anyType>

次に、このXMLをMSXMLドキュメントにロードします。

var response = Server.CreateObject("Msxml2.DOMDocument.4.0");
response.async = false;
response.validateOnParse = false;
response.resolveExternals = false;

response.setProperty("SelectionNamespaces", "xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://api.createsend.com/api/'");
response.setProperty("SelectionLanguage", "XPath");

if (response.load(request.responseXML)) 
{
    // If I uncomment this, the XML is correctly written out
    // Response.Write(response.xml);

    var nodes = response.selectNodes("//string");

    // No nodes are found, this is always zero
    Response.Write(nodes.length);

    for (var x = 0; x < nodes.length; x++) 
    {
        // Do something with each time zone value here
    }
}

コメントからわかるように、問題は、私が何をしても、それらの「文字列」ノードと一致しないように見えることです。ASP / Javascriptに関してはかなり錆びています-名前空間と関係があるのではないかと思います(過去にこれに問題があったことは知っています)が、何が原因かわかりません。

誰かが私が間違っていることを指摘できますか?どんな助けでも大歓迎です!

4

2 に答える 2

2

あなたの誤解は、デフォルトの名前空間処理にあります。ここには、XPath式のデフォルトの名前空間などはありません。XMLにプレフィックスがない場合でも、プレフィックスを使用する必要があります。

var nsDef = "";
nsDef = nsDef + "xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance' ";
nsDef = nsDef + "xmlns:api='http://api.createsend.com/api/' ";

response.setProperty("SelectionNamespaces", nsDef);
response.setProperty("SelectionLanguage", "XPath");

var nodes = response.selectNodes("//api:string");

プレフィックスを使用しない場合、XPath式は空の名前空間で処理されます。これが、で何も選択できなかった理由です"//string"

于 2009-12-07T17:26:40.110 に答える
1

XPathで使用されるデフォルトの名前空間をオーバーライドすることはできません。MSXMLでは、XPathのデフォルトの名前空間は常に「名前なし」の名前空間です。SelectionNamespacesただし、プロパティで使用されるエイリアスのセットがドキュメントのエイリアスと一致する必要はありません(もちろん、同じものを使用することは可能であれば意味があります)。

次のように名前空間のセットを定義します。-

var ns = "xmlns:a='http://api.createsend.com/api/' "
       + "xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance'"
response.setProperty("SelectionNamespaces", ns);

stringこれで、次の要素を使用してすべての要素を選択できます。-

var nodes = response.selectNodes("//a:string");

これは私がこれを全体としてコーディングする方法です:-

var response = Server.CreateObject("MSXML2.DOMDocument.3.0");  // or use 6.0 but not 4.0
response.async = false;
response.validateOnParse = false;
response.resolveExternals = false;


response.setProperty("ServerHTTPRequest", true); 

if (response.load(apiurl + "/User.GetTimezones?ApiKey=" + apikey)) 
{

    var ns = "xmlns:a='http://api.createsend.com/api/' "
           + "xmlns:d1p1='http://www.w3.org/2001/XMLSchema-instance'"
    response.setProperty("SelectionNamespaces", ns);

    response.setProperty("SelectionLanguage", "XPath");  // remove for 4.0 or above is default

    var nodes = response.selectNodes("//a:string");

    Response.Write(nodes.length);

    for (var x = 0; x < nodes.length; x++) 
    {
        // Do something with each time zone value here
    }
}

ノート:-

  • GETリクエストの場合、個別のServerXMLHttpオブジェクトを使用する必要はありません。プロパティを有効にすることで、内部でServerXMLHttpを使用するようにDOMDocumentに指示できServerHTTPRequestます。(ところで、あなたのコードは、ResponseXMLプロパティによって公開されたDOMDocumentを新しいDOMDocumentに冗長にストリーミングしているようです)。
  • サポートされているプラ​​ットフォームに確実に存在するため、3.0バージョンのMSXMLを使用することをお勧めします。そうでない場合は、6.0をインストールして使用します。
  • 4.0以降でXPathになるようにSelectionLanguageを指定することは冗長であり、デフォルトの選択言語です。
于 2009-12-07T18:15:01.880 に答える