GNOME Bugzilla – Bug 518786
xsl:copy of processing-instruction does not terminate correctly, produces invalid code
Last modified: 2008-02-26 09:35:53 UTC
Matching a processing-instruction and copying the node does not produce the correct output: it will be terminated only with '>' rather than the expected '?>' == Reproduction == This PHP example demonstrates the problem: <?php $xsld = new DOMDocument(); $xsld->loadXML( <<<EOT <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="ISO-8859-1" indent="no" omit-xml-declaration="yes" media-type="text/html"/> <xsl:template match="processing-instruction()"> <xsl:copy/> </xsl:template> </xsl:stylesheet> EOT ); $xh = new XSLTProcessor(); $xh->importStyleSheet( $xsld ); $doc = new DOMDocument(); $doc->loadXML( <<<EOT <?php some code ?> <root></root> EOT ); echo $xh->transformToXML( $doc ); ?> == Workaround == The following workaround is available to handle specific processing instructions. <xsl:template match="processing-instruction('php')"> <xsl:text disable-output-escaping="yes"><?php </xsl:text> <xsl:value-of select="." disable-output-escaping='yes'/> <xsl:text disable-output-escaping="yes">?></xsl:text> </xsl:template>
Created attachment 105962 [details] the PHP code which reproduces the problem
Nope, libxslt behaviour is correct. You parse the XML as XML. In XML PIs are terminated by ?> so the content of the PI does not include the ? character When you serialize back as HTML, in HTML the PIs are just terminated by > so libxml2 serializer does not output a ? So either output as XML (<xsl:output method="xml") or add the ? in the XSLT. No bug here, Daniel
Okay, understood. Switching to XML output does what I intended.