Click to See Complete Forum and Search --> : XPath query


elias85
03-30-2008, 03:12 PM
i tried /bookstore/book[count(*)=4] in the XPath Checker in firefox on the http://www.w3schools.com/xpath/books.xml

why my code its not returning the 3 books as in that addon?

Heres my code:
<html>
<body>
<script type="text/javascript">
function loadXMLDoc(fname)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
xmlDoc.async=false;
xmlDoc.load(fname);
return(xmlDoc);
}

xml=loadXMLDoc("books.xml");

// http://www.zvon.org/xxl/XPathTutorial/General/examples.html

//select all books and their elements
//path="/bookstore/book/*/text()";

//select all the book prices
//path="/bookstore/book/price/text()";

// select the 1st book
//path="/bookstore/book[1]/title/text()";

//Selecting title Nodes with Price>35
//path="/bookstore/book[price>35]/title/text()";

//Select the books which doesnt have any attributes
//path="/bookstore/book[not(@*)]/*/text()";

//which of the children nodes of bookstore have 4 children each?
path="/bookstore/book[count(*)=4]";

// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path);

for (i=0;i<nodes.length;i++)
{
document.write(nodes[i].nodeValue);
document.write("<br />");
}
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var nodes=document.evaluate(path, xml, null, XPathResult.ANY_TYPE,null);
var result=nodes.iterateNext();

while (result)
{
document.write(result.nodeValue + "<br />");
result=nodes.iterateNext();
}
}
</script>
</body>
</html>