[xsl] XML > XSL > PHP possible?

Subject: [xsl] XML > XSL > PHP possible?
From: "Michael B Allen ioplex@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 30 Sep 2022 00:49:41 -0000
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"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="html"
    encoding="UTF-8"
    indent="no"/>
<!--
<xsl:output method="xml"
    omit-xml-declaration="yes"
    encoding="UTF-8"/>
-->

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="processing-instruction('php')">
    <xsl:processing-instruction name="php">
        <xsl:value-of disable-output-escaping="yes" select="."/>
    </xsl:processing-instruction>
</xsl:template>

<xsl:template match="html">
    <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html&gt;
</xsl:text>
    <html lang="en">
        <xsl:apply-templates/>
    </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:  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

Current Thread