XSLT processing instruction to use PHP

This is a discussion on XSLT processing instruction to use PHP within the PHP Language forums, part of the PHP Programming Forums category; Hi! I've got me an XSL tranformation stylesheet for my XML file. In the XSL file I now wish ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-05-2004
Hayko Riemenschneider
 
Posts: n/a
Default XSLT processing instruction to use PHP

Hi!

I've got me an XSL tranformation stylesheet for my XML file. In the XSL
file I now wish to use PHP to do some scripting. So I thought I'll use
the PIs like this:

<xsl:processing-instruction name="php">
echo $hello;
</xsl:processing-instruction>

But this just gets ignored. No error, just not processed. Do I need to
enable something (in PHP, Apache) to use these PIs?

This is on an Apache/2.0.49 (Win32) PHP/4.3.6 Server.

Thank you for any pointers.
hayko
Reply With Quote
  #2 (permalink)  
Old 09-05-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: XSLT processing instruction to use PHP

Hayko Riemenschneider wrote:
> Hi!
>
> I've got me an XSL tranformation stylesheet for my XML file. In the
> XSL file I now wish to use PHP to do some scripting. So I thought
> I'll use the PIs like this:
>
> <xsl:processing-instruction name="php">
> echo $hello;
> </xsl:processing-instruction>
>
> But this just gets ignored. No error, just not processed. Do I need to
> enable something (in PHP, Apache) to use these PIs?
>
> This is on an Apache/2.0.49 (Win32) PHP/4.3.6 Server.
>


A processing-instruction does not execute the code within. The following
example:

<xsl:template match="/">
<xsl:processing-instruction name="foo">
<xsl:text>type="txt/xml"</xsl:text>
</xsl:processing-instruction>
</xsl:template>

Only generates the following tag in the output:

<?foo type="txt/xml"?>

Which doesn't do anything.

If you want to use PHP functions in your XSLT stylesheet, you can do this
through the document() function.

See the examples in the following manual page:

http://nl2.php.net/manual/en/functio...e-handlers.php

And yes, this requires the parsing of the stylesheet to be done by PHP's
XSLT extension...


JW



Reply With Quote
  #3 (permalink)  
Old 09-06-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: XSLT processing instruction to use PHP

Janwillem Borleffs wrote:
> And yes, this requires the parsing of the stylesheet to be done by
> PHP's XSLT extension...
>


I have found another way, using the PHP cli and msxsl (an msxml wrapper),
without the need to have the XSLT extension installed:

XSLT (test.xslt):
====
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<?php echo "test"?>
</xsl:template>
</xsl:stylesheet>

XML (test.xml, just to have something to work with):
====
<?xml version="1.0" encoding="iso-8859-1" ?>
<root />

Command line:
==========
> php test.xslt | msxsl -o test.html test.xml -


When you have the short_open_tag directive enabled in your php.ini file, you
can disable it temporary using the -d option:

> php -d short_open_tag=off test.xslt | msxsl -o test.html test.xml -


This way, test.xslt gets parsed by the PHP interpreter first and the output
is catched by msxsl (indicated by the last hyphen). The generated test.html
will contain the following:

<?xml version="1.0" encoding="UTF-16"?>
test

Needless to mention that this example only works on Windows platforms.

Perhaps this is useful to you...


JW



Reply With Quote
  #4 (permalink)  
Old 09-06-2004
Hayko Riemenschneider
 
Posts: n/a
Default Re: XSLT processing instruction to use PHP

On 06.09.2004 00:31, Janwillem Borleffs wrote:

> A processing-instruction does not execute the code within. The
> following example: [..] <?foo type="txt/xml"?>


Thanks for clearing that up. I was under illusion PHP code would be
magically executed. ;-)

> If you want to use PHP functions in your XSLT stylesheet, you can do
> this through the document() function.


I started on the examples. But it seems awefully complicated. Isn't
there an another way to PHP parse the XML or XSL file before it gets
XML/XSLT parsed?

> And yes, this requires the parsing of the stylesheet to be done by
> PHP's XSLT extension...


Yup, I've got them installed. The 'normal' parsing works now, just not
with the extra PHP code inside.

Thanks for the help
hayko
Reply With Quote
  #5 (permalink)  
Old 09-06-2004
Hayko Riemenschneider
 
Posts: n/a
Default Re: XSLT processing instruction to use PHP

On 06.09.2004 01:06, Janwillem Borleffs wrote:

> I have found another way, using the PHP cli and msxsl (an msxml
> wrapper), without the need to have the XSLT extension installed:


[.. example of what I was looking for..]

> Needless to mention that this example only works on Windows
> platforms.


Thank you for looking into that. It is actually exactly what I was
looking for, just without the extra command line piping.

I'm eventually going to run this on a different machine, with just PHP
and hopefully browsers able of XSLT processing.

I wish to grab session specific variables and use them whilst processing
my XML file.

hayko
Reply With Quote
  #6 (permalink)  
Old 09-06-2004
Terence
 
Posts: n/a
Default Re: XSLT processing instruction to use PHP

lets say you have the resultant transformation produced by
http://localhost/transform.php
which produces
<?xml version="1.0"?>
<test><?php echo "test" ?></test>


then to reprocess it, just call it from another PHP script like so
include_once "http://localhost/transform.php";


Of course this requires short open tags to be off. But it is simpler
than anything else. You can use this rechnique for any sort of
processing pipeline but be careful when getting a script to recursively
call itself :)


I haven't tried this myself but I think I read somehwere that protocol
identifiers (like "http://") are supported by include statements. It
should work.
Reply With Quote
  #7 (permalink)  
Old 09-06-2004
Terence
 
Posts: n/a
Default Re: XSLT processing instruction to use PHP

I have not yet come across a legitimate need to re-process with
PHP/XSLT. Issues like this can usually be resolved by superior design.

I assume you are well aware that you can pass strings to the XSLT
processor when you call the xslt_process() function (see last argument).

so, $a = array("msg" => "this is a test");

could be passed to XSLT via

xslt_process(,,,,$a); // insert other params as required (can use NULLs)

and would be used in the XSLT like

<xsl:param name="msg" select="''" />

You MUST include the above line! The value will be overwritten with the
one supplied from PHP.

Reply With Quote
  #8 (permalink)  
Old 09-06-2004
Hayko Riemenschneider
 
Posts: n/a
Default Re: XSLT processing instruction to use PHP

On 06.09.2004 04:00, Terence wrote:

> I have not yet come across a legitimate need to re-process with
> PHP/XSLT. Issues like this can usually be resolved by superior
> design.


I'm working on the design... =)

> I assume you are well aware that you can pass strings to the XSLT
> processor when you call the xslt_process() function (see last
> argument).


I understand, yet I wish to leave out the PHP processing of the XML.
Ideally, I'd only use the XSLT with the XML file and that's it. But I
wish to add some dynamics and with this I wanted to use the session
variables.

Is there no way - other than PHP processing the XML file or using the
windows pipeline example from Janwillem's post - to use a bit of PHP?

I experimented with adding .xml or .xsl files to the preprocessed file list
by my Apache PHP module. As this works fine with .html files, it kills
the browser with .xml or .xls files.

hayko
Reply With Quote
  #9 (permalink)  
Old 09-06-2004
Berislav Lopac
 
Posts: n/a
Default Re: XSLT processing instruction to use PHP

Hayko Riemenschneider wrote:
> On 06.09.2004 00:31, Janwillem Borleffs wrote:
>
>> If you want to use PHP functions in your XSLT stylesheet, you can do
>> this through the document() function.

>
> I started on the examples. But it seems awefully complicated. Isn't
> there an another way to PHP parse the XML or XSL file before it gets
> XML/XSLT parsed?


Sure, just give them an .php extension.

If you do your transformations client-side, chance is that some browsers,
like IE, won't recognize the files as XML -- in that case, and if you're
running the scripts off Apache with MultiView enabled, add the .php
extension to their normal extensions (i.e. file.xml.php and file.xslt.php)
and call them without the .php (e.g. file.xml).

Berislav

--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.


Reply With Quote
  #10 (permalink)  
Old 09-06-2004
Hayko Riemenschneider
 
Posts: n/a
Default Re: XSLT processing instruction to use PHP

On 06.09.2004 14:09, Berislav Lopac wrote:
> Hayko Riemenschneider wrote:
>> Isn't there an another way to PHP parse the XML or XSL file before
>> it gets XML/XSLT parsed?

>
> Sure, just give them an .php extension. If you do your
> transformations client-side, chance is that some browsers, like IE,
> won't recognize the files as XML


This is the case, and I was hoping to do all the XSLT client-side. But
that won't work along with the PHP. Especially since the browser won't
make that callback to ask for the PHP evaluation.

> -- in that case, and if you're running the scripts off Apache with
> MultiView enabled, add the .php extension to their normal extensions
> (i.e. file.xml.php and file.xslt.php) and call them without the .php
> (e.g. file.xml).


That's actually quite nifty. I could use a dummy PHP script to process
the .xml file and then spit it out as output to the browser for the
client-side transformation.

Thanks for the hint!
hayko
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:35 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0