Using ASP to Load and Display XML
XML is a plain‑text format that lets you define custom tags to store data. Because the tags are user‑defined, you can model almost any data structure you need. Think of XML as a lightweight, portable database where each tag functions like a column in a table and each set of nested tags represents a row.
In classic ASP the most common way to work with XML is through the Microsoft XML Parser, a COM component shipped with Internet Explorer 5.0 and later. The parser exposes a Document Object Model (DOM) that you can manipulate from VBScript, giving you access to nodes, attributes, and text values.
To illustrate, let’s build a simple address book. The XML file, persons.xml, looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Persons>
<Person id="1">
<Name>John Doe</Name>
<Email>john.doe@example.com</Email>
</Person>
<Person id="2">
<Name>Jane Smith</Name>
<Email>jane.smith@example.com</Email>
</Person>
</Persons>
Each <Person> node holds a unique id attribute and two child nodes: Name and Email. The file can be extended with as many Person entries as you wish.
Now load the XML in ASP. Create a file named display.asp and paste the following code:
Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument.6.0")
xmlDoc.async = False
xmlDoc.validateOnParse = False
xmlDoc.load(Server.MapPath("persons.xml"))
If xmlDoc.parseError.errorCode
Response.Write "XML load failed: " & xmlDoc.parseError.reason
Response.End
End If
Once the document is loaded, you can traverse its nodes. To print every person’s name and email, use a loop like this:
Set personNodes = xmlDoc.selectNodes("/Persons/Person")
For Each person In personNodes
name = person.selectSingleNode("Name").text
email = person.selectSingleNode("Email").text
Response.Write "
" & name & " <" & email & ">
"Next
This script outputs each record wrapped in a paragraph tag. When you request display.asp in the browser, you’ll see a list of names with their email addresses. Because the XML file is plain text, you can easily update it by adding or removing Person elements, and the ASP page will reflect those changes without touching the code.
Working with XML in ASP feels straightforward once you grasp the DOM. The parser handles parsing errors for you, and the selectNodes method lets you fetch a collection of nodes that match an XPath expression. The next section will dive deeper into XPath and show how you can pull just a single person or only specific data fields.
Selecting Specific Data with XPath
XPath is the language that lets you address nodes in an XML document with path expressions. It behaves much like the SQL SELECT clause when you’re used to database queries. Instead of filtering rows with a WHERE clause, XPath uses predicates that you attach to node names.
Using the same persons.xml file, suppose you want to retrieve only the person whose id is 2. The XPath expression would be /Persons/Person[@id='2']. In classic ASP, you can apply it with the selectSingleNode method:
Set person = xmlDoc.selectSingleNode("/Persons/Person[@id='2']")
If Not person Is Nothing Then
name = person.selectSingleNode("Name").text
email = person.selectSingleNode("Email").text
Response.Write "
" & name & "
"Response.Write "
Email: " & email & "
"Else
Response.Write "
Person not found.
"End If
That single line of code returns exactly the data you need. You can also use XPath to fetch all names only, leaving out the email addresses. The expression /Persons/Person/Name selects every Name node. Looping over that set looks like this:
Set nameNodes = xmlDoc.selectNodes("/Persons/Person/Name")
For Each nameNode In nameNodes
Response.Write "
- " & nameNode.text & " "
Next
For more complex filtering, XPath supports functions such as contains(), starts-with(), and arithmetic comparisons. For example, to find a person whose email ends with @example.com, you could use /Persons/Person[ends-with(Email, '@example.com')]. The MSXML parser supports a rich subset of XPath 1.0, so you can write sophisticated queries that would otherwise require a full database backend.
Because XPath is declarative, you can keep your ASP code clean. The query string fully describes the data you want, and the parser returns the exact node set. This separation of concerns makes maintenance easier: you change the XML structure without touching the ASP logic, as long as the XPath expression still points to the correct nodes.
In practice, developers often combine XPath with other XML techniques. For instance, you might fetch a subset of nodes, then transform them into HTML with XSLT. XPath gives you the precise selection; XSLT turns that selection into a presentable format. The following section demonstrates how to harness XSL to output a user‑friendly table.
Transforming XML to HTML with XSL
While the ASP loop above can generate HTML, writing markup inline makes the code verbose and hard to read. XSLT, the XML Stylesheet Language for Transformations, offers a clean separation: you keep the XML data untouched and use a stylesheet to describe how that data should appear in HTML.
In Internet Explorer, XSL support is built into the MSXML library. IE5 had limited support, but starting with IE6 you get full XSLT 1.0 compliance. If you target modern browsers, you can rely on their native XSLT processors or use JavaScript libraries.
Let’s create a stylesheet, persons.xsl, that turns the persons.xml data into an HTML table:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Address Book</title>
<style>
table {border-collapse: collapse; width: 100%;}
th, td {border: 1px solid #ccc; padding: 8px; text-align: left;}
th {background-color: #f2f2f2;}</style>
</head>
<body>
<h1>Address Book</h1>
<table>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<xsl:apply-templates select="Persons/Person"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Person">
<tr>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="Email"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
Notice that the stylesheet is pure XML. The <xsl:apply-templates> call tells the processor to apply the Person template to each person node, which outputs a table row with the name and email.
To apply this stylesheet in ASP, you can use the XSLTemplate object:
Set xslTemplate = Server.CreateObject("Msxml2.XSLTemplate.6.0")
Set xslDoc = Server.CreateObject("Msxml2.FreeThreadedDOMDocument.6.0")
xslDoc.async = False
xslDoc.load(Server.MapPath("persons.xsl"))
xslTemplate.stylesheet = xslDoc
Set processor = xslTemplate.createProcessor()
Set xmlDoc = Server.CreateObject("Msxml2.FreeThreadedDOMDocument.6.0")
xmlDoc.async = False
xmlDoc.load(Server.MapPath("persons.xml"))
processor.input = xmlDoc
processor.transform = True
If processor.output
Response.ContentType = "text/html"
Response.Write processor.output
Else
Response.Write "Transformation failed."
End If
When you navigate to this ASP page, you’ll see a nicely formatted table with names and email addresses. The HTML is generated by the XSLT engine, not by the ASP code, which keeps your presentation logic separate from data handling.
Beyond simple tables, XSLT lets you sort, filter, or group data before rendering. You could add <xsl:sort select="Name"/> inside the Person template to display records alphabetically. Because XSLT works on XML, you can reuse the same stylesheet across multiple data sets, provided the structure matches.
To wrap up, you now have three key tools: the DOM for loading XML in classic ASP, XPath for selecting exactly what you need, and XSLT for turning that data into polished HTML. Together they enable rapid development of data‑centric web applications without a heavy database stack.
Download the complete example, including persons.xml, persons.xsl, and the ASP pages, here.
Article appeared at Codefinger.de – founded in 2000 by Sonu Kapoor. Sonu studied E‑Commerce in India and now lives in Germany. After graduation he worked for several German firms and currently serves as a Network Consultant and Software Developer. His experience spans VC++, ASP, ASP.NET, XML, and XSL. He writes for sites like
Tags





No comments yet. Be the first to comment!