Front End XML For ASP Developers - Configuration Files (Page 3 of 5 )
As we see with most commercially available systems today, XML has become the format of choice for application configuration files. Take, for example, products like BEA WebLogic or the Microsoft .NET framework – and you will find that both system-level and application-specific configuration data is stored in an XML format.
Along the same lines, we now see XML used for formatting configuration files in custom-built internal Web and client/server applications, often even replacing such widely used standard as ".ini" files.
Why XML then? Simple. Because it's both easy to read by humans -- and thanks to standard libraries available in all popular languages -- easy to parse within the code.
A good example of this happening is a situation when you need to persist your settings in an ASP application. In this case, using global.asa just isn't good enough – application variables expire because of inactivity, and the data is lost. Let's take a look at an example when XML is used to build a typical configuration file and persist the server hit count tally.
The key to implementing this logic is to load the data into application variables in order to make it available on all pages across the application. This can be accomplished in the Application_onStart procedure of the global.asa file:
The last piece of the puzzle is making sure that the value of the HitCount variable is saved to the XML file before IIS is unloaded from memory. This can be done in Application_OnEnd procedure of global.asa file, as shown below:
Set xmlRoot = Nothing Set objXmlDom = Nothing End Sub
Formatting XML for presentation In most systems today the key to information processing is its immediate availability over the web. So instead of parsing XML data and storing it in a temporary location (be it in memory, structured database or files on the hard drive), XSL templates are used to format XML data into HTML and present it to web clients right there and then.
XSL Stands For eXtensible Style Sheet Language An XSL document is an XML-based template, which is used by an XML parser to format XML data into HTML documents. XSL syntax allows for such programming structures as loops and conditional statements, but is mostly geared towards formatting data for presentation rather than building business logic.
For example, the owner of a Yahoo store may need to review all orders before uploading them to database. Given XML in the earlier code example, it would be very easy to create an XSL style sheet that converts the XML into one big HTML table.
Keep in mind, the parser components still needs to be called in order to accomplish this task. Note that both XML and XSL files must first be loaded into parser components, and only then can the style sheet can be applied. For example:
Set objXml = Server.CreateObject("Microsoft.XMLDOM") objXml.async = false objXml.load(Server.MapPath(".") & "\YahooOrders.xml")
Set objXsl = Server.CreateObject("Microsoft.XMLDOM") objXsl.async = false objXsl.load(Server.MapPath(".") & "\YahooOrders.xsl")
'-- Apply stylesheet now Response.Write objXml.transformNode(objXsl)
The reason for this structure is the case when one XML file can be formatted using different style sheets depending on user preferences. This facilitates the key concept of separating data and presentation layers of the application.