[英]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):这是我的 XML 文件 (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:这是我的 Javascript 文件:
<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 :(我无法获得 bk 命名空间标签内的值:(
I have tried all that //*[name() etc etc rubbish, no go :(我已经尝试了所有这些 //*[name() 等垃圾,不行 :(
Any help would be much appreciated!!!!!!!!!!!!!!!!!!!任何帮助将非常感激!!!!!!!!!!!!!!!!!!!
Kind Regards, Sam Gungormez亲切的问候, Sam Gungormez
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
}
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.) ).啊哈我的代码在 IE 中运行,但不是像 Firefox 这样的浏览器(有趣的是,如果代码由服务器(apache 等)托管,则仅在 IE 中执行)。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.