Home arrow XML arrow Page 5 - XSL Transformation With PHP And Sablotron

An Evening At The Moulin Rouge - XML

So you've got your XML, and you've also got an XSLT stylesheet to format it. But how do you put the two of them together? Fear not - you can use PHP's Sablotron extension to perform XSLT transformation of XML data on the server. This article tells you how.

TABLE OF CONTENTS:
  1. XSL Transformation With PHP And Sablotron
  2. Getting Down To Business
  3. Start It Up
  4. Handling Things Better
  5. An Evening At The Moulin Rouge
  6. Mistakes Happen
  7. Publish Or Die!
  8. Endzone
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
November 30, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
What you've just seen is a generic way to process XML data with PHP. It isn't the only one, though; for larger, more complex files, PHP offers yet another method of applying an XSLT stylesheet to your XML.

Consider the following XML file:

<?xml version="1.0"?>




<review id="57" category="2">




<title>Moulin Rouge</title>




<cast>
<person>Nicole Kidman</person>
<person>Ewan McGregor</person>

<person>John
Leguizamo</person>
<person>Jim Broadbent</person>

<person>Richard
Roxburgh</person>
</cast>




<director>Baz Luhrmann</director>




<duration>120</duration>




<genre>Romance/Comedy</genre>




<year>2001</year>




<body>
A stylishly spectacular extravaganza, <title>Moulin Rouge</title>
is hard
to categorize; it is, at different times, a love story, a costume drama,
a
musical, and a comedy. Director <person>Baz Luhrmann</person> (well-known
for
the very hip <title>William Shakespeare's Romeo + Juliet</title>) has
taken
some simple themes - love, jealousy and obsession - and done
something completely
new and different with them by setting them to music.
</body>




<rating>5</rating>




<teaser>Baz Luhrmann's over-the-top
vision of Paris at the turn of the
century
is witty, sexy...and completely unforgettable</teaser>




</review>

And the corresponding XSLT stylesheet to transform this information into a simple Web page:

<?xml version="1.0"?>




<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">




<xsl:template match="/review">
<html>
<head>
<basefont face="Arial"
size="2"/>
</head>
<body>
<xsl:apply-templates select="title"/> (<xsl:apply-templates
select="year"/>)
<br
/>
<xsl:apply-templates select="teaser"/>
<p />
<xsl:apply-templates
select="cast"/>
<br />
<xsl:apply-templates select="director"/>
<br
/>
<xsl:apply-templates select="duration"/>
<br />
<xsl:apply-templates
select="rating"/>
<p>
<xsl:apply-templates select="body"/>
</p>
</body>
</html>
</xsl:template>




<xsl:template match="title">
<b><xsl:value-of select="." /></b>
</xsl:template>




<xsl:template match="teaser">
<xsl:value-of select="." />
</xsl:template>




<xsl:template match="director">
<b>Director: </b> <xsl:value-of
select="." />
</xsl:template>




<xsl:template match="duration">
<b>Duration: </b> <xsl:value-of
select="." /> minutes
</xsl:template>




<xsl:template match="rating">
<b>Our rating: </b> <xsl:value-of
select="." />
</xsl:template>




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




<xsl:template match="person[position() != last()]">
<xsl:value-of select="."
/>,
</xsl:template>




<xsl:template match="person[position() = (last()-1)]">
<xsl:value-of select="."
/>
</xsl:template>




<xsl:template match="person[position() = last()]">
and <xsl:value-of select="."
/>
</xsl:template>




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




<xsl:template match="body//title">
<i><xsl:value-of select="." /></i>
</xsl:template>




<xsl:template match="body//person">
<b><xsl:value-of select="." /></b>
</xsl:template>




</xsl:stylesheet>

Here's the PHP script which brings the two together:

<?php




// XML file
$xmlstringarray = file("movie.xml");




// XSL file
// note that you may need to specify an absolute path here
$xslfile
= "movie.xsl";




// start transforming...
xslt_output_begintransform($xslfile);




for($count=0; $count<sizeof($xmlstringarray); $count++)
{
echo $xmlstringarray[$count];
}




// end transformation and output
xslt_output_endtransform();
?>

I'll start by storing the content of the XML file in an array.

// XML file
$xmlstringarray = file("movie.xml");

Next, I'll store the location of the XSLT file in a variable.

// XSL file
// note that you may need to specify an absolute path here
$xslfile
= "movie.xsl";

Finally, it's time to call the xslt_output_begintransform() function.

xslt_output_begintransform($xslfile);




for($count = 0;$count < sizeof($xmlstringarray); $count++)
{
echo $xmlstringarray[$count];
}




xslt_output_endtransform();

The xslt_output_begintransform() function is a special function which take only one parameter: the location of the XSLT file. Once this function is invoked, the XSLT processor will transform all the content that it encounters as per the rules in the named file until it's told to stop via the function xslt_output_endtransform().

In this case, I've written a simple "for" loop to iterate through the elements of the XML array, printing each element as it finds it. The XSLT processor will transform this output as it is printed, providing the desired result.



This technique comes in particularly handy if you don't have your XML data in a static file, but generate it dynamically - say, from a database, or by combining multiple XML documents into a single file. You can write PHP code to obtain the data and massage it into an XML-compliant format, and then enclose that code in calls to xslt_output_begintransform() and xslt_output_endtransform() to transform it.

 
 
>>> More XML Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: