Home arrow ASP arrow Page 3 - XML Strengths and Weaknesses with DOM, ASP and XSL
ASP

XML Strengths and Weaknesses with DOM, ASP and XSL


Since the inception of XML, many developers have wondered why they need XML and how they can use XML to their benefit. In this article Nakul looks at some of the terminology that comes with using XML and its related technologies, as well as how to create and transform XML documents with ASP and XSL using Microsoft's MSXML parser.

Author Info:
By: Nakul Goyal
Rating: 5 stars5 stars5 stars5 stars5 stars / 42
March 05, 2002
TABLE OF CONTENTS:
  1. · XML Strengths and Weaknesses with DOM, ASP and XSL
  2. · XML Definitions
  3. · XML and DOM
  4. · Our XML example explained
  5. · Conclusion

print this article
SEARCH DEVARTICLES

XML Strengths and Weaknesses with DOM, ASP and XSL - XML and DOM
(Page 3 of 5 )

Microsoft have provided us with the MSXML parser, which exposes an XML document in the form of a DOM (Document Object Model). With the XML DOM, you can load and parse XML files, gather information about those files, navigate through and manipulate those files. To learn more about the details of the XML DOM, please refer to this site.

Now that we've discussed the reasons for using XML, it's time to look at some source code. We will examine some ASP scripts that create and display XML data. We're going to create an XML file using both static data and data from a database using ADO. The DOM methods createNode and appendChild, as well as the text property are used to construct an in-memory XML tree.

XML with ASP

The following example illustrates how to create an XML tree (in memory) and then persist is to disk using the save method:

<%

Dim xmldoc

Set xmldoc = Server.CreateObject("Microsoft.XMLDOM")

' Check to see if document has data. If it does, don't build it

If (xmldoc.childNodes.length = 0) Then

' Build the XML document

Set root = xmldoc.createNode("element", "Hi-Tech", "")

xmldoc.appendChild (root)

Set onode = xmldoc.createNode("element", "Employee", "")

onode.Text = "Gurpreet Singh"

xmldoc.documentElement.appendChild (onode)

Set inode = xmldoc.createNode("element", "Address", "")

onode.appendChild (inode)

Set child = xmldoc.createNode("element", "Address1", "")

child.Text = "Nepean Ont"

inode.appendChild (child)

Set child = xmldoc.createNode("element", "Address2", "")

child.Text = "Canada"

inode.appendChild (child)

End If

xmldoc.save (Server.Mappath("savedI2.xml"))

%>


In the example above we create an XMLDOM Object. We then create a root node and its child node using the createNode function. Next, we append the nodes after assigning the text property to each of them. Finally, we save the in-memory XML tree to a file, savedI2.xml.

We can also build an XML file from the results of a database query. I've included two files with the support material for this article: pubtest.asp and saved.xsl. Pubtest.asp connects to the SQL Server 2000 pubs database, retrieving several records from the author's table, formatting them as a new XML document and saving that document as saved.xml.

The saved.xsl file contains an XSL style sheet which is used by pubtest.asp to format saved.xml as HTML. You should download the support material before continuing.

Here's an exert from pubtest.asp:

Do While Not rs.EOF

Set onode = xmldoc.createNode("element", "Employee", "")

xmldoc.documentElement.appendChild (onode)

Set inode = xmldoc.createNode("element", "Name", "")

inode.Text = rs.fields(0) & " " & rs.fields(1)

onode.appendChild (inode)

'Grab another recordset based on the authored

Sql = "select title_id,royaltyper from titleauthor " & _

"where au_id = '" & rs.fields(2) & "'"

Set rs2 = conn.Execute(Sql)

If Not (rs2.EOF = True And rs2.bof = True) Then

Set inode = xmldoc.createNode("element", "Titles", "")

onode.appendChild (inode)

Set child = xmldoc.createNode("element", "TitleId", "")

child.Text = rs2.fields(0)

inode.appendChild (child)

Set child = xmldoc.createNode("element", "royalty", "")

child.Text = rs2.fields(1)

inode.appendChild (child)

rs2.Close

Set rs2 = Nothing

End If

rs.movenext

Loop


One of the XML elements that results from the ASP code shown above looks like this:

<Hi-Tech>

<Employee>

<Name>Bennet Abraham</Name>

<Titles>

<TitleId>BU1032</TitleId>

<royalty>60</royalty>

</Titles>

</Employee>


Here's an extract from the XSL style sheet file, saved.xsl:

<xsl:for-each select="Hi-Tech/Employee">

<TR>

<TD WIDTH="40%" BGCOLOR="lightyellow">

<FONT FACE="Verdana" SIZE="2" COLOR="black">

<xsl:value-of select="Name" />

</FONT>

</TD>

<TD WIDTH="30%" BGCOLOR="lightyellow">

<FONT FACE="Verdana" SIZE="2" COLOR="black">

<xsl:value-of select="Titles/TitleId" />

</FONT>

</TD>

<TD WIDTH="30%" BGCOLOR="lightyellow">

<FONT FACE="Verdana" SIZE="2" COLOR="black">

<xsl:value-of select="Titles/royalty" />

</FONT>

</TD>

</TR>

</xsl:for-each>


When I ran pubtest.asp in my browser, here's what it looked like:

The result of combining the XML with an XSL stylesheet

Let's now run through the entire process of retrieving data from the pubs database, saving it to an XML file, and loading and transforming this document as XSL with the MSXML parser.
blog comments powered by Disqus
ASP ARTICLES

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials