Using Named Buffers

I l @ ve RuBoard

Thus far, all the examples you've seen have assumed the XML and XSLT data to be stored in static files. This assumption is true in most cases; however, some occasions do arise when your XML is dynamically generated, either from a database, by combining two or more files, or from some other dynamic data source. In such cases, saving this dynamically generated XML to a file and then processing it with xslt_process() can be a needlessly long and tedious process.

This is where named buffers come in. Named buffers allow you to store your XML and XSLT data in memory and then pass these variables on to xslt_process() for normal XSLT transformation.

As discussed in the "A Closer Look at xslt_process()" sidebar, the xslt_process() function can actually accept up to six arguments. Typically, the second and third arguments are references to the XML and XSLT files; however, they can also be the names of buffers containing the XML and XSLT data.

The following snippet might make this clearer:

 $arg_buffer = array("/xml" => $xml_string, "/xslt" => $xslt_string);  $xp = xslt_create();  $result = xslt_process($xp, "arg:/xml", "arg:/xslt", NULL, $arg_buffer); 

As you can see, I first defined an associative array named $arg_buffer , and set it up with two keys representing the XML and XSL data, respectively. Next, I've used these keys (with the arg: prefix) as arguments to xslt_process() , which reads the data in the corresponding buffer and uses it to perform the transformation.

It should be noted at the outset that this capability is available only if your PHP build uses the Sablotron engine. This is because the arg: URI scheme is a Sablotron-specific extension, and it is not likely to work with other XSLT processors.

When using this technique, it is mandatory to specify the name of the variable containing the buffers as the ( otherwise optional) fifth argument to xslt_process() .

Listing 4.7 is a detailed example to put it all in context.

Listing 4.7 Using Named Buffers
 <?php  // set the filenames  $xml_file = "list.xml";  $xslt_file = "list.xsl";  // convert to strings  $xml_string = join('', file($xml_file));  $xslt_string = join('', file($xslt_file));  // set up buffers  $arg_buffer = array("/xml" => $xml_string, "/xslt" => $xslt_string);  // create the XSLT processor  $xp = xslt_create() or die("Could not create XSLT processor");  // process the two strings to get the desired output  if($result = xslt_process($xp, "arg:/xml", "arg:/xslt", NULL, $arg_buffer))  {       echo $result;  }  else  {       echo "An error occurred: " . xslt_error($xp) . "(error code " . xslt_errno($xp) . graphics/ccc.gif ")";  }  // free the resources occupied by the handler  xslt_free($xp);  ?> 

You should also take a look at Listing 4.12, which demonstrates how named buffers can be used with dynamically generated XML data from a database.

I l @ ve RuBoard


XML and PHP
XML and PHP
ISBN: 0735712271
EAN: 2147483647
Year: 2002
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net