XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 21st, 2002, 01:48 PM
Lionheart Lionheart is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Groningen, NL
Posts: 42 Lionheart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 28 sec
Reputation Power: 8
evaluating php expressions using Sablotron

Hi,

I am using php >4.1, with Sablotron.

I have both an xml and xsl file, and use sablotron to output html. But the php expressions inside these files are not evaluated (i.e. left out in the html-file)

How can they be evaluated for sure?

Richard

(most examples use sablotron with php4.0 and are therefore not of much use...)

Reply With Quote
  #2  
Old May 21st, 2002, 02:49 PM
spaceMonkey spaceMonkey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 4 spaceMonkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Where are the PHP expressions? Inside the XML file or the XSL?

I had a similar issue where I wanted processing instructions inside the XSL to be executed during XSL transformation.

Look at the xslt_set_sax_handlers() function. I believe if you set a processing instruction handler, you can execute the PHP code. xslt_set_sax_hanlders() takes an array of the handlers. You can see an example at the doc page for xslt_set_sax_handler(), but that function is discontinued as of PHP 4.0.6+.

PHP Code:
### XML FILE
<root>
  <
id>1</id>
  <?
php code(); ?>
</root>

### PHP code to transform XML file
<?php
// create an XSLT handle
$xh xslt_create();

// set Processing Instruction handler to process()
xslt_set_sax_handlers(
  
$xh, array('pi'=>'process')
);

// perform transformation
$result xslt_process($xh'xmlfile.xml''xslfile.xsl');

xslt_free($xh);

// processing instruction handler
function process($parser$target$data) {
  if(
$target == 'php') {
    eval(
$data);
  }
}
?> 


This should execute the code() function in that XML document when it's parsed. I'll try this out when I get home to see if I get any errors.

Also, I believe it will execute before the transformation. I don't know how to return a string from the processing instruction handler to be inserted into the resulting document at the point that the instruction is received at.

Reply With Quote
  #3  
Old May 22nd, 2002, 05:21 AM
Lionheart Lionheart is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Groningen, NL
Posts: 42 Lionheart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 28 sec
Reputation Power: 8
php-expressions are in both, xml-file and xsl-file...

Reply With Quote
  #4  
Old May 22nd, 2002, 08:00 AM
spaceMonkey spaceMonkey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 4 spaceMonkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok, it was kind of odd to execute a PI in an XSL file. I had to use the syntax to create a PI in the resulting transformed document:

PHP Code:
<xsl:processing-instruction name="php">
code()
</
xsl:processing-instruction


This would create the code
PHP Code:
<?php code() ?>
in your resulting document and process the PI handler.

Now, if you wanted that to output something in the template, I don't really know how to insert that in the spot where the PI is located at. I didn't get this crap working the way I liked, so I think I'm moving over to Smarty Templates.

I don't know if there's some way to assign the output of the PI to an XSL variable and use it in your stylesheet, or what. If someone could figure that out, I'd be very interested.

Last edited by spaceMonkey : May 22nd, 2002 at 08:02 AM.

Reply With Quote
  #5  
Old May 23rd, 2002, 04:23 PM
Lionheart Lionheart is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Groningen, NL
Posts: 42 Lionheart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 28 sec
Reputation Power: 8
Unhappy

I am this far, and really stuck, right now...

Here is what I have:

index.php:

PHP Code:
<?php

// the files
$path "/www/files_xml/";
$xmlfilename $path "file.xml";
$xslfilename $path "file.xsl";


// Allocate a new XSLT processor
$xslthandler xslt_create() or die("No XSLT-handler available. Aborted.");


// set Processing Instruction handler to process()
// void xslt_set_sax_handlers ( resource processor, array handlers)
xslt_set_sax_handlers($xslthandler, array('pi'=>'PIHandler'));


// process the two files to get the desired output
$result xslt_process($xslthandler$xmlfilename$xslfilename);
if (
$result) {
  echo 
$result;
}
else {
  print 
"Sorry, \$xmlfile could not be transformed by \$xslfile into";
  print 
" the \$result variable. Reason: " xslt_error($xslthandler);
  print 
". Error code: " xslt_errno($xslthandler);
}


// free the resources occupied by the xslthandler
xslt_free($xslthandler);


// processing instruction handler
function PIHandler($parser$data$target) {
  echo 
$data// not working...
  
switch (strtolower($target)) {
    case 
"php":
      eval(
"?>$data"); // not working...
      
echo Date("Y"); // blah...
      
break;
  }
}

?>


processing instruction handler isn't working...
$target contains 'php' when found an pi. But the $data does contains 'blah blah' (no data). Further it does process the code inside the pihandler, but I still can't pass php code to it.

(in php4.0 the arguments were like in: function PIHandler($parser, $target, $data), I changed the order, so that $target contains 'php' when an pi is encountered)
----------------

The xml file contains sometimes pieces of php code like:
PHP Code:
<?php 
  
print "Hi!; 
 ?>


------


The xsl-file contains a piece like:

PHP Code:
<xsl:template match="processing-instruction('php')">
  
hello
  
<xsl:processing-instruction name="php">
   echo 
"hello";
   <!-- <
xsl:value-of select="."/> -->
  </
xsl:processing-instruction>
 </
xsl:template


But nothing seems to work...

------

It's really frustrating: I mean, I just want to evaluate some php code inside the xml and xsl files. What's the big deal? Why should this be so complicated? Please help.

Thanks,
Richard

Last edited by Lionheart : May 23rd, 2002 at 04:26 PM.

Reply With Quote
  #6  
Old May 23rd, 2002, 04:47 PM
spaceMonkey spaceMonkey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 4 spaceMonkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
What's really frustrating is the little help you can get online. I haven't had a chance to play around with your examples...

Reply With Quote
  #7  
Old May 28th, 2002, 01:23 PM
Lionheart Lionheart is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Groningen, NL
Posts: 42 Lionheart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 28 sec
Reputation Power: 8
Well, I am stuck, still am.

Hope you or someone else can shed light on it...

Richie

Reply With Quote
  #8  
Old May 29th, 2002, 11:06 PM
spaceMonkey spaceMonkey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 4 spaceMonkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I can't get the XML PIs to even execute when transforming. As for the XSL ones, I got it to execute the PHP code, but it just echoes "hello" or whatever at the top of the resulting document.

I can't figure out how to store a return value of a function into the resulting document.

Have you checked out Krysalis? It's an XML/XSL engine inspired by Cocoon, but it's completely written in PHP...

Reply With Quote
  #9  
Old May 30th, 2002, 07:25 AM
Lionheart Lionheart is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Groningen, NL
Posts: 42 Lionheart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 28 sec
Reputation Power: 8
I had the same problem (maybe because the data model places xml and PI's at the same level, and starts with PI's?). I made some changes / progress. I will post it later. It is still not complete, but a lot better...

I will at Krysalis. Thanks for the wink.

Richie

Reply With Quote
  #10  
Old May 31st, 2002, 04:06 PM
Lionheart Lionheart is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Groningen, NL
Posts: 42 Lionheart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 28 sec
Reputation Power: 8
Thumbs up Progression

It is working!

(I use Sablotron and php4.1+)
Here is what I have at this moment:

index.php:

PHP Code:
<?php

// the files
$path "/www/files_xml/";
$xml $path "file.xml";
$xsl $path "file.xsl";


// Allocate a new XSLT processor
$xslthandler xslt_create() or die ("No XSLT-handler available. Aborted.");


// set Processing Instruction handler to process()
// void xslt_set_sax_handlers ( resource processor, array handlers)
xslt_set_sax_handlers($xslthandler, array('pi'=>'PIHandler'));


// process the two files to get the desired output
$result = eval ("?>"xslt_process(
                               
$xslthandler,    // resource xh
                               
$xml,            // string xml
                               
$xsl,            // string xsl
                               
NULL,            // string result
                               
array(),         // array arguments
                               
array()          // array parameters
                             
) );


//if (!$result) printf( "Sablotron Error (%s): <br /><strong>%s</strong>", 
//            xslt_errno($xslthandler), xslt_error($xslthandler) );


// free the resources occupied by the xslthandler
xslt_free($xslthandler);


// processing instruction handler
// still not functioning as expected
function PIHandler($parser$data$target) {
  global 
$datum;
  switch (
strtolower($target)) {
    case 
"php":
      
$datum Date("Y"); // this variable one is passed to the output
      
break;
  }
}


echo 
$result;
?>

-----

ABOUT PI-HANDLER:
$target contains 'php' when found an pi. But the $data does contains 'blah blah' (no data).
The 'hello' and 'other 'blahblah' came from the previous PI-handler (the line: echo $data; // not working...)
Fact is, it is still not working as expected... But when given a value to a variable, it will be passed to the html output...

---------

file.xml:
PHP Code:
<?xml version='1.0' encoding="iso-8859-1"?>
<!DOCTYPE sermon SYSTEM "/www/files_xml/file.dtd">
<content>
...
   <p>
     blah bla. blah bla. blah bla. blah bla.
     blah bla. blah bla. blah bla. blah bla.</p>
   <p>
     <?php print "Hi!"?><br />
     <?php print $datum?></p>
   <p>
     blah bla. blah bla. blah bla. blah bla.
     blah bla. blah bla. blah bla. blah bla.</p>

</content>


This file contains php-code, that needs to be evaluated!
Also a piece $datum that got a value from the index.php.
Wow!

---------

file.xsl:
PHP Code:
<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY footer SYSTEM '/www/files_xml/footer_xml'> ]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

 <xsl:template match="/">
  <html>
   <xsl:apply-templates select="content"/> 
  </html>
 </xsl:template>

...

 <!-- Copy all the other source tree nodes. -->
 <xsl:template match="@*|node()|processing-instruction()|comment()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()|processing-instruction()|comment()"/>
  </xsl:copy>
 </xsl:template>

...

 <xsl:template match="processing-instruction('php')">
  <xsl:processing-instruction name="php">
   <xsl:value-of select="."/>
  ?</xsl:processing-instruction>
 </xsl:template>

 <xsl:template match="comment()">
  <xsl:comment><xsl:value-of select="."/></xsl:comment>
 </xsl:template>

...

</xsl:stylesheet>



______________________
QUESTIONs
xsl_file:
PHP Code:
<xsl:copy-of select="document('/www/files_xml/footer.xml')" /> 


QUESTION 1.:
My footer include-file contains <?php ... ?>. This results in a parser error... Who can bring in a solution on this?

---------
xsl_file:
PHP Code:
<p>php statement: <?php print "blahhhh"?></p> 


QUESTION 2.:
My xsl_file contains also <?php ... ?>. This is simply left out in the output. Who can bring in a solution on this?
______________________


Hope this helps you! Hopefully can you give me some help as well!

Richard L.

Last edited by Lionheart : June 1st, 2002 at 06:25 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > evaluating php expressions using Sablotron


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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 On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump