XSL Transformation With PHP And Sablotron - An Evening At The Moulin Rouge
(Page 5 of 8 )
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.
Next: Mistakes Happen >>
More XML Articles
More By Harish Kamath, (c) Melonfire