XSLT 处理器 transformToXML 无样式信息
XSLT Processor transformToXML no style information
我正在尝试使用 PHP(版本 5.5.19)中的 XstlProcessor Class 来执行 xls 转换。如果我执行脚本并打印结果,则只打印没有转换的旧 xml 文件,它显示 "This XML file does not appear to have any style information associated with it.".
应该执行转换的脚本部分:
$xml = new DomDocument;
$xml->load("tmp.xml");
$xsl = new DomDocument;
$xsl->load("bookings.xsl");
$proc = new XsltProcessor;
$proc->importStyleSheet($xsl);
$html = $proc->transformToXML($xml);
if(!$html) die('XLST processing error\n');
echo $html;
XSL 文件
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Booking Overview</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Date</th>
<th>Name</th>
<th>Surname</th>
<th>Street</th>
<th>City</th>
<th>Country</th>
<th>Brand</th>
<th>Model</th>
</tr>
<xsl:for-each select="bookings/booking">
<tr>
<td><xsl:value-of select="@bdate" /></td>
<td><xsl:value-of select="customer/name"/></td>
<td><xsl:value-of select="customer/surname"/></td>
<td><xsl:value-of select="address/street"/></td>
<td><xsl:value-of select="address/city"/></td>
<td><xsl:value-of select="address/country"/></td>
<td><xsl:value-of select="vehicle/brand"/></td>
<td><xsl:value-of select="vehicle/model"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- all bookings-->
<bookings>
<!--booking-->
<booking bdate ="2015-01-14">
<customer>
<name>Josef</name>
<surname>Mongo</surname>
</customer>
<address>
<street>Alberstrasse</street>
<city>Graz</city>
<country>Austria</country>
</address>
<vehicle>
<brand>Audi</brand>
<model>R8</model>
</vehicle>
</booking>
<!--booking-->
<booking bdate ="2014-07-23">
<customer>
<name>Hannelore</name>
<surname>Metutschnik</surname>
</customer>
<address>
<street>Moserhofgasse</street>
<city>Graz</city>
<country>Austria</country>
</address>
<vehicle>
<brand>Fiat</brand>
<model>Punto</model>
</vehicle>
</booking>
<!--booking-->
<booking bdate ="2014-11-20">
<customer>
<name>Josef</name>
<surname>Mongo</surname>
</customer>
<address>
<street>Alberstrasse</street>
<city>Graz</city>
<country>Austria</country>
</address>
<vehicle>
<brand>BMW</brand>
<model>M6</model>
</vehicle>
</booking>
<!--booking-->
<booking bdate ="2014-11-23">
<customer>
<name>Onder</name>
<surname>Graf</surname>
</customer>
<address>
<street>Mariahilferstrasse</street>
<city>Wien</city>
<country>Austria</country>
</address>
<vehicle>
<brand>BMW</brand>
<model>M6</model>
</vehicle>
</booking>
<!--booking-->
<booking bdate ="2014-11-23">
<customer>
<name>Onder</name>
<surname>Graf</surname>
</customer>
<address>
<street>Mariahilferstrasse</street>
<city>Wien</city>
<country>Austria</country>
</address>
<vehicle>
<brand>BMW</brand>
<model>M6</model>
</vehicle>
</booking>
</bookings>
您的输入 XML 格式正确且 XSLT 代码正确。我无法在 PHP 5.4.30.
下重现您的问题
我怀疑您将结果保存在扩展名为 *.xml
而不是 *.html
的文件中。如果用浏览器打开,它将报告没有样式表与此 XML 文件相关联。所以,解决方案是:更改文件扩展名。
我正在尝试使用 PHP(版本 5.5.19)中的 XstlProcessor Class 来执行 xls 转换。如果我执行脚本并打印结果,则只打印没有转换的旧 xml 文件,它显示 "This XML file does not appear to have any style information associated with it.".
应该执行转换的脚本部分:
$xml = new DomDocument;
$xml->load("tmp.xml");
$xsl = new DomDocument;
$xsl->load("bookings.xsl");
$proc = new XsltProcessor;
$proc->importStyleSheet($xsl);
$html = $proc->transformToXML($xml);
if(!$html) die('XLST processing error\n');
echo $html;
XSL 文件
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Booking Overview</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Date</th>
<th>Name</th>
<th>Surname</th>
<th>Street</th>
<th>City</th>
<th>Country</th>
<th>Brand</th>
<th>Model</th>
</tr>
<xsl:for-each select="bookings/booking">
<tr>
<td><xsl:value-of select="@bdate" /></td>
<td><xsl:value-of select="customer/name"/></td>
<td><xsl:value-of select="customer/surname"/></td>
<td><xsl:value-of select="address/street"/></td>
<td><xsl:value-of select="address/city"/></td>
<td><xsl:value-of select="address/country"/></td>
<td><xsl:value-of select="vehicle/brand"/></td>
<td><xsl:value-of select="vehicle/model"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- all bookings-->
<bookings>
<!--booking-->
<booking bdate ="2015-01-14">
<customer>
<name>Josef</name>
<surname>Mongo</surname>
</customer>
<address>
<street>Alberstrasse</street>
<city>Graz</city>
<country>Austria</country>
</address>
<vehicle>
<brand>Audi</brand>
<model>R8</model>
</vehicle>
</booking>
<!--booking-->
<booking bdate ="2014-07-23">
<customer>
<name>Hannelore</name>
<surname>Metutschnik</surname>
</customer>
<address>
<street>Moserhofgasse</street>
<city>Graz</city>
<country>Austria</country>
</address>
<vehicle>
<brand>Fiat</brand>
<model>Punto</model>
</vehicle>
</booking>
<!--booking-->
<booking bdate ="2014-11-20">
<customer>
<name>Josef</name>
<surname>Mongo</surname>
</customer>
<address>
<street>Alberstrasse</street>
<city>Graz</city>
<country>Austria</country>
</address>
<vehicle>
<brand>BMW</brand>
<model>M6</model>
</vehicle>
</booking>
<!--booking-->
<booking bdate ="2014-11-23">
<customer>
<name>Onder</name>
<surname>Graf</surname>
</customer>
<address>
<street>Mariahilferstrasse</street>
<city>Wien</city>
<country>Austria</country>
</address>
<vehicle>
<brand>BMW</brand>
<model>M6</model>
</vehicle>
</booking>
<!--booking-->
<booking bdate ="2014-11-23">
<customer>
<name>Onder</name>
<surname>Graf</surname>
</customer>
<address>
<street>Mariahilferstrasse</street>
<city>Wien</city>
<country>Austria</country>
</address>
<vehicle>
<brand>BMW</brand>
<model>M6</model>
</vehicle>
</booking>
</bookings>
您的输入 XML 格式正确且 XSLT 代码正确。我无法在 PHP 5.4.30.
下重现您的问题我怀疑您将结果保存在扩展名为 *.xml
而不是 *.html
的文件中。如果用浏览器打开,它将报告没有样式表与此 XML 文件相关联。所以,解决方案是:更改文件扩展名。