【发布时间】: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。