php - 导入样式表函数 PHP
问题描述
我正在尝试运行函数 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>
提前致谢,
解决方案
问题是第二行
$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" );
推荐阅读
- node.js - 在节点 pg client.query 中不返回
- php - 未找到 Html 聊天限制,也向下滚动,
- mysql - 在查询中使用碳时出错
- webpack - 网站的 Webpack 配置
- html - 更改弹出窗口的默认关闭按钮
- javascript - Express:如果 JSON 中不存在对象,如何返回 404?
- pipenv - 使用 pipenv 时如何通过结帐更新和切换分支?
- matplotlib - 使用自定义处理程序时忽略 matplotlib 图例参数
- oracle - 使用 XMLTABLE 的 LEFT OUTER JOIN 不起作用?
- powerbi - 为什么在power bi desktop中多次调用OData api