#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Groningen, NL
    Posts
    42
    Rep Power
    14

    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...)
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    4
    Rep 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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Groningen, NL
    Posts
    42
    Rep Power
    14
    php-expressions are in both, xml-file and xsl-file...
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    4
    Rep 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 09:02 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Groningen, NL
    Posts
    42
    Rep Power
    14

    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 05:26 PM.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    4
    Rep 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...
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Groningen, NL
    Posts
    42
    Rep Power
    14
    Well, I am stuck, still am.

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

    Richie
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    4
    Rep 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...
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Groningen, NL
    Posts
    42
    Rep Power
    14
    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
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Groningen, NL
    Posts
    42
    Rep Power
    14

    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 07:25 AM.

IMN logo majestic logo threadwatch logo seochat tools logo