Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

html - xPath XML file with namespaces using Javascript

I have done a TON of research and still have just hit a dead-end :(

Here's my XML file (test.xml):

<bookstore>
  <book genre="autobiography">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <bk:book genre="novel" bk:genre="fiction" 
xmlns:bk="http://purl.org/dc/elements/1.1/">
    <bk:title>The Confidence Man</bk:title>
    <bk:author>
      <bk:first-name>Herman</bk:first-name>
      <bk:last-name>Melville</bk:last-name>
    </bk:author>
    <bk:price>11.99</bk:price>
  </bk:book>
</bookstore>

Here's my Javascript file:

<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");
//xml.remove_namespaces;
path="/bookstore/bk:book/bk:title";
// code for IE
if (window.ActiveXObject)
{
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 />");
  }
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();

while (result)
  {
  document.write(result.childNodes[0].nodeValue);
  document.write("<br />");
  result=nodes.iterateNext();
  }
}
</script>

</body>
</html>

I cannot get the value inside the bk namespaced tags :(

I have tried all that //*[name() etc etc rubbish, no go :(

Any help would be much appreciated!!!!!!!!!!!!!!!!!!!

Kind Regards, Sam Gungormez

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...