The Test Page
The Test Page
Now let's see this all at work by looking at the test page, which is included in the source download as default.asp. We will use the JavaScript alert() function to display the XML because if we show it in the browser the tags will not be visible.
When it loads, the test page will invoke the init() function. It will create two instances of the XML DOM, one for the XML source document, and the second for the dynamic XSL file. The last thing it does is apply the XSL to the source XML, and saves the resulting string in the "processed" variable. This is our translated XML. The full code is shown here:
<html>
<head>
   <link REL="stylesheet" TYPE="text/css" HREF="list.css">
</head>
<body>
<hr color=red>
<button onclick='alert(source.xml);'>1. Show incoming.xml</button>
<button onclick='alert(dynstyle.xml);'>2. Show dynstyle.xsl</button>
<button onclick='alert(processed);' id=button1 name=button1>
   3. Show Translation
</button><br>
<hr color=red>
<script FOR="window" EVENT="onload">
 init();
</script>
<script>
var source;
var sourceName = "incoming.xml";
var dynstyle;
var dynstyleName = "interpreter.asp";
var processed = "";
function init(){
   // Do init stuff. Called by the parent frame.
   source = new ActiveXObject('Microsoft.XMLDOM');
   source.async = false;
   source.load(sourceName);    Â
   // did the XML file load OK?
   if (source.parseError.errorCode != 0){
       msg = 'Error loading SOURCE file.'
       msg += '\nDescription: ' + source.parseError.reason Â
       msg += '\nSource text: ' + source.parseError.srcText
   }
   root = source.documentElement;
   dynstyle = new ActiveXObject('Microsoft.XMLDOM');
   dynstyle.async = false;
   dynstyle.load(dynstyleName)
   // did the XML file load OK?
   if (dynstyle.parseError.errorCode != 0){
       msg = 'Error loading DYNSTYLE file.'
       msg += '\nDescription: ' + dynstyle.parseError.reason Â
       msg += '\nSource text: ' + dynstyle.parseError.srcText
   }
   processed = source.transformNode(dynstyle)
}
</script>
</body>
</html>
I have provided three buttons on the test page that show the three entities that make up this example:
q Button 1 shows the original un-translated source document (incoming.xml)
q Button 2 shows the dynamically-generated XSL returned as a result from interpreter.asp
q Button 3 shows the translated version of the source document, after the style has been applied to it.
How it Works
The following screen shot shows the un-translated source document:
|
This screen shot shows our dynamically generated XSL:
|
This final screen shot shows the result of the translation:
|
Summary
In this chapter, we have seen a way to use ASP to generate an XSL file dynamically. We did this by using an ASP page to create an instance of the XML DOM and employing DOM methods to build the result tree. The information needed to build the result tree came from the contents of an XML file. A test web page then used the dynamic XSL to translate from one XML structure to another.
So now we have finished our discussion on styling XML for our web browsers. We will use XSL frequently as we progress through this book. In the next chapter we shall move on to storing and retrieving XML data from databases using ActiveX Data Objects (ADO).
Produced by Michael Claßen
All Rights Reserved. Legal Notices.
URL: https://www.webreference.com/xml/resources/books/professionalaspxml/40281004.htm
Created: Jan. 05, 2001
Revised: Jan. 05, 2001