importStyleSheet函数 PHP [关闭]

问题描述 投票:0回答:1

我试图运行函数importStyleSheet,但当我运行我的函数时,我有一个警告。

  • 警告:XSLTProcessor::importStylesheet()期望参数1是object,boolean,但当我运行函数时,我得到一个警告:警告。XSLTProcessor::importStylesheet() expects parameter 1 to be object, boolean given我的文件路径是正确的。

这是我的代码。

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

先谢谢你。

php xml xslt xls
1个回答
1
投票

问题出在第二行

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

改为

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

load 方法返回一个 boolean 所以你已经,在这一点上,改变 $xsl 的结果中,而不是引用到 DOMDocument

你可能还想改变

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

$xmldoc->load( "E:/wamp64/www/structuration/test.xml" );
© www.soinside.com 2019 - 2024. All rights reserved.