Page 1 of 1

XML Class error

Posted: Thu Dec 10, 2009 6:50 am
by st89
Following on from a previous post (viewtopic.php?f=1&t=110020) I have the following as home.php:

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);
And the following as classes.php:

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);
  }
}
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:

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 19
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! :D

Re: XML Class error

Posted: Thu Dec 10, 2009 11:45 am
by AbraCadaver
Your functions don't return anything, they just echo to the browser. Try:

Code: Select all

public function output() {
    $content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    $content .= "<catalog>\n";
    $content .= "<cd>\n";
    $content .= "<title>Empire Burlesque</title>\n";
    $content .= "<artist>Bob Dylan</artist>\n";
    $content .= "<country>USA</country>\n";
    $content .= "<company>Columbia</company>\n";
    $content .= "<price>10.90</price>\n";
    $content .= "<year>1985</year>\n";
    $content .= "</cd>\n";
    $content .= "</catalog>\n";
 
    return $content;
  }
I would recommend heredoc: http://us3.php.net/manual/en/language.t ... ax.heredoc

Re: XML Class error

Posted: Thu Dec 10, 2009 7:08 pm
by st89
AbraCadaver wrote:Your functions don't return anything, they just echo to the browser. Try:

Code: Select all

public function output() {
    $content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    $content .= "<catalog>\n";
    $content .= "<cd>\n";
    $content .= "<title>Empire Burlesque</title>\n";
    $content .= "<artist>Bob Dylan</artist>\n";
    $content .= "<country>USA</country>\n";
    $content .= "<company>Columbia</company>\n";
    $content .= "<price>10.90</price>\n";
    $content .= "<year>1985</year>\n";
    $content .= "</cd>\n";
    $content .= "</catalog>\n";
 
    return $content;
  }
I would recommend heredoc: http://us3.php.net/manual/en/language.t ... ax.heredoc
Thanks it now works!!!!! If anyone else is reading this, you'll also need to remove all instances of \n to avoid errors too (I think because DomDocument->loadXML(); just takes one large string, irregardless of formatting/whitespace).