I have done a TON of research and still have just hit a dead-end :(
Here\'s my XML file (test.xml):
Ahhh My code was working in IE but not broswers like Firefox (Interesting thing, the code only executed in IE if it was being hosted by a server (apache, etc.) ).
I now fixed it all and it works flawlessly with all browsers. I can't thank you enough Martin.
I wasn't parsing the function constructors variables correctly.
Here is fully functional code:
<html>
<body>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
xml=loadXMLDoc("test.xml");
var path="/bookstore/bk:book/bk:title";
if (typeof xml.evaluate !== 'undefined')
{
var result = xml.evaluate(
path,
xml,
function (prefix) {
if (prefix === 'bk') {
return 'http://purl.org/dc/elements/1.1/';
}
else {
return null;
}
},
XPathResult.ANY_TYPE,
null
);
// now use the code here you already have in your sample for evaluate
var nodes=xml.evaluate(
path,
xml,
function (prefix) {
if (prefix === 'bk') {
return 'http://purl.org/dc/elements/1.1/';
}
else {
return null;
}
},
XPathResult.ANY_TYPE,
null);
var result=nodes.iterateNext();
while (result)
{
document.write(result.childNodes[0].nodeValue);
document.write("<br />");
result=nodes.iterateNext();
}
}
else if (typeof xml.selectNodes !== 'undefined' && typeof xml.setProperty != 'undefined')
{
xml.setProperty('SelectionLanguage', 'XPath');
xml.setProperty('SelectionNamespaces', 'xmlns:bk="http://purl.org/dc/elements/1.1/"');
var nodes = xml.selectNodes(path);
// now use the code you already have for selectNodes
var nodes=xml.selectNodes(path);
//var nodes=xmlDoc.getElementsByTagName('bk:title');
for (i=0;i<nodes.length;i++)
{
document.write(nodes[i].childNodes[0].nodeValue);
document.write("<br />");
}
}
</script>
</body>
</html>
Here is some sample code to show how to deal with namespaces:
var path="/bookstore/bk:book/bk:title";
if (typeof xml.evaluate !== 'undefined') {
var result = xml.evaluate(
path,
xml,
function (prefix) {
if (prefix === 'bk') {
return 'http://purl.org/dc/elements/1.1/';
}
else {
return null;
}
},
XPathResult.ANY_TYPE,
null
);
// now use the code here you already have in your sample for evaluate
}
else if (typeof xml.selectNodes !== 'undefined' && typeof xml.setProperty != 'undefined') {
xml.setProperty('SelectionLanguage', 'XPath');
xml.setProperty('SelectionNamespaces', 'xmlns:bk="http://purl.org/dc/elements/1.1/"');
var nodes = xml.selectNodes(path);
// now use the code you already have for selectNodes
}