我尝试了很多东西,但是我无法在屏幕上加载任何东西。 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>
答案 0 :(得分: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;