Re: [xsl] XML > XSL > PHP possible?

Subject: Re: [xsl] XML > XSL > PHP possible?
From: "Toshihiko Makita tmakita@xxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 30 Sep 2022 01:22:04 -0000
I'm using following template with xsl:output/@method="html".

It's works fine now in user projects.

B B B <!--
B B B function:B B Processing instruction output template
B B B param:B B B B B prmPiName, prmPiContent
B B B return:B B B B text()+
B B B note:B B B B B B Saxon does not outputs processing instruction as expected in HTML5 output.
B B B B B B B B B B B B B B B <?php o= ?> is converted into <?php o= >
B B B B B B B B B B B B B B B To avoid this error, use xsl:text instead.
B B B -->
B B B <xsl:template name="outputPI" as="text()+">
B B B B B B B <xsl:param name="prmPiName" as="xs:string"/>
B B B B B B B <xsl:param name="prmPiContent" as="xs:string"/>


B B B B B B B <xsl:text disable-output-escaping="yes">&lt;?</xsl:text>
B B B B B B B <xsl:value-of select="$prmPiName"/>
B B B B B B B <xsl:text> </xsl:text>
B B B B B B B <xsl:value-of select="$prmPiContent" disable-output-escaping="yes"/>
B B B B B B B <xsl:text></xsl:text>
B B B B B B B <xsl:text disable-output-escaping="yes">?&gt;</xsl:text>
B B B </xsl:template>


On 9/30/2022 9:50 AM, Michael B Allen ioplex@xxxxxxxxx wrote:
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


--
/*--------------------------------------------------
 Toshihiko Makita
 Development Group. Antenna House, Inc. Ina Branch
 E-Mail tmakita@xxxxxxxxxxxxx
 8077-1 Horikita Minamiminowa Vil. Kamiina Co.
 Nagano Pref. 399-4511 Japan
 Tel +81-265-76-9300 Fax +81-265-78-1668
 Web site:
 http://www.antenna.co.jp/
 http://www.antennahouse.com/
 --------------------------------------------------*/

Current Thread