首页 > 解决方案 > 导入样式表函数 PHP

问题描述

我正在尝试运行函数 importStyleSheet 但运行函数时出现警告:

这是我的代码:

<?php
$xsl = new DOMDocument('1.0','UTF-8');
$xsl = $xsl->load("E:/wamp64/www/structuration/test.xsl");

$xslt = new xsltProcessor();
$xslt->importStyleSheet($xsl);
$xmldoc = new DOMDocument('1.0','UTF-8');
$xmldoc->load(file_get_contents("E:/wamp64/www/structuration/test.xml"));
print $xslt->transformToXML($xmldoc);
?>

这是我的 xsl 文件:

<?xml version="1.0" encoding="UTF-8"?>
<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>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>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

我的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</catalog>

提前致谢,

标签: phpxmlxsltxls

解决方案


问题是第二行

$xsl = $xsl->load("E:/wamp64/www/structuration/test.xsl");

将其替换为

$xsl->load("E:/wamp64/www/structuration/test.xsl");

load方法返回 a boolean,因此您此时已更改$xsl为布尔结果而不是对DOMDocument

你可能还想改变

$xmldoc->load(file_get_contents("E:/wamp64/www/structuration/test.xml"));

$xmldoc->load( "E:/wamp64/www/structuration/test.xml" );

推荐阅读