'How to write PHP in XSLT

I have been trying for the longest time to write a simple php echo statement in a .xsl file, but every site that I look at does not seem to function for me when I enter a processing-instruction tag. I even tried changing the namespace for php and I am not sure why my code is not outputting the echo statement. Here is my .xsl file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:php="http://php.net/xsl"
    exclude-result-prefixes="php" version="1.0">
    <xsl:output omit-xml-declaration="yes" method="html" />
    <xsl:template match="/">
        <html> 
            <head>
            </head>
            <body bgcolor="#000">
                    <div class="main" style="background:lightblue;">
                        <xsl:processing-instruction name="php">
                        echo 'OK IT DOESN't WORK!!!!!!!!!!!!!!!!!!!';
                        </xsl:processing-instruction>
                        <xsl:for-each select="webpage/content/main">
                            <a href="{link}" style="color:#000; text-decoration:none;">
                                <h1><xsl:value-of select="heading" /></h1>
                                <h2><xsl:value-of select="subheading" /></h2>
                                <div class="img">
                                    <img src="../images/{image}" width="100%" height="auto"/>
                                </div>
                                <xsl:value-of select="description" />
                            </a>
                        </xsl:for-each>
                    </div>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

I also attempted to use the following tag to surround the html tags and then used the processing-instruction tag anywhere inbetween:

<xsl:result-document href="example.php" method="html">
</xsl:result-document>

AND Still No Luck. Can someone please show me what I am doing wrong?



Solution 1:[1]

The reason why your xsl:processing-instruction doesn't work is simple.
You defined your output method as html with your

<xsl:output omit-xml-declaration="yes" method="html" />

instruction. But HTML does not have processing-instructions!
So simply change the method attribute of the xsl:output to "xml" and your code should work as expected.

Summary:
HTML does not have processing-instructions. Only XML does. With this change, some part of the output looks like

<?php 
                        echo 'OK IT DOESN't WORK!!!!!!!!!!!!!!!!!!!';
                        ?>

which is (kind of) a valid processing instruction.
P.S.: I just tested it: xhtml also doesn't work as output method.

Solution 2:[2]

The XSLT and serialization specifications (all versions) say that this should produce the output <?php echo 'OK IT DOESN't WORK!!!!!!!!!!!!!!!!!!!';> (plus some whitespace). This isn't actually useful; it was defined this way for historic reasons that are difficult to recall. HTML5 attaches no meaning to processing instructions, and they are serialized in a format that's not even useful for PHP.

I don't know why you aren't seeing this output, but the question is somewhat academic since the correct output wouldn't be useful to you anyway.

If I recall correctly, the XML output method isn't that useful for generating PHP scripts either, since PHP is not in the general case well-formed XML.

Solution 3:[3]

I would use this approach:

Make the xslt using the xsl:param and feed those with php.

php:

<?php
$xmlUri      = '/some/path/to/your.xml';
$xslUri      = '/some/path/to/your.xsl';
$xmlDocument = new DOMDocument;
$xslDocument = new DOMDocument;

if ($xmlDocument->load($xmlUri) && $xslDocument->load($xslUri)) {

    $xsltProc = new XSLTProcessor();
    $xsltProc->setParam('pi', "OK IT DOESN't WORK!!!!!!!!!!!!!!!!!!!");
    if ($xsltProc->importStyleSheet($xslDocument)) {
        echo $xsltProc->transformToXML($xmlDocument);
    }
}

Your xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:param name="pi"/>
  <xsl:output omit-xml-declaration="yes" method="xml" />
  <xsl:template match="/">
    <html> 
      <head>
      </head>
      <body bgcolor="#000">
        <div class="main" style="background:lightblue;">
          <xsl:processing-instruction name="php">
            <xsl:value-of select="$pi"/>
          </xsl:processing-instruction>
          <xsl:for-each select="webpage/content/main">
            <a href="{link}" style="color:#000; text-decoration:none;">
              <h1><xsl:value-of select="heading" /></h1>
              <h2><xsl:value-of select="subheading" /></h2>
              <div class="img">
                <img src="../images/{image}" width="100%" height="auto"/>
              </div>
              <xsl:value-of select="description" />
            </a>
          </xsl:for-each>
        </div>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Michael Kay
Solution 3 Siebe Jongebloed