Re: PHP-sablotron
| From: | Sterling Hughes | Date: | Fri, 01 Sep 2000 14:52:09 +0000 |
| Subject: | Re: PHP-sablotron | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-31576@lists.php.net to get a copy of this message | ||
> Sterling Hughes wrote:
>
> > Great,
> >
> > Lets work on this!
> >
> > I'm in GMT -5 (Ny, US) but I've actually been switching timezones lately
because
> > of all the work I've had to do, now I exist in my own special timezone
called
> > the "I'll be awake no matter when you send me an e-mail timezone" :-). I
> > luckily just finished the last of all the major work this morning and slept
for
> > the entire day (scary).
> >
> > Lets open up the conversation on this module right away, I think the first
thing
> > to discuss would be the PHP API, this is what I'm envisioning for an API:
> >
> > 1) Hook it into the output buffering support so someone could do something
> > like:
> >
> > <?php
> > xslt_output_transform("somefile.xsl");
> >
> > // All output would then be filtered through the style sheet
> > xslt_output_endtransform();
> >
> > ?>
> >
>
> This would be very nice indeed, didn't think of it yet, but maybe we can start
with
> the
> basic API, like the one below.
>
Actually this seems quite easy, just:
PHP_FUNCTION(xslt_output_transform)
{
zval **file;
SABOTLS_FETCH();
if (ZEND_NUM_ARGS() != 1 ||
zend_get_parameters_ex(1, &file) == FAILURE) {
WRONG_PARAM_COUNT;
}
SABLOTG(output_transform_file) = estrndup(Z_STRVAL_PP(file),
Z_STRLEN_PP(file));
php_start_ob_buffer();
}
PHP_FUNCTION(xslt_output_endtransform)
{
char *result;
int ret = 0;
OLS_FETCH();
SABLOTLS_FETCH();
if (OG(nesting_level) == 0) {
RETURN_NULL();
}
char *arguments[] = {
"/_xmlinput", OG(active_ob_buffer).buffer,
"/_output", NULL
};
ret = SablotProcess(SABLOTG(output_transform_file),
"arg:/_xmlinput", "arg:/_output", NULL,
arguments, &result);
if (ret) {
/* Sablot Error Handling */
if (SABLOTG(output_transform_file)
efree(SABLOTG(output_transform_file));
RETURN_NULL();
}
PUTS(result);
php_end_ob_buffer(0);
efree(SABLOTG(output_transform_file));
if (resutl)
SablotFree(result);
}
Off the top of my head...
> >
> > 2) Lets also have the basics =-):
> >
> > int xslt_transform(string xsl_uri, string transform_uri, string result_uri[,
> > array params[, array buffers[, string result]]])
> > Transform an xslt file
> >
> > <?php
> > if (xslt_transform($template, $datafile, 'arg:/result', 0, 0, &$result)) {
> > echo "The results of the transformation: $result";
> > }
> > ?>
>
> Is the "arg:/result" systax used somewhere yet in an extension? I did never
> encounter it.
> I wonder if it's neccesairy to makes this call so difficult, maybe the
_process call
> would suffice.
>
> Why would you want the other parameters exactly?
>
'arg:/' is a Sablotron thing, it says that the variable is not a filename but a
variable to be filled in by the buffers array. Then in the buffers array you
specify it like so:
<?php
$buffers = array("/_result" => "somefile.html");
if (xslt_transform($template, $datafile, 'arg:/_result', 0, $buffers)) {
print "Transformed into $buffers[_result]";
}
?>
> >
> >
> > int xslt_process(string xsl, string data, string result)
> > Transform data and place it result:
> >
> > <?php
> > if (xslt_process($xsl, $data, &$result)) {
> > echo "The result of transformation: $result";
> > }
> > ?>
> >
>
> This one looks neat too.
>
> >
> > 3) And finally, Lets have that nice little resource management api
> >
> > resource xslt_create(void)
> > Create a new xslt_processor
> >
> > <?php
> > $xh = xslt_create();
> >
> > if (xslt_run($xh, 'test.xsl', 'test')) {
> > $data = xslt_fetch_result ($xh);
> > }
> >
> > xslt_free($xh);
> > ?>
> >
> > int xslt_run (resource xslt_handle, string xsl_file, string data_file[,
string
> > result[, array params[, array buffers]]])
> > Parse a xsl file with data_file
>
> Same comments as for xslt_transform here...
>
> >
> >
> > <?php
> >
> > $xh = xslt_create();
> >
> > $buffers = array(xsl_file => 'test.xsl',
> > data_file => 'test');
>
> Wht do you want this?
>
These are named buffers, you tell sablotron what they are and it processes them,
you can then access them using the xslt_fetch_result() function (only, of
course, the result buffer).
You can also use the plain filenames with out buffers and more advanced
features:
<?php
$ret = xslt_run($xh, 'test.xsl', 'test');
if ($ret) {
$data = xslt_fetch_result($xh);
}
xslt_free($xh);
?>
> >
> >
> > $ret = xslt_run($xh, 'arg:/xsl_file', 'arg:/data_file',
> > 'arg:/something', 0,
> > $buffers);
> > if ($ret) {
> > $data = xslt_fetch_result ($xh, 'arg:/something');
> > }
> > xslt_free($xh);
> > ?>
> >
> > int xslt_free(resource xslt_handler)
> > Free a xslt_handler
> >
> > See above examples.
> >
>
> >
> > string xslt_error(resource xslt_handle)
> > Get an error string with the last error on the given handler.
> >
> > <?php
> > $xh = xslt_create();
> > print xslt_error($xh);
> > xslt_free($xh);
> > ?>
>
> Looks like handy to me.
>
> >
> >
> > int xslt_log(resource xslt_handle, string filename[, int errorlevel])
> > Log Sablot messages
> >
> > <?php
> > $xh = xslt_create();
> > xslt_log($xh, "/www/logs/error_log");
> >
> > if (!xslt_run($xh, $xsl, $datafile))
> > print "Error Logged";
> >
>
> This one too.
>
>
> >
> > int xslt_register_sax_handlers(resource xslt_handle, array handlers)
> > Register SAX handlers for event based parsing.
> >
> > <?php
> > function StartDoc ()
> > {
> > print "Document Started";
> > }
> >
> > function EndDoc ()
> > {
> > print "Document Finished";
> > }
> >
> > $handlers = array("Document" =>
> > array("StartDoc", "EndDoc"));
> >
> > $xh = xslt_create();
> > xslt_register_sax_handlers($xh, $handlers);
> >
> > if (xslt_run($xh, 'test.xsl', 'test')) {
> > $data = xslt_fetch_result ($xh);
> > }
> > xslt_free($xh);
> > ?>
> >
>
> I didn;t look at the SAX interface yet. I'll have a look at it, but it seems
useful
> for me too.
>
Its pretty easy (I think) to implement, just register our own handlers, then
have a call_user_function.
All of the SAX functions return void, therefore nothing special is required.
-Sterling