Using XML
 

XML (Extensible Markup Language) is designed to hold data in user-defined tags. XML doesn't contain any predefined tags, you must create your own tags, which can hold the required data.
It's important to understand that XML does not perform any task by itself. Its only usage is to store information in a portable way. You can imagine an XML file to be like a plain text database file. The XML-tags are similar to the tables of a database. In a database you will also have to define your tables, which will hold your data.
Here is a sample of a simple XML file:

  <?xml version="1.0" encoding="ISO-8859-1"?>
<EmployeesInfo>
   <Employees>
      <Firstname>Nancy</Firstname>
      <title>Sales Representative</title>
      <city>Seattle</city>
   </Employees>
   <Employees>
      <Firstname>Andrew</Firstname>
      <title>Vice President, Sales</title>
      <city>Tacoma</city>
   </Employees>
   <Employees>
      <Firstname>Janet</Firstname>
      <title>Sales Representative</title>
      <city>Kirkland</city>
   </Employees>
</EmployeesInfo>


In the same way as this, you can enter any number of 'Employee' records.
To load and display the XML data you will need an XML Parser. The Microsoft XML Parser is a COM component and is directly available with IE 5.0. The parser will load the XML file into a document object. Once you have loaded your XML file in the document object, you can easily retrieve the data via a DOM object.
Here is a snippet of code, which will display the data using ASP and VBScript.

  <%
       XMLFile = Server.MapPath("EmployeesInfo.XML")
      Set objXMLDoc = Server.CreateObject("MSXML.DomDocument")
      objXMLDoc.async = False
      objXMLDoc.Load (XMLFile)
      Set docHeadlines = objXMLDoc.documentElement.selectNodes("Employees")
      Dim numNodes, nn
      numNodes = docHeadlines.length
      For i = 0 To numNodes – 1
            Set nn = docHeadlines.nextNode()
            Response.Write (nn.Text + "<br>")
      Nex
%>

 

Using XPath
The above example will display each and every Employee’s. In order to retrieve a specific ‘Employee’ or to display the ‘Employee’s name only we can make use of XPath. You can compare XPath with the SELECT statement of a database. You can use XPath for XML the same way you would use the SELECT syntax to get a specific record from a database. You can say that XPath is the answer, if you need to make a specific selection. I will use the same example as above to show you the usage of XPath:

 

<%
      XMLFile = Server.MapPath("EmployeesInfo.XML")
      Set objXMLDoc = Server.CreateObject("MSXML.DomDocument")
      Call objXMLDoc.setProperty("SelectionLanguage", "XPath")
      objXMLDoc.async = False
      objXMLDoc.Load (XMLFile)

      strQry = Request("Qry")
      xmlQuery = "//*/Firstname[contains(text(),'" + strQry + "')]/parent::*"
      Set docHeadlines = objXMLDoc.documentElement.selectNodes(xmlQuery)
      Dim numNodes, nn
      numNodes = docHeadlines.length
      For i = 0 To numNodes - 1
            Set nn = docHeadlines.nextNode()
            Response.Write (nn.Text + "<br>")
      Next
%>


Using XSL
By now, you have seen how to load data and how to select specific data. But what if we need to display the data in a table or in a form, so that it looks much more user friendly? For this purpose you will want to use XSL. XSL is a language that can transform XML into HTML code. XSL can be used to give your XML data a user friendly look. XSL can also do much more. With XSL you will also be able to sort and filter your XML data. The following example extends the example with the use of XSL:
NOTE: XSL in not compatible with IE 5.0. IE6 fully supports XSL.
Person.xsl

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="Person">
            <tr><td><xsl:value-of select="Name"/></td>
                  <td><xsl:value-of select="Age"/></td></tr>
      </xsl:template>
</xsl:stylesheet>


Here your modified XML file:

 

<%
      XMLFile = Server.MapPath("EmployeesInfo.XML")
      XSLFile = Server.MapPath("EmployeesInfo.xsl")
      Set objXMLDoc = Server.CreateObject("MSXML.DomDocument")

      Call objXMLDoc.setProperty("SelectionLanguage", "XPath")
      objXMLDoc.async = False
      objXMLDoc.Load (XMLFile)

      set xsl=Server.CreateObject("MSXML.DomDocument")
      xsl.async = False
      xsl.load(XSLFile)

      strQry = Request("Qry")
      xmlQuery = "//*/Firstname[contains(text(),'" + strQry + "')]/parent::*"
      Set docHeadlines = objXMLDoc.documentElement.selectNodes(xmlQuery)
      Dim numNodes, nn
      numNodes = docHeadlines.length
      For i = 0 To numNodes - 1
            Set nn = docHeadlines.nextNode()
            Response.Write (nn.transformNode(xsl))
      Next
%>