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