Retrieve quick results with ASP/XML and XSL
Recently I was working on a new Project about ASP along with XML/XSL. I recognized that ASP with XML is a very powerful tool and that was the main reason I wrote this article.
In this article, I will explain how to retrieve XML elements with ASP and how to retrieve specific values from XML files with the help of XPath. I will also introduce the usage of XSL.
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. Its important to understand that XML does not perform any task by itself. Its only usage is to store information that can be sent in a portable way. You can imagine an XML file like a plain text database file. The XML-tags are similar with 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:
This way, you could enter any number of ‘Person’ records you like. 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. 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.
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. We can use the XPath for XML the same way we 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.
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 than now? For this purpose we will use XSL. XSL is a language to transform XML into HTML code. XSL will give your XML data a user friendly look. But XSL can do much more. With XSL you will also be able to sort and filter of any XML data. We will extend our example to see the usage of XSL.
NOTE: XSL in not compatible with IE 5.0. IE6 fully supports XSL.
Here your modified XML file:
After completing the project, I have 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.
Article appeared at Codefinger.de
Codefinger was founded 2000 by Sonu Kapoor. Sonu has studied E-Commerce in India and currently lives in Germany. After graduation he worked for several companies in Germany. He currently works as a Network Consultant and Software Developer. He has written many applications in various programming languages like VC++, ASP, ASP.NET, XML or XSL. Besides his passion for developing applications, Sonu writes articles for several major websites like Developer.com. He also works as a freelancer for CodeGuru.