July 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Getting Quick Results: XML, XSL, and ASP

  • September 30, 2002
  • By Sonu Kapoor
  • Send Email »
  • More Articles »

Recently I was working on a new project that used ASP along with XML and XSL. I recognized from this project that ASP with XML is a very powerful combination and that others may be interested in seeing how easy this combination is to use.

In this article, I show how to retrieve XML elements using ASP and how to retrieve specific values from an XML files with the help of XPath. I also introduce the usage of XSL.

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"
?><PersonList>
  <Person>
    <Name>Sonu Kapoor</Name>
    <Age>24</Age>
    <Gender>M</Gender>
    <PostalCode>54879</PostalCode>
  </Person>
  <Person>
    <Name>Jasmin</Name>
    <Age>28</Age>
    <Gender>F</Gender>
    <PostalCode>78745</PostalCode>
  </Person>
</PersonList>

In the same way as this, you can enter any number of 'Person' 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 inot 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 JScript. You'll see that VBScript or another language could have been used just as easily.


<%@ Language=JScript%>
<%Server.ScriptTimeout=21478836%>
<%Response.Buffer=false%>

<%
var objXMLDoc = Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
objXMLDoc.async = false;
objXMLDoc.load(Server.MapPath("Person.xml"));

var docHeadlines = objXMLDoc.documentElement.selectNodes("Person");
var numNodes,nn;
numNodes=docHeadlines.length;

for(var i=0;i<numNodes;i++)
{
    nn = docHeadlines.nextNode();
   Response.Write(nn.text+"<br>");
}
%>

Using XPath

The above example will display each and every person. In order to retrieve a specific 'Person' or to display the 'Person'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:


<%@ Language=JScript%>
<%Server.ScriptTimeout=21478836%>
<%Response.Buffer=false%>

<%
var objXMLDoc = Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
objXMLDoc.async = false;
objXMLDoc.load(Server.MapPath("Person.xml"));

var xmlQuery = "/PersonList/Person[Name='Jasmin']";

var docHeadlines = objXMLDoc.documentElement.selectNodes(xmlQuery);
var numNodes,nn;
numNodes=docHeadlines.length;

for(var i=0;i<numNodes;i++)
{
    nn = docHeadlines.nextNode();
   Response.Write(nn.text+"<br>");
}
%>

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:


<%@ Language=JScript%>
<%Server.ScriptTimeout=21478836%>
<%Response.Buffer=false%>

<html><body>
<Center><h2>My Friends</H2>
<table border="1"width="100%">
<tr>
<td width="50%"bgcolor="red"><b>Name</b></td>
<td width="50%"bgcolor="red"><b>Age</b></td>
</tr>

<%
var objXMLDoc = Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
objXMLDoc.async = false;
objXMLDoc.load(Server.MapPath("Person.xml"));

var xsl=Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
xsl.async = false;
xsl.load(Server.MapPath("Person.xsl"));

var xmlQuery = "//PersonList/Person[Name='Jasmin']";
var docHeadlines = objXMLDoc.documentElement.selectNodes("Person");

var numNodes,nn;
numNodes=docHeadlines.length;
for(var i=0;i<numNodes;i++)
{
nn = docHeadlines.nextNode();
Response.Write(nn.transformNode(xsl));
// Response.Write(nn.text+"<br>");
}
%>
</Table>
</body></html>

After completing the project, I learned how to use XML with ASP, how to use XPath and the power of XSL. I hope my article provided you with some useful information, too.

Download the source: download.zip

About the Author:

This Article is provided by Sonu Kapoor and is associated with Codefinger ( www.codefinger.de), a company specializing in Windows Application Development.

# # #






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel