簡體   English   中英

document.evaluate使用iterateNext返回null

[英]document.evaluate returns null with iterateNext

我一直在關注w3學校使用xpath導航我的xml文檔的示例,但是我從iterateNext()返回的所有內容都是null。 下面是我的blog.xml文件。

<blog
xmlns ="http://www.w3schools.com" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="blogschema.xsd">
<Title>My blog</Title>

<Entry>
    <Heading id="101">week1</Heading>   
    <body>
        <text>enter text right here</text>
        <pictures>pictures in the body</pictures>

    </body>
    <labels>Seperate labels with commas</labels>
    <date> 20121119</date>

</Entry>





</blog>

這是我的html腳本,永遠不會達到while語句因為結果總是返回null,這可能是我忽略的東西,但我認為如果它在W3學校它應該真的有用。

xmlDoc=loadXMLDoc("blog.xml");//loads xml file  
//loadXmlContent(xmlDoc); using xml dom

path="/blog/Title"
if(document.implementation && document.implementation.createDocument)
{

    var nodes = xmlDoc.evaluate(path, xmlDoc, null, 5, null);
    alert(nodes);
    var result = nodes.iterateNext();


    while (result)
    {document.write(result.childNodes[0].nodeValue);}

}
</script>

在輸入中有一個默認的命名空間聲明xmlns="http://www.w3schools.com" ,您需要考慮到這一點

var nodes = xmlDoc.evaluate("df:blog/df:Title", xmlDoc, function(prefix) { if (prefix === "df") return "http://www.w3schools.com"; }, 5, null);

var result;

while ((result = nodes.iterateNext()) != null)
{
  document.write(result.textContent);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM