XML Class error
Posted: Thu Dec 10, 2009 6:50 am
Following on from a previous post (viewtopic.php?f=1&t=110020) I have the following as home.php:
And the following as classes.php:
I was hoping that when $xml_data->output() and $xsl_data->output() were called they would pass strings to loadXML() and all would work. Instead, I got this to the browser:
I also strangely got "Empire Burlesque" set as the HTML page title (proabably a namespace issue).
Once again, I'd be grateful if someone could point out where I've gone wrong! I think that XSLT is failing because it is being passed empty strings (indicating an error) BUT I can't see how this is when I am passing a class function that outputs a string (which in both XML and XSL cases are carbon copies of code that work when their respective files are called using DomDocument->load('filename.xml').
Thanks in advance for any help/tips/pointers!
Code: Select all
<?php
require('classes.php');
$xml_data = new xml_data();
$xsl_data = new xsl_data();
// Load XML
$xml = new DomDocument;
$xml->loadXML($xml_data->output());
// Load XSL
$xsl = new DomDocument;
$xsl->loadXML($xsl_data->output());
// Transform file
$xp = new XsltProcessor();
$xp->importStylesheet($xsl);
echo $xp->transformToXML($xml);Code: Select all
<?php
class xml_data {
public function output() {
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<catalog>\n";
echo "<cd>\n";
echo "<title>Empire Burlesque</title>\n";
echo "<artist>Bob Dylan</artist>\n";
echo "<country>USA</country>\n";
echo "<company>Columbia</company>\n";
echo "<price>10.90</price>\n";
echo "<year>1985</year>\n";
echo "</cd>\n";
echo "</catalog>\n";
}
public function __destruct() {
return (NULL);
}
}
class xsl_data {
public function output() {
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n";
echo "<xsl:template match=\"/\">\n";
echo "<html>\n<head>\n";
echo "<title>Output</title>\n";
echo "</head>\n<body>\n";
echo "<h2>My CD Collection</h2>
<table>
<tr>
<th>Title</th>
<th>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>";
echo "</body>\n</html>\n";
echo "</xsl:template>\n</xsl:stylesheet>";
}
public function __destruct() {
return (NULL);
}
}Code: Select all
Bob Dylan USA Columbia 10.90 1985
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Empty string supplied as input in /var/www/html/home.php on line 9
My CD Collection
Title Artist
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Empty string supplied as input in /var/www/html/home.php on line 13
Warning: XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: compilation error in /var/www/html/home.php on line 17
Warning: XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: xsltParseStylesheetProcess : empty stylesheet in /var/www/html/home.php on line 17
Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: No stylesheet associated to this object in /var/www/html/home.php on line 19Once again, I'd be grateful if someone could point out where I've gone wrong! I think that XSLT is failing because it is being passed empty strings (indicating an error) BUT I can't see how this is when I am passing a class function that outputs a string (which in both XML and XSL cases are carbon copies of code that work when their respective files are called using DomDocument->load('filename.xml').
Thanks in advance for any help/tips/pointers!