views:

318

answers:

2

Hi all,

OK, I'm at my wits end. This seems like it should be a completely trivial thing to do, yet after an hour I still just cannot make it work.

I'm trying to get a list of time zones from the Campaign Monitor API; unfortunately the page I need to do this in is written in classic ASP/Javascript so I can't just use the API wrapper.

I'm making the request like this:

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

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

The correct XML is coming back from the server, as follows:

<anyType d1p1:type="ArrayOfString" xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://api.createsend.com/api/"&gt; 
    <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>

Then I am loading this XML into an MSXML document:

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

The problem, as you can see from the comments, is that I can't seem to match those 'string' nodes no matter what I do. I'm pretty rusty when it comes to ASP/Javascript - I suspect it's something do do with the namespaces (I know I had problems with this in the past), but I'm not sure what.

Can anyone point out what I'm doing wrong? Any help much appreciated!

+2  A: 

Your misconception is in default namespace handling. There is no such thing as a default namespace for XPath expressions here - you must use a prefix, even if it does not have a prefix in the 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");

If you do not use a prefix, XPath expressions are handled in the empty namespace. This is why you could not select anything with "//string".

Tomalak
I'm afraid going to give the answer to Anthony because he went a bit above and beyond the call of duty, but thanks a lot for your help!
Mark B
+1  A: 

You cannot override the default namespace used by XPath. In MSXML the XPath default namespace is always the "no name" namespace. However there is no need for the set of aliases used in the SelectionNamespaces property to match those of the document (although of course it makes sense where possible to use the same ones).

Define your set of namespaces like this:-

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

Now you can select all the string elements with:-

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

This is how I would code this as a whole:-

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

Notes:-

  • For a GET request there is no need to use a separate ServerXMLHttp object, you can instruct the DOMDocument to use ServerXMLHttp internally when by enabling the ServerHTTPRequest property. (BTW, your code seems to be reduntantly streaming the DOMDocument exposed in by the ResponseXML property into a new DOMDocument).
  • I prefer to use the 3.0 version of MSXML since that is guaranteed to be present on supported platforms. If not then I would install 6.0 and use that.
  • Specifying the SelectionLanguage to be XPath on 4.0 or above is redundant, its the default selection language.
AnthonyWJones
It appears there's a bit of a gap in my XML knowledge, best read up! All working now, very grateful, thanks.
Mark B