XML and PHP [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

XML and PHP [Electronic resources] - نسخه متنی

Vikram Vaswani

نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
افزودن یادداشت
افزودن یادداشت جدید

PHP and XSLT

Although there is nothing to stop you from creating an XML document and a corresponding XSLT stylesheet, you typically need to rely on a third-party XSLT processor (such as Sablotron) to combine the two. Web browsers today have limited XML parsing capabilities, and even less support for XSLT stylesheet processing; this implies that an intermediary layer, such as PHP, is required to perform the task of formatting XML data per XSLT-specified template rules.

PHP 4.0.6 (and later) comes with an XSLT extension designed specifically to provide a consistent interface to different XSLT processors (Sablotron, Xalan, and so on). Created by Sterling Hughes, this XSLT extension works much like an abstraction layer, exposing a generic API that can be used to interact with any compatible XSLT processor. Because the functions defined in this API are common to most XSLT engines, it allows developers to switch between different processors without requiring any changes to the PHP code or application layer.

If you're using a stock PHP binary, it's quite likely that you'll need to recompile PHP to add support for this extension to your PHP build (detailed instructions for accomplishing this are available in Appendix A, "Recompiling PHP to Add XML Support").

As of PHP 4.1.1, the XSLT extension supports only the Sablotron XSLT engine, although you can expect to see support for other XSLT engines in future PHP releases.

Out with the Old . . .

Earlier versions of PHP supported a different Sablotron-specific extension that was activated by adding the --with-sablot option during the build process. That extension has now been superseded by the newer, more generic API described in the "PHP and XSLT" section. If you're using a PHP build earlier than 4.0.6, you should upgrade to the latest version in order to use the functions discussed in this chapter.

A Simple Example

PHP's XSLT extension exposes a number of functions to easily transform XML documents into other formats. Together, these functions can be organized into a standard process that is applicable to all XSL Transformations performed with PHP.

In order to demonstrate this process, consider the PHP script in Listing 4.1 and Listing 4.2 to carry out an XSL Transformation on the server and return the generated result tree to the browser.

Listing 4.3 Performing an XSL Transformation with PHP

<?php 
// set the filenames 
$xml_file = "list.xml"; 
$xslt_file = "list.xsl"; 
// create the XSLT processor 
$xp = xslt_create() or die("Could not create XSLT processor"); 
// process the two files to get the desired output 
if($result = xslt_process($xp, $xml_file, $xslt_file)) 
{
// print output 
echo $result; 
} 
else 
{
// else display error 
echo "An error occurred: " . xslt_error($xp)
 . "(error code " . xslt_errno($xp) . 
")"; 
} 
// free the resources occupied by the handler 
xslt_free($xp); 
?> 

This is much simpler than it looks. Let's go through it step by step:

The first order of business is to create an instance of the XSLT processor, which is accomplished via PHP's xslt_create() function.

$xp = xslt_create() or die("Could not create XSLT processor"); 

The resulting handle is assigned to a variable, and it is used in calls to subsequent XSLT functions.

Next, the stylesheet and XML source need to be processed. PHP offers the xslt_process() function for this purpose, which accepts three arguments: a handle for the XSLT processor, the name of the file containing the XML data, and the name of the file containing the XSLT template rules.

if($result = xslt_process($xp, $xml_file, $xslt_file)) 
{
echo $result; 
} 
else 
{
echo "An error occurred: " . xslt_error($xp) 
. "(error code " . xslt_errno($xp) . 
")"; 
} 

The xslt_process() function is the real workhorse hereit reads the XML source tree, matches it against the rules in the XSLT stylesheet, and outputs a new result tree. The results of the transformation are assigned to a variable ($result in the previous example). Note that for large or complex transformations, the process can take awhile.

Windows on the World

If you're using Windows, you'll need to specify the full path to the XML and XSL files for these examples to work correctly.

Most often, you want to store and display the results of the transformation to the user. If, however, you prefer to write the result tree to a file, you can specify the filename as an optional fourth argument to xslt_process(). For an example, check out Listing 4.20.

If the processing is successful, all that remains is to output the result. If it is not successful, however, the xslt_error() and xslt_errno() functions can be used to obtain error information (see the "Handling Errors" section for more on these functions).

After the processing is complete, the final task is to clean things up by freeing all memory occupied by the handle:

xslt_free($xp); 

And that's it. XSLT processing in three easy steps!

A Closer Look at xslt_process()

If you take a look at the documented API for PHP's XSLT extension, you see that I told a little white lie in my explanation of Listing 4.3. The xslt_process() function is actually capable of accepting six different arguments, not just the three described after Listing 4.3.

These arguments are the following, in order:

Handle representing the XSLT processor

Name of the file or buffer containing XML data

Name of the file or buffer containing XSLT data

Name of the file to which to save the result tree (optional)

Associative array of argument buffers (optional)

Associative array of XSLT parameters (optional)

Named buffers are discussed in the "Using Named Buffers" section.

XSLT parameters are discussed in the "Passing Parameters to an XSLT Stylesheet" section.

/ 84