Is it possible to write PHP code in an XSLT file? Here is an example:

<xsl:template match="property">

<?php echo "Hello World"; ?><br />

<xsl:value-of select="title" /><br />
<xsl:value-of select="island" />, <xsl:value-of select="city" /><br /><br />
Bedrooms: <xsl:value-of select="bedrooms" /><br />
Bathrooms: <xsl:value-of select="bathrooms" />
</xsl:template>

What I am trying to do is dynamically resize an image. I created a script that is called in an image tag and does the resizing. The image tag is declared in the XSLT file like this:

<img src=\"../scripts/resize_image.php?image=$path&max_width=100&max_height_100\" border=\"0\">

The XSLT breaks on the $path variable though. I'm not sure how to get it to recognize $path and pass it to the resize image script. I hope this makes sense, it's a tough one for me to explain!

Thanks for any help.

    if you want the the code was interpreted by php you should specify to the php server the xslt file extention

    after this php will scan xslt for php balise

      the server is apache. what lines need to be added to the httpd.conf file? Is it similiar to the AddType declarations you add to get apache to recognize php?

        yes that it

        AddType application/x-httpd-php .phtml .pwml .php3 .php4 .php .php2 .inc

        you add .xslt

        and this should work

        .. restart the server after modification of file

          Didn't work and I know why. The server is not actually parsing the XSLT file. I'm using PHP's XSLT functions like this:

          // set the filenames
          $xml_file = "home.xml"
          $xslt_file = "home.xsl";
          
          // convert the xslt to a string
          $xml_string = joint(' ', file($xml_file));
          $xslt_string = join(' ', file($xslt_file));
          
          // set up buffers
          $arg_buffer = array("/xml" => $xml_string, "/xslt" => $xslt_string);
          
          // create the XSLT parser
          $xp = xslt_create() or die("Could not create XSLT processor");
          
          // process the two files to get the desired output
          if ($result = xslt_process($xp, "arg:/xml", "arg:/xslt", NULL, $arg_buffer)){
          
          // print output
          echo $result;
          
          }else{
          
          // else display error
          echo "An error ocurred: ".xslt_error($xp)."(error code ".xslt_errno($xp).")";
          
          }
          
          // free the resources occupied by the handler
          xslt_free($xp);
          

          So php transforms the documents and prints out the combined results but it still will not recognize PHP code written in the XSLT file. Has anybody run across this problem before or have any more suggestions?

            in this case i don't have any idea

            sorry 🙁

              You can't use PHP in XSL. I assume you must be using XML with your XSL? Just pass the variables you want to use ($path in this case) into an XML value, and use then display it in the XSL?... Like <xsl:value-of select="path" />

                Write a Reply...