How to use xpath on clientside

Here I am explaining how to use xpath on clientside

<html>
<head>
<script type="text/javascript">
readXmlUsingXPath();
function readXmlUsingXPath(){
var xmlDoc = null;
// code for IE

if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

xmlDoc.async=false;

xmlDoc.load("data.xml");

var xmlNodeList = xmlDoc.selectNodes("//Book[number(Pages)<30]");

for(var idx = 0; idx < xmlNodeList.length; idx++ ){

document.write("Title: " + xmlNodeList.item(idx).selectSingleNode("Title").text );

document.write(" Pages: " + xmlNodeList.item(idx).selectSingleNode("Pages").text);

document.write(" ISBN: " + xmlNodeList.item(idx).selectSingleNode("ISBN").text );

document.write("<br />")

}

}

// code for Mozilla, Firefox, Opera, etc.

else if (document.implementation && document.implementation.createDocument)

{

var requester = new XMLHttpRequest();

var xml = getXMLDocument("data.xml");

var xmlNodeList1 = xml.selectNodes("//Book[number(Pages)<30]");

for(var idx = 0; idx < xmlNodeList1.length; idx++ ){

document.write("Title: " + xmlNodeList1.item(idx).selectSingleNode("Title").text );

document.write(" Pages: " + xmlNodeList1.item(idx).selectSingleNode("Pages").text);

document.write(" ISBN: " + xmlNodeList1.item(idx).selectSingleNode("ISBN").text );

document.write("<br />")

}

}

}

function getXMLDocument(name)
{
requester.open("GET", name, false);
requester.send(null);
return requester.responseXML;
}


</script>
</head>

</html>

No comments: