我一直在遵循w3学校的示例,使用xpath在我的xml文档中导航,但是我从iterateNext()得到的结果是空的。下面是我的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语句永远不会到达,因为result总是返回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>发布于 2012-12-31 23:05:04
在输入中有一个默认的名称空间声明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);
}https://stackoverflow.com/questions/14102573
复制相似问题