Приглашаем посетить
Литература 20 век (20v-euro-lit.niv.ru)

Hack 7. Give Your Customers Formatting Control with XSL

Previous
Table of Contents
Next

Hack 7. Give Your Customers Formatting Control with XSL

Hack 7. Give Your Customers Formatting Control with XSL Hack 7. Give Your Customers Formatting Control with XSL

Use PHP's XSL support to enable your customers to design their own pages.

Amazon provides an interesting service to its corporate customers. The customer can skin an Amazon page by providing an XSL stylesheet that formats the XML data about the products, prices, and related data. This means that if you're a corporate customer, you can add your own links and graphics, and even customize the look and feel of Amazon.com to give purchasing pages an integrated look.

This hack does the same with PHP's XSL engine (and no corporate membership is required!). Figure 2-10 shows the flow of XSL processing in this hack (and with XSL in general). The processor takes two inputs. In this case, the input.xml file contains the data for the page, and the format.xsl file contains the formatting for the page, along with specifications for where the data is to be placed. The XSL processor then takes these two inputs and emits XML, HTML, or text.

Figure 2-10. The XSL processing flow
Hack 7. Give Your Customers Formatting Control with XSL


2.6.1. The Code

Save the code in Example 2-11 as conv.php.

Example 2-11. Generating output from XML and XSL
	<?php
	$xml = new DOMDocument();
	$xml->Load( "input.xml" );

	$xsl = new DOMDocument();
	$xsl->Load( "format.xsl" );

	$xslproc = new XSLTProcessor();
	$xslproc->importStylesheet( $xsl );
	print( $xslproc->transformToXML( $xml ) );
	?>

As a sample XML file, save Example 2-12 as input.xml.

Example 2-12. A sample XML file
	<books>
		<book name="Code Generation in Action" />
		<book name="MDA Explained" />
		<book name="PHP in a Nutshell" />
	</books>

Save Example 2-13 and name it format.xsl.

Example 2-13. Some sample XSL to handle formatting
	<?xml version="1.0" encoding="UTF-8"?>
	<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	   <xsl:output method="html" />
	   <xsl:template match="/">
	    <html>
		 <body>
		  <xsl:for-each select="/books/book">
		   <xsl:value-of select="@name" /><br/>
		  </xsl:for-each>
		 </body>
		</html>
	  </xsl:template>
	 </xsl:stylesheet>

2.6.2. Running the Hack

This hack runs using the command-line PHP interpreter. Execute conv.php using this command line:

	% php conv.php>

You should get the result shown in Figure 2-11.

Figure 2-11. Running the XSL interpreter on the command line
Hack 7. Give Your Customers Formatting Control with XSL


This shows the data provided in the books.xml file, formatted into HTML. You could just as easily pipe this output to an HTML file, or set up the script to emit HTML when XML is requested. Whatever your approach, it's easy to take XML and XSL and get HTML (or any other structured markup, such as WML) as a response.

You can provide your own data dynamically from an SQL data source by creating a DOMDocument XML object on the fly that holds the data, allowing even more flexibility. You also might want to allow the XML and XSL to be supplied on the command lineinstead of hardcoding them into the PHPmaking conv.php a useful utility for dynamic web sites.

You can use this code in your own web pages and allow customers to upload XSL stylesheets. "Create a Media Upload/Download Center" [Hack #97] has code for allowing uploads of files and storing them in a local directory. XSLT is a very powerful, if a little cryptic, XML manipulation language. If you get into it, I strongly recommend Michael Kay's excellent book, XSLT 2.0 Programmer's Reference (Wiley).

2.6.3. See Also


Previous
Table of Contents
Next