简体   繁体   中英

Why is my PHP, XSL, and XML not working?

I have tried a multitude of things and I cannot get anything to load on the screen. The PHP loads the XML on the screen no problem, but when I add the XSL code in the PHP, the screen is then blank, no error or nothing. Can anyone see why?

Thank you

PHP file:

<?php
$doc = new DOMDocument();
$doc->load( 'books.xml' );

$xsl = new DOMDocument;
$xsl->load( 'books.xsl' );

$books = $doc->getElementsByTagName( "book" );

$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

foreach( $books as $book )
{
    $authors = $book->getElementsByTagName( "author" );
    $author = $authors->item(0)->nodeValue;

    $publishers = $book->getElementsByTagName( "publisher" );
    $publisher = $publishers->item(0)->nodeValue;

    $titles = $book->getElementsByTagName( "title" );
    $title = $titles->item(0)->nodeValue;

    echo $proc->transformToXML($doc);

    // echo "$title - $author - $publisher\n";
}    
?>

XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<books>
  <book>
    <author>Jack Herrington</author>
    <title>PHP Hacks</title>
    <publisher>O'Reilly</publisher>
  </book>
  <book>
    <author>Jack Herrington</author>
    <title>Podcasting Hacks</title>
    <publisher>O'Reilly</publisher>
  </book>
</books>

XSL file:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
    <body>
      <h2>My CD Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>author</th>
            <th>title</th>
            <th>publisher</th>
          </tr>
          <xsl:for-each select="books/book">
            <tr>
              <td><xsl:value-of select="author"/></td>
              <td><xsl:value-of select="title"/></td>
              <td><xsl:value-of select="publisher"/></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
  </html>
</xsl:template>

</xsl:stylesheet>

It's very simple , you need only this:

 $xsl = new DOMDocument;
 $xsl->load(  ($xsl_url));
 // Create new XSLTProcessor
 $xslt = new XSLTProcessor();
 // Load xsl stylesheet
 $xslt->importStylesheet($xsl);
 // Load XML input file
 $xml = new DOMDocument;
 $xml->load(  ($xml_url));
 // Transform to string
 $results = $xslt->transformToXML($xml);
 //response
 print $results;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM