The Power Of Three: XML, XSLT and PHP -- Part III
Ray Hunter on 2002 May 15
Ray Hunter on 2002 May 15
Part 3: The Power of PHP
This is the third and final article in THE POWER OF THREE: XML, XSLT, AND PHP series. We will learn how to bring it all together and develop websites that utilize XML, XSLT and PHP to create a rich and powerful site
Here We Go
Now that PHP is configured for XML and XSLT (Sablotron), we are ready to get some serious coding done. First, we need to recall our two test files.
XML File:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="example.xslt"?>
<person>
<name>
<firstname>John</firstname>
<middlename>Michael</middlename>
<lastname>Stevens</lastname>
</name>
<email>jstevens@company.com</email>
<phone>
<home>999 999-9999</home>
<cellular>888 888-8888</cellular>
</phone>
<department>Software Engineering</department>
</person>
XSLT File:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:stylesheet version="1.0" mlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/person">
<html>
<head>
<title>XML and XSLT Example</title>
</head>
<body>
<table>
<tr>
<td>Personal Information:</td>
</tr>
<tr>
<td><xsl:value-of select="name/firstname" /></td>
<td><xsl:value-of select="name/middlename" /></td>
<td><xsl:value-of select="name/lastname" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td><xsl:value-of select="email" /></td>
</tr>
<tr>
<td><xsl:value-of select="department" /></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Notice that I added another line in the XSLT file:
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
This line allows us to specify the type of output that we want to generate. The output is specified as HTML. We could have specified XML or something else yet, we are concerned with HTML output only.
If we want to check the transformation of the XML file, we can use IE 5.5+ and open the XML file in the browser. Name the XML file "example.xml" and the XSLT file "example.xslt". In the XML file above, we notice the line <?XML-stylesheet type="text/xsl" href="example.xsl"?> which is used to call the XSLT file (example.xsl) for the transformation. We need to make sure the XSLT file is in the same directory as the XML file.
Personal Information
First Name: John
Middle Name: Michael
Last Name: Stevens
Email Address: jstevens@company.com
Home #: 999 999-9999
Cell #: 888 888-8888
Department: Software Engineering
If the output is different then above, we need to check the syntax of the files with the ones above and then retry.
Now, let's get PHP doing this for us. What do we need to do first? We need to understand how XSLT works in PHP. Remember we can review all the functions of XSLT at the PHP.net site: http://www.php.net/manual/en/ref.xslt.php.
Here we go'
First, we create a file called "example.php". This file will be doing all of the XSLT function calls for us. Open the file up for editing and let's add the following code:
<?php
// Creating a XSLT Processor
// "$xsltproc" is the XSLT processor resource.
$xsltproc = xslt_create();
// Processing the two files
$result = xslt_process( $xsltproc, 'example.xml', 'example.xslt' );
// If we have a result then display the contents'
// If not then die and display the error information
if( $result ) {
echo "$result";
}
else {
die( sprintf( "The XSLT processor failed [%d]: %s", xslt_errno( $xsltproc ), xslt_error( $xsltproc ) ) );
}
// Free up the XSLT Processor
xslt_free( $xsltproc );
?>
Let's step through the file:
1. We created an XSLT processor that will handle the transformation for us.
2. Next, we called the function "xslt_process" to process the two files. The XSLT processor will use "examples.xslt" to transform "example.xml". This will give us a "mixed" return value that we assigned to "$result".
3. Check "$result" for a value. (Could do better here'but this is a basic file.)
4. If we have a result then we display the value of "$result" with an echo statement.
5. Else if no result then we error out with an error message and display the XSLT error number.
6. After the If-Else statement we need to clean up the processor. We call the XSLT function "xslt_free" to clean up the "$xsltproc" resource.
Now we put these file on our web server and open up "example.php". We should see the same data that is in the XML data file "example.xml":
Personal Information
First Name: John
Middle Name: Michael
Last Name: Stevens
Email Address: jstevens@company.com
Home #: 999 999-9999
Cell #: 888 888-8888
Department: Software Engineering
That is the basics of XSLT in PHP. Now we can move to something that is a little more difficult.
Let's make our own XML file in PHP and then transform it with our "example.xslt" file. This will allow us to create some dynamic content.
Let me give you the "example.php" file first to review and follow:
<?php
// Creating data fields for XML data
$first = "Ray";
$mid = "BigDog";
$last = "Hunter";
$email = "rhunter@hunterhysteria.com";
$phone = "999 999-9999";
$cell = "777 777-7777";
$dept = "Writing Dept.";
// Creating XML data string
// This needs to be like the XML file (contains xml tags)
$xmldata = "<person><name>"
. "<firstname>$first</firstname>"
. "<middlename>$mid</middlename>"
. "<lastname>$last</lastname>"
. "</name>"
. "<email>$email</email>"
. "<phone>"
. "<home>$phone</home>"
. "<cellular>$cell</cellular>"
. "</phone>"
. "<department>$dept</department>"
. "</person>";
// Create parameter string to pass to the xslt_process function
// We associate xml with the xmldata.
$params = array( 'xml' => $xmldata );
// Create the XSLT processor
$xsltproc = xslt_create();
$result = xslt_process( $xsltproc, 'arg:xml', 'example.xslt', NULL, $params );
if( $result ) {
echo "$result";
}
else {
die( sprintf( "Cannot process XSLT document [%d]: %s", xslt_errno( $xsltproc ), xslt_error( $xsltproc ) ) );
}
xslt_free( $xsltproc );
?>
What did we do there? We added tons of new code. Let's review:
1. We added data fields or variables that contain values that would be in an XML document.
2. Then we created an XML string that is similar to the "example.xml" file that we created earlier. We substituted the variables into the XML string.
3. Next we created an array called "$params" and associated the index "xml" to the "xmldata" string. This will allow me to pass the XML data to the XSLT processor.
4. We created the XSLT processor.
5. We then added some more parameters to the "xslt_process" function. The second argument is the index "xml" from the "$params" array. This allows us to pass values from "$params" to the XSLT processor. We are passing the data in "$xmldata" to the XSLT processor by way of the "$params" array. That is somewhat confusing just know that it works.
6. The third argument to "xslt_process" is the XSLT file that we created.
7. The fourth argument is a NULL value. You can specify a file in which to output your transformation to.
8. The fifth argument is the array "$params" that we created which we are passing to "xslt_process" because it has the XML data in it.
Let's stop here for now. We do not want to get too complicated with this-though we might like to push the limits. Let me suggest some ideas if you want to get more complex in your XML and XSLT dealings. Start by creating complex XSLT files that utilize CSS files or do transformation to PS or PDF files. My favorite is to store data in a database and then create XML files from that data and display it using Sablotron.
For example, you could create a function that returns a specific person that a user is requesting. Access a database and get all the data you need. Create a XML data string with the data and pass it to the "xslt_processor". You could also save the data out to a file for future reference or use it to communicate with another application via XML. That is somewhat complex...but you get my idea. The possibilities of using XML, XSLT, and PHP are endless.
If you do something wild and crazy, send me an email-I'd love to hear about it.
Ray Hunter
rhunter@hunterhysteria.com
