XML Strengths and Weaknesses with DOM, ASP and XSL - Our XML example explained
(Page 4 of 5 )
Firstly, we connect to SQL Server 2000 using a system DSN. The DSN is called "pubs", and you should create the DSN using control panel. It should connect to your SQL server, more specifically to its pubs database. We instantiate an ADO connection object, passing in the DSN to its open method:
'Open database connection
Set conn = Server.CreateObject("ADODB.Connection")
dsn = "DSN=pubs;UID=sa;PWD="
conn.Open dsnOnce we're connect to our database, we create a new XML document and assign it a root element called "Hi-Tech". We then proceed to retrieve a recordset from the authors table of our pubs database:
If (xmldoc.childNodes.length = 0) Then
' Build the XML document
Set root = xmldoc.createNode("element", "Hi-Tech", "")
xmldoc.appendChild (root)
' Queries the database for customer data
Sql = "select au_lname,au_fname,au_id from authors"
Set rs = conn.Execute(Sql)
rs.MoveFirstWe then loop through each record in the recordset, appending its title, titleID and royalty fields to the XML document that we created earlier. We use the createNode and appendChild methods to do so:
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)Once we've retrieved each of the records from the recordset and appended them to our XML document, we save the XML document to our local machine using MSXML's save method:
xmldoc.save server.mappath("saved.xml")We're now at the point where we have an XML file called saved.xml, as well as the style sheet that's included in the support material for this article, called saved.xml. We instantiate a new XMLDOM object for each of these files, calling the transformNode method of the XML DOM object with a reference to the XML DOM object that contains the XSL file:
sourceFile = Server.MapPath("saved.xml")
styleFile = Server.MapPath("saved.xsl")
set source = Server.CreateObject("Microsoft.XMLDOM")
source.async = false
source.load(sourceFile)
set style = Server.CreateObject("Microsoft.XMLDOM")
style.async = false
style.load(styleFile)Lastly, we use Response.Write to output the transformed XML to the browser:
Response.Write source.transformNode(style)Next: Conclusion >>
More ASP Articles
More By Nakul Goyal