Hi,
I'm trying to generate PHP from XML. This seems like something that
should be possible but I fear it is not.
Consider the following test.xml > test.xsl > test.php example:
XSL <<<<<<<<<
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
B B B B xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"
B B B B encoding="UTF-8"
B B B B indent="no"/>
<!--
<xsl:output method="xml"
B B B B omit-xml-declaration="yes"
B B B B encoding="UTF-8"/>
-->
<xsl:template match="@*|node()">
B B B B <xsl:copy>
B B B B B B B B <xsl:apply-templates select="@*|node()"/>
B B B B </xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction('php')">
B B B B <xsl:processing-instruction name="php">
B B B B B B B B <xsl:value-of disable-output-escaping="yes" select="."/>
B B B B </xsl:processing-instruction>
</xsl:template>
<xsl:template match="html">
B B B B <xsl:text disable-output-escaping="yes"><!DOCTYPE html>
</xsl:text>
B B B B <html lang="en">
B B B B B B B B <xsl:apply-templates/>
B B B B </html>
</xsl:template>
</xsl:stylesheet>
XML <<<<<<<<<
<html>
<head>
<title>test</title>
</head>
<body>
<header></header>
<main>
<?php
echo '<b>Hello, World!</b>';
?>
<img src="test.png" alt="test"/>
</main>
<footer>
</footer>
</body>
</html>
OUTPUT METHOD HTML <<<<<<<<<
With output method="html":
$ xsltproc -o test.php test.xsl test.xml
$ cat test.php
<!DOCTYPE html>
<html lang="en">
<head><meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>test</title>
</head>
<body>
<header></header>
<main>
<?php echo '<b>Hello, World!</b>';
<img src="test.png" alt="test">
</main>
<footer>
</footer>
</body>
</html>
the trailing '?' is not present which causes PHP to generate an error:
PHP Parse error:B syntax error, unexpected '>', expecting end of file
in /path/to/test.php on line N
OUTPUT METHOD XML <<<<<<<<<
With output method="xml":
$ xsltproc -o test.php test.xsl test.xml
$ cat test.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<header/>
<main>
<?php echo '<b>Hello, World!</b>';
?>
<img src="test.png" alt="test"/>
</main>
<footer>
</footer>
</body>
</html>
the trailing ? is present but the result is not valid HTML because of
the self closing tags in void elements like img, header, img, input,
...
Is there some trick to work-around this?
Mike