【问题标题】:importStyleSheet function PHP [closed]importStyleSheet 函数 PHP [关闭]
【发布时间】:2020-09-08 04:38:56
【问题描述】:

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

  • 警告:XSLTProcessor::importStylesheet() 期望参数 1 是对象,给定的布尔值 我的文件的路径是正确的。

这是我的代码:

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

提前致谢,

【问题讨论】:

  • 顺便说一句,给DOMDocument 构造函数提供参数是没有用的,因为它们总是被DOMDocument::load 方法覆盖。 $xsl = new DOMDocument; 就够了。
  • 不确定您是否可以使用位于服务器上的 php 文件中的 DOMDocument::load 访问此绝对 URL。请改用相对 URL。

标签: php xml xslt xls


【解决方案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" );

【讨论】:

  • 谢谢我没有看到这个小错误!!
猜你喜欢
  • 2021-01-07
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多