Thursday, November 10, 2011

Lets work around with XPath

XPath is nothing more than a language which gives you the capability of finding information in an XML document.
So what is XPath is all about : 
  • XPath is a syntax for defining parts of an XML document
  • XPath uses path expressions to navigate in XML documents
  • XPath contains a library of standard functions
  • XPath is a major element in XSLT
  • XPath is a W3C recommendation
Lets start with a sample demo;
I have created an xml file which contains information about flights schedule and I call it as "flightSchedule.xml" for the moment. 


Following is my xml file:
<!--?xml version="1.0" encoding="utf-8" ?-->
<flightschedule>
  <flight>
    <flightid>flight-001</flightid>
    <flightdestination>India</flightdestination>
    <flighttime>10.45.00AM</flighttime>
    <flightreturntime>15.20.00PM</flightreturntime>
</flight>
  <flight>
    <flightid>flight-002</flightid>
    <flightdestination>Singapore</flightdestination>
    <flighttime>06.00.00AM</flighttime>
    <flightreturntime>12.45.00PM</flightreturntime>
 </flight>
  <flight>
    <flightid>flight-546</flightid>
    <flightdestination>West Indies</flightdestination>
    <flighttime>01.00.00AM</flighttime>
    <flightreturntime>23.00.00PM</flightreturntime>
  </flight>
  <flight>
    <flightid>flight-879</flightid>
    <flightdestination>Australia</flightdestination>
    <flighttime>08.10.00AM</flighttime>
    <flightreturntime>13.30.00PM</flightreturntime>
  </flight>
  <flight>
    <flightid>flight-324</flightid>
    <flightdestination>Japan</flightdestination>
    <flighttime>04.40.00AM</flighttime>
    <flightreturntime>16.00.00PM</flightreturntime>
     </flight>
</flightschedule>


Now lets see how can we access or load this xml file in my .aspx page.
  • We can use "XMLHttpRequest" to load the xml file like the following                                                                                  var xhttp = new XMLHttpRequest();
So, in my example i will show you how to select values from the xml file using XPath. Imaging I want to select all the flight ids. So I can set is as "/flightSchedule/flight/flightId", and I can select the values using var nodes = xml.selectNodes(path);


And here is the output
flight-001
flight-002
flight-546
flight-879
flight-324


Following is the code for the sample demo;
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xPath.aspx.cs" Inherits="xPath.xPath" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp = new XMLHttpRequest();
}
else
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", dname, false);
xhttp.send("");
return xhttp.responseXML;
}
xml = loadXMLDoc("flightSchedule.xml");
path = "/flightSchedule/flight/flightId";
// code for IE
if (window.ActiveXObject)
{
var nodes = xml.selectNodes(path);


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


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


Happy Coding 




No comments:

Post a Comment