"Attempt to use an XML processing instruction in HTML" error in PHP file



I'm using PHP to read both an .xml and .xsl file on the server, then transformToXML($xml). When I run the resultant HTML5 code through the W3C Markup Validation Service, I get the following validation error:



Saw <?. Probable cause: Attempt to use an XML processing instruction in HTML.
(XML processing instructions are not supported in HTML.) <?xml version="1.0"?>


The problem seems to be that, in addition to the HTML that is being generated through the XSL Transform, the following line is being written into the HTML (and is visible when I view the page source in the browser):



<?xml version="1.0"?>


PHP code:



<!DOCTYPE html>
<html>
<head>
....
</head>
<body>
... some hard-coded HTML here ...

<?php
// Load XML file
$xml = new DOMDocument;
$xml->load('cdcatalog.xml');

// Load XSL file
$xsl = new DOMDocument;
$xsl->load('cdcatalog.xsl');

// Configure the transformer
$proc = new XSLTProcessor;

// Attach the xsl rules
$proc->importStyleSheet($xsl);

echo $proc->transformToXML($xml);
?>

... more hard-coded HTML here ...
</body>
</html>


XML code:



<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
...
</catalog>


XSL code:



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR">
<xsl:template match="/">
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left"="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title" /></td>
<td><xsl:value-of select="artist" /></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Does anyone know how to get around this, or is the real issue that I'm embedding the transformed HTML into the middle of existing hard-coded HTML in the PHP file?


Thanks for any guidance!


No comments:

Post a Comment