2011-08-31 62 views
0

我做的研究提示音,並命名空間的XPath XML文件還是剛剛進入了死衚衕:(與使用Javascript

我的繼承人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命名空間的標籤:(

我已經試過裏面的值所有// * [名稱()等等等等垃圾,沒有去:(

任何幫助將不勝感激!!!!!!!!!!!!!!!!!!!

親切的問候, 山姆Gungormez

回答

1

下面是一些示例代碼來顯示如何處理命名空間:

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 
} 
1

唉唉我的代碼在IE工作,但不broswers如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>