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

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

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

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

Vikram Vaswani

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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















Using Named Buffers



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) .
")";
}
// 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.




/ 84