implementing xpath with javascript
example input file (books.xml)
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="th">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
------------------------------------------------------------------------
javascript that implement xpath
<html>
<body>
<script>
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("books.xml");
/* selects all the title elements that have an attribute named "lang"
var oXMLNodeList = xmlDoc.selectNodes("//title[@lang]");*/
/* selects all the title elements that have an attribute name "lange" with a value of "th"
var oXMLNodeList = xmlDoc.selectNodes("//title[@lang='th']"); */
/* selects the price elements that have price more that 35.00
var oXMLNodeList = xmlDoc.selectNodes("/bookstore/book[price>35.00]/price");*/
/* selects the title element of the first book element
var oXMLNodeList = xmlDoc.selectNodes("/bookstore/book[0]/title");*/
for( var idx = 0; idx < oXMLNodeList.length; idx++ )
{
// document.write("title: " + oXMLNodeList.item(idx).selectSingleNode("price").text + "<br/>");
document.write("value : " + oXMLNodeList[idx].childNodes[0].nodeValue + "<br/>");
}
</script>
</body>
</html>
----------------------------------------------------------------------------