<?xml version="1.0"?> <?xml-stylesheet href="phpbuilder.xsl" type="text/xsl"?> <?cocoon-process type="xslt"?>
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="phpbuilder"> <xsl:processing-instruction name="cocoon-format">type="text/html" <html> <head> <title>XML/XSL/Cocoon Transformation</title> </head> <body> <p> <h1><div align="center"> <xsl:value-of select="heading" /> </div></h1> </p> <p> <xsl:value-of select="message" /> </p> </body> </html> </xsl:template> </xsl:stylesheet>
Hello World
'); ?>
<?php
/*
I want to print out a greeting to a number of
countries, not just the World, so in no particular order
*/
$countries = array (
"UK", "USA", "France", "Germany", "Holland",
"Belgium", "Spain", "Denmark", "Finland",
"Sweden", "Japan", "China", "New Zealand", "Australia"
);
//Now open up a file on the server called dynamic-phpbuilder.xml
$handle = fopen("./dynamic-phpbuilder.xml","w");
/*
Now we build up a string that we can save out to our file, notice that the
"<?" tags have been separated so we don't confuse the PHP interpreter.
*/
$tofile = "<" . "?" . "xml version=\"1.0\"" . "?" . ">\n";
$tofile .= "<" . "?" . "xml-stylesheet href=\"dynamic-phpbuilder.xsl\" type=\"text/xsl\"" . "?" . ">\n";
$tofile .= "<" . "?" . "cocoon-process type=\"xslt\"" . "?" . ">\n\n";
$tofile .= "<phpbuilder>\n";
$tofile .= "\t<heading>This is a Dynamically Generated XML that has been Transformed using XSLT</heading>\n";
/*
We now do a very simple loop and output our
greeting to all the countries in our example array:
*/
for ($x=0;$x<sizeof($countries);$x++) {
$tofile .= "\t<message>\n";
$tofile .= "\t\t<greeting>" . $countries[$x] . "</greeting>\n";
$tofile .= "\t</message>\n";
}
$tofile .= "</phpbuilder>\n";
//Write the string to our file and close the handle
fwrite($handle,$tofile);
fclose($handle);
//Finally we redirect to the newly created dynamic-phpbuilder.xml file.
header("Location: dynamic-phpbuilder.xml");
?>
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="phpbuilder"> <xsl:processing-instruction name="cocoon-format">type="text/html"</xsl:processing-instruction> <html> <head> <title>PHP-XML/XSL/Cocoon Transformation</title> </head> <body> <p> <h1><div align="center"> <xsl:value-of select="heading" /> </div></h1> </p> <xsl:for-each select="message"> <p> Hello <xsl:value-of select="greeting" /> </p> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>
<xsl:for-each select="message">
this allows us to apply the transformation to any instance of a <greeting> within a <message>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>PHP-XML/XSL/Cocoon Transformation</title>
</head>
<body>
<p><h1>
<div align="center">This is a Dynamically Generated XML that has been Transformed using XSLT</div>
</h1></p>
<p>Hello UK</p>
<p>Hello USA</p>
<p>Hello France</p>