1

我已经尝试了很多东西,但我无法在屏幕上加载任何东西。PHP 在屏幕上加载 XML 没有问题,但是当我在 PHP 中添加 XSL 代码时,屏幕是空白的,没有错误或什么都没有。谁能明白为什么?

谢谢

PHP 文件:

<?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 文件:

<?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 文件:

<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>
4

1 回答 1

1

这很简单,你只需要这个:

 $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;
于 2013-04-30T05:58:09.757 回答