Web Templates
Web Templates  
Photoshop Layouts  
Flash Templates  
Templates Resource - Web Templates
   
 
  Sign UpPassword
Templates Resource Services Support Specials Members View Cart
 
  Photoshop Tutorials
  HTML Tutorials
  ASP Tutorials
  CSS Tutorials
  Javascript Tutorials
  PHP Tutorials
  XML Tutorials
  Flash Tutorials
  Fireworks Tutorials
  3D Studio Max Tutorials
  Cinema 4d Tutorials
  Graphics
  Website Tools
  Web Templates
  About Templates Resource
  Affiliates Program
  TR Support
  Webmaster Resources
 Columbia Tech Support

WebDesignHelper.co.uk


 

XML Tutorials

Displaying XML with XSL Stylesheets

Beginner

 

Displaying XML with XSL Stylesheets
This article will introduce you to the XSL stylesheet language and how it can be used to display XML as HTML in a browser.

Introduction
XML is increasingly being used to store data on the web. But since XML tags are not pre-defined, browsers do not know how to display them. The purpose of this article is to explain how XSL stylesheets work, and how they can be used to display XML data.

Example Files
XSL (eXtensible Stylesheet Language) is an XML-based stylesheet language, much like CSS is to HTML. Since it's basically a type of XML file, it bears a striking resemblance to XML. Before we look at an XSL file, let's define the XML file we will use throughout this tutorial, example.xml.

<?xml version="1.0" encoding="utf-8"?>
<news>
<item>
<title>Two Companies Form A Merger!</title>
<author>John Doe</author>
<date>09.15.03</date>
<story>
Onlookers were stunned today when two companies merged, for some reason.
No one could be reached for comment.</story>
</item>
<item>
<title>Some Government Guy Talks About Something</title>
<author>Jane Smith</author>
<date>09.02.03</date>
<story>
Passersby were bored when some government guy was overheard talking about something.
No one could be reached for comment.</story>
</item>
</news>

As you can see, our XML file describes news items. Now let's look at the XSL file we will use to display this XML file, example.xsl. We'll first see the entire file, then we'll look at it a little more closely.

1. <?xml version="1.0" encoding="utf-8"?>
2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3. <xsl:template match="/">
4. <html>
5. <body>
6. <xsl:for-each select="news/item">
7. <h4><font color="#0000DD"><xsl:value-of select="title" /></font>
8. by <xsl:value-of select="author" />
9. <xsl:value-of select="date" /><br />
10. <xsl:value-of select="story" /></h4><br />
11. </xsl:for-each>
12. </body>
13. </html>
14. </xsl:template>
15. </xsl:stylesheet>

Inside Stylesheets
Glancing over this code reveals that HTML is dispersed among it. Aside from the HTML, the rest of the code is XSL style tags. Line 1 defines this file as an XML file (if you remember from above, XSL files are a type of XML file). Line 2 defines this file as a stylesheet. Line 3 defines a template. Templates are used to help organize your code. The match parameter holds the XML element for which the template is defining.

It should be noted that XSL stylesheets access XML data as a directory tree, using the "/" operator to traverse the tags, much like a file system. Passing just a "/" will select the entire document. Looking at our example, we have matched our template with the entire document.

Line 6 contains the tag. For you programmers, this should seem obvious. This tag creates a loop and will process the every occurrence of the element passed to the select parameter. Our example is selecting the "news/item" element. This again uses XSL's "directory method" of selecting XML elements. To extract a single value from the XML file, we use the tag. The select parameter, again, represents the XML element holding the value you want.

Displaying The XML File
OK. So know we have our XML file, as well as our XSL stylesheet. There are several ways to display XML data as pretty HTML. I will introduce you to 2 of such ways: direct linking, PHP processing.

Direct Linking
This is by far the simplest way to view your newly stylesheeted ( <= is that a word? ) XML file is to link it explicitly to the stylesheet. To do this simply add this as the second line in the XML file (view this file: example_final.xml):

That's all there is too it. Now point your browser to the xml file and you should be seeing sweet, pure HTML. This method mirrors how HTML files can link in a CSS file.

PHP Processing
This next example of displaying XML data uses PHP. This may prove to be a more useful solution. We will be utilizing PHP's xslt_*() functions. Here's the code from example.php:

<?php
$proc = xslt_create(); // this creates the xslt processor
$xml = "file://example.xml";
$xsl = "file://example.xsl";
$html = xslt_process($proc, $xml, $xsl);
echo $html;
xslt_free($proc);
?>

This little script simply creates an xslt processor, processes the files, and echoes the output. The "file://" appendage on the variables tells the processor that the files are physical. The xslt_process() function will also accept variables holding the actual XML/XSL data.

Summary
Well, we have covered how to display XML data using an XSL stylesheet and PHP. That's all. Go home. Er...you're probably already at your home. Then....stay there.

 

 

 

This tutorial is provided to you by Spoono

More Tutorials
 

XML Tutorials

Commenting and CDATA
This article will teach you about commenting and learning how to write text which you want to go unparsed in XML.

Beginner

Intro to XML
This article will give a brief introduction to XML.

Beginner

RSS - Really Simple Syndication
RSS, or Really Simple Syndication is an XML-based format that allows links, news posts, and other lists to be displayed in an organized manner.

Intermediate

XML Rules
Learn the simple rules of XML.

Beginner

XML Stylesheets
This article will introduce you to the XSL stylesheet language and how it can be used to display XML as HTML in a browser.

Intermediate

More Tutorials

Web Template Preview 92300501

Web Templates ID:

92300501

User Level:

Advanced web template


Web Template Preview 92300601

Web Templates ID:

92300601

User Level:

Advanced web template


Web Template Preview 92300901

Web Templates ID:

92300901

User Level:

Advanced web template


Web Template Preview 92301001

Web Templates ID:

92301001

User Level:

Advanced web template


Templates Resource FAQ Terms of Use Site Map Link To Us

© Copyright 2003-2008 Templates Resource