我已经做了大量的研究,但还是没有任何结果。
我的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>
我的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>
我无法获得bk命名空间标记中的值:(
我已经尝试了所有的//*[name()等等等等垃圾,不去:(
任何帮助将非常感激 !!!!!!!!!!!!!!!!!!!
亲切的问候,山姆Gungormez
下面是一些示例代码来展示如何处理名称空间:
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
}
啊,我的代码在IE中工作,但不是像Firefox这样的浏览器(有趣的是,代码只有在服务器(apache等)托管的情况下才能在IE中执行)。
我现在修复了这一切,它可以在所有浏览器上完美地工作。我怎么感谢你都不为过,马丁。
我没有正确解析函数构造函数变量
下面是功能完整的代码:
<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>