Ive been following the examples from w3 schools on using a xpath to navigate through my xml document however all i get back from iterateNext() is null. Below is my blog.xml file.

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

This is my html script, the while statement is never reached as result always returns null, this maybe something that im overlooking, but i assumed that if it was on w3 schools it should really work.

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>
有帮助吗?

解决方案

There is a default namespace declaration xmlns="http://www.w3schools.com" in the input that you need to take into account with e.g.

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归因
不隶属于 StackOverflow
scroll top