ASP 101 - Active Server Pages 101 - Web04
The Place ASP Developers Go!



Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
The Top 10 ASP Links @ Microsoft.com
What is Adovbs.inc and Why Do I Need It?
An Overview of ASP.NET

QUICK TIP:
Saving Data to an Excel File
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


The Test Page
Back Contents Next

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).


BackContentsNext
©1999 Wrox Press Limited, US and UK.
Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers