XSLT processing instruction to use PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hayko Riemenschneider

    #1

    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:processi ng-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
  • Janwillem Borleffs

    #2
    Re: XSLT processing instruction to use PHP

    Hayko Riemenschneider wrote:[color=blue]
    > 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:processi ng-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.
    >[/color]

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

    <xsl:template match="/">
    <xsl:processi ng-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:



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


    JW



    Comment

    • Janwillem Borleffs

      #3
      Re: XSLT processing instruction to use PHP

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

      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:styleshe et 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:
      ==========[color=blue]
      > php test.xslt | msxsl -o test.html test.xml -[/color]

      When you have the short_open_tag directive enabled in your php.ini file, you
      can disable it temporary using the -d option:
      [color=blue]
      > php -d short_open_tag= off test.xslt | msxsl -o test.html test.xml -[/color]

      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



      Comment

      • Hayko Riemenschneider

        #4
        Re: XSLT processing instruction to use PHP

        On 06.09.2004 00:31, Janwillem Borleffs wrote:
        [color=blue]
        > A processing-instruction does not execute the code within. The
        > following example: [..] <?foo type="txt/xml"?>[/color]

        Thanks for clearing that up. I was under illusion PHP code would be
        magically executed. ;-)
        [color=blue]
        > If you want to use PHP functions in your XSLT stylesheet, you can do
        > this through the document() function.[/color]

        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?
        [color=blue]
        > And yes, this requires the parsing of the stylesheet to be done by
        > PHP's XSLT extension...[/color]

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

        Thanks for the help
        hayko

        Comment

        • Hayko Riemenschneider

          #5
          Re: XSLT processing instruction to use PHP

          On 06.09.2004 01:06, Janwillem Borleffs wrote:
          [color=blue]
          > I have found another way, using the PHP cli and msxsl (an msxml
          > wrapper), without the need to have the XSLT extension installed:[/color]

          [.. example of what I was looking for..]
          [color=blue]
          > Needless to mention that this example only works on Windows
          > platforms.[/color]

          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

          Comment

          • Terence

            #6
            Re: XSLT processing instruction to use PHP

            lets say you have the resultant transformation produced by

            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.

            Comment

            • Terence

              #7
              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.

              Comment

              • Hayko Riemenschneider

                #8
                Re: XSLT processing instruction to use PHP

                On 06.09.2004 04:00, Terence wrote:
                [color=blue]
                > 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.[/color]

                I'm working on the design... =)
                [color=blue]
                > 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).[/color]

                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

                Comment

                • Berislav Lopac

                  #9
                  Re: XSLT processing instruction to use PHP

                  Hayko Riemenschneider wrote:[color=blue]
                  > On 06.09.2004 00:31, Janwillem Borleffs wrote:
                  >[color=green]
                  >> If you want to use PHP functions in your XSLT stylesheet, you can do
                  >> this through the document() function.[/color]
                  >
                  > 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?[/color]

                  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.


                  Comment

                  • Hayko Riemenschneider

                    #10
                    Re: XSLT processing instruction to use PHP

                    On 06.09.2004 14:09, Berislav Lopac wrote:[color=blue]
                    > Hayko Riemenschneider wrote:[color=green]
                    >> Isn't there an another way to PHP parse the XML or XSL file before
                    >> it gets XML/XSLT parsed?[/color]
                    >
                    > 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[/color]

                    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.
                    [color=blue]
                    > -- 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).[/color]

                    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

                    Comment

                    Working...