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/">
<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!
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:
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"
.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:-
Now you can select all the
string
elements with:-This is how I would code this as a whole:-
Notes:-
ServerHTTPRequest
property. (BTW, your code seems to be reduntantly streaming the DOMDocument exposed in by the ResponseXML property into a new DOMDocument).