When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Sunday, August 13, 2000

Using XSL Stylesheets to Translate XML into HTML
By Arno Jansen


This is an explanation of using XSL stylesheets to translate XML into HTML. It is part of another article I wrote: Online Content Creation using ASP, XML and XSL. Here's the situation: I have an XML file and I want to represent the data from that file in two different ways in HTML. I use an ASP page that loads the XML file with either one of the stylesheets. As said, I created two stylesheets, one for presenting the XML data into a viewable HTML file and one to translate it to an HTML form to let surfers edit the contents.

- continued -

For this article I will use the memo-XML file. It has a memo structure which contains a FROM ,TO a SUBJECT line and the MESSAGE. The XML file for this structure looks like this:

<?xml version="1.0"?>
<memo>
  <from>Arno Jansen</from> 
  <to>XML developers</to>
  <subject>Small XML example</subject>
  <message>
    This is a small XML example on using ASP in
    combination with XML and XSL.
  </message>
</memo>

Now that we know how the data is modelled, let's have a look at the XSL stylesheets. First the one for viewing the content. First of all a standard header to indicate that we are dealing with a stylesheet:

<?xml version="1.0"?> 
<xsl:stylesheet 
   xmlns:xsl="http://www.w3.org/TR/WD-xsl"
   xmlns="http://www.w3.org/TR/REC-html40" 
   result-ns="">

This is just standard, but after this point we create our own stuff. When an XML document is parsed by an XML parser, its contents are put in a tree structure similar to that of a filesystem on a computer. In an XML tree there is a root element similar to the rootdirectory on a filesystem, which can be addressed by "/". The root element of an XML file is basically the file itself. To select this rootnode from the XML tree in a stylesheet we create a pattern to select the root node. We can do that by putting <xsl:template match="/"> in our stylesheet. Keep in mind that we need to close the tag also by putting </xsl:template> in the stylesheet.

Now that we have selected the root node we have can do something with it. I do not do anything special with it but processing all childnodes by putting <xsl:apply-templates/> between the opening and closing tag. So this is what the template for the rootnode looks like:

<xsl:template match="/">
  <xsl:apply-templates />
</xsl:template>

The apply templates processes all childnodes and as we saw in the XML file the first childnode is the <MEMO> tag. Because we want HTML output, we can output HTML header information when we find this tag:

<xsl:template match="memo">
  <html>
    <head>
      <title>XML/XSL memo example by Arno Jansen</title>
    </head>
    <body>
      <xsl:apply-templates /> 
    </body>
  </html>
</xsl:template>

In the template we set the title for the page and write the HTML page's header and footer. Between the <BODY> and </BODY> tag, we tell the stylesheet to process its childnodes again. The next tag the XML files shows us is the <FROM> tag, which indicates who the memo is coming from. If the tag is found we want to output the word from: in bold, followed by the value that is between the <FROM> and </FROM> tag. We accomplish that using this template:

<xsl:template match="from">
  <B>
    From:
  </B>
  <xsl:value-of select="."/>
  <br/>
</xsl:template>

A new tag is found in this template, the <xsl:value-of> tag. This tag selects the value of a node in the document tree. It uses the select attribute to indicate which nodevalue should be picked. In this case we want to get the value of the current node (the FROM tag) to be inserted, so we select the dot (.) to select the current node. Again, this is similar to a filesystem where the . in the DIR command indicates the current directory. (Similar to this, two dots (..) indicate an upper level in the tree structure). Because we know that the FROM tag has no sub tags (thus no child nodes), we do not have to add the <xsl:apply-templates/> tag to this template.

In Part 2 we'll look at creating the actual XSL stylesheet that will be responsible for rendering the HTML from the XML data!

  • Read Part 2


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES