First off, for those of you who don't know what Cocoon is, check out
http://xml.apache.org where you can
download the source and read up on it. In short Cocoon is a suite of
Java Servlets that sit on your server waiting for .xml pages and then -
if applicable - it applies the relevant XSL transformations. With one XML
page and different XSL pages you can deliver rendered output suitable for
your Browser, for example HTML for your Desktop or WML for your WAP device.
I've been playing with Cocoon as a "learning" project for some time now but
my main problem has been that of generating the XML dynamicly by using PHP
and then using Cocoon to do the transformations. You cant embed PHP into a
.xml page because Cocoon doesn't recognise it and to write XSP's I needed to
learn Java, which at the current time is not an option for me.
Prompted by a few postings to the PHP4Beta list I realised that it would be
possible to employ a workaround and still use PHP, so I set off to see if I
could get it to work. Needless to say I did, so here is a very simple example
of using PHP to generate XML dynamically and then using Cocoon to give me browser friendly mark-up.
Stage 1) Static XML
How to write XML is outside of the scope of this article, but here is a simple XML page called phpbuilder.xml so you can see how it is laid out.
Initially we have our "Prolog" including a declaration that tells Cocoon that we want to do an XSLT transformation:
Notice that the HTML is embedded in the Stylesheet with references to my "heading"
and my message
Also notice that all of my HTML tags have their end equivalents, e.g. </p>.
You cannot get away without closing a particular tag with XML as it does not conform, if you had a
tag such as <br>, it would be written <br/> which indicates that it is an empty tag.
By saving the above example out as phpbuilder.xsl and navigating to my phpbuilder.xml file on a server with Cocoon installed, I would get the resulting HTML:
Cocoon Static Transformation
This is an XML/XSL Transformation Stage 1
Stage 2) Bring on the PHP
Ok, so this is all well and good, but Its static mark-up, I want to use PHP to generate the XML dynamically, so lets look at the next example, phpbuilder.php:
I want to print out a greeting to a number of
countries, not just the World, so in no particular order
The only difference here is the
this allows us to apply the transformation to any instance of a <greeting> within a <message>
So navigating to phpbuilder.php gives us the following markup to our browser:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
<div align="center">This is a Dynamically Generated XML that has been Transformed using XSLT</div>
To summarise, we have started with a php script that writes some valid XML to a file and then
redirects to that .xml page. As it's .XML Cocoon wakes up and starts to parse it, combined
with the XSL we have browser friendly code that was Dynamically generated by PHP.
Create a new Stylesheet for WML and Cocoon will allow you to use the same php code to make a WAP freindly page =)