SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    May 2008
    Location
    Amsterdam
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP5 libxslt - Use registerPHPFunctions() and php:function() within <XSL:INCLUDE/> ?

    In PHP4 I have been using the XSLT Sablotron library a lot in various projects. I am now in the proces of changing my applications to make them compatible with PHP5 and run into a serious problem:

    With Sablotron i used to include templates in my xsl templates using the PHP schemeHandler function, like this:

    <xsl:include href="content:link_edit"/>

    In the schemHandler in PHP i select the right function based on the scheme ('content' in above example) and return the xsl template 'link_edit' which is available as a string in PHP (loaded from a database).

    In PHP5 this seems impossible. There is now the option of calling php functions using registerPHPFunctions() and the php namespace, like this:

    <xsl:value-of select="php:function(...)"/>
    <xsl:copy-of select="php:function(...)"/>
    <xsl:variable select="php:function(...)"/>


    But this does not work with <xsl:include href="php:function(...)"/>

    Does anyone have a solution for inlcuding a template directly as a string from php? (so not as a file)

    If this cannot be achieved with PHP5 and the libxslt library i'm forced to stay with PHP4 and the good old Sablotron...

    Hope someone can help, because I really would like to switch to PHP5!

    Thnx!
    Marco

  2. #2
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    365
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP5 is backwards compatible with most aspects PHP4, have you tried running your script as is and see if it works? if not what errors appear?

  3. #3
    SitePoint Member
    Join Date
    May 2008
    Location
    Amsterdam
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by taliesinnz View Post
    PHP5 is backwards compatible with most aspects PHP4, have you tried running your script as is and see if it works? if not what errors appear?
    PHP5 is not backwords compatible with PHP4 in this case. Like i mentioned, the Sablotron module is not available anymore and has been replaced by the Libxslt library.

    Libxslt has a different set of functions and does not have the schemeHandler functionality anymore...

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Finland
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    xsl:include supports streams, maybe you could work it out by writing custom stream.

    http://fi.php.net/manual/en/function...r-register.php

    you can access your stream from xslt like any other url
    <xsl:include href="content://link_edit"/>

  5. #5
    SitePoint Member
    Join Date
    May 2008
    Location
    Amsterdam
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JaskaS View Post
    you can access your stream from xslt like any other url
    <xsl:include href="content://link_edit"/>
    Tnx JaskaS! Looks like a solution, will post my findings later.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Finland
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried it and it works fine. Here is working example:

    PHP Code:
    <?php
    class XslTemplateLoaderStream {
        var 
    $position 0;
        var 
    $template null;

        function 
    stream_open($path$mode$options, &$opened_path)
        {
            
    $url parse_url($path);
            
    $this->template call_user_func(array($this,'getTemplate_'.$url['host']));
            return 
    true;
        }
        function 
    stream_read($count)
        {
            
    $ret substr($this->template$this->position$count); 
            
    $this->position += $count;   
            return 
    $ret;
        }
        function 
    stream_write($data)
        {
            return 
    0;
        }
        function 
    url_stat(){
            return array();     
            
        }
        function 
    stream_eof()
        {
            return 
    true;
        }
        
        function 
    getTemplate_test(){
            return 
    '<?xml version="1.0" encoding="UTF-8"?>
                <xsl:stylesheet version="1.0" 
                         xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                    <xsl:template match="allusers">
                      <html><body>
                        <h2>Users</h2>
                        <table>
                        <xsl:for-each select="user">
                          <tr><td>
                            <xsl:value-of
                                 select="string(uid)"/>
                          </td></tr>
                        </xsl:for-each>
                        </table>
                      </body></html>
                    </xsl:template>
                </xsl:stylesheet>'
    ;
        }
    }

    stream_wrapper_register("xsltpl""XslTemplateLoaderStream");

    $xml = <<<EOB
    <allusers>
     <user>
      <uid>bob</uid>
     </user>
     <user>
      <uid>joe</uid>
     </user>
    </allusers>
    EOB;

    $xsl = <<<EOB
    <?xml version="1.0" encoding="UTF-8"?>
        <xsl:stylesheet version="1.0" 
             xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
        <xsl:output method="html" encoding="utf-8" indent="yes"/>
        <xsl:include href="xsltpl://test"/>
    </xsl:stylesheet>
    EOB;

    $xmldoc = new DOMDocument('1.0');
    $xmldoc->loadXML($xml);
    $xsldoc = new DOMDocument('1.0');
    $xsldoc->loadXML($xsl);

    $proc = new XSLTProcessor();
    $proc->importStyleSheet($xsldoc);
    echo 
    $proc->transformToXML($xmldoc);
    ?>

  7. #7
    SitePoint Member
    Join Date
    May 2008
    Location
    Amsterdam
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ooh, you're fast!! I was just finalizing the required method url_stat() when I saw your post coming in

    My own test works perfectly also now.. just tried it with a global variable where I loaded the xsl template. Will now modify it to load the templates from classes in my project, and load them based on the specified path.

    This is great, thnx for your help!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •