我正在尝试以 XML 格式获取具有粘性类的表的内容。
我的 PHP 代码是:
<?php
// Load the XML source
$xml = new DOMDocument;
$out = $xml->load("collection.html");
$xsl = new DOMDocument;
$xsl->load('collection.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
$xml = $proc->transformToXML($xml);
$xml = simplexml_load_string($xml);
print_r($xml);
?>
而 collection.html HTML 是:
<table>
<thead>
<tr>
<th>A</th>
</tr>
<tbody>
<tr>
<td>B</td>
</tr>
</tbody>
</thead>
</table>
<table class="sticky-enabled">
<thead><tr><th>Date</th><th>Time</th><th>Location</th><th>Tracking Event</th> </tr></thead>
<tbody>
<tr class="odd"><td>16-04-2013</td><td>19:20</td><td>International Hub</td><td>Forwarded for export</td> </tr>
<tr class="even"><td>16-04-2013</td><td>18:53</td><td>International Hub</td><td>Received and processed</td> </tr>
<tr class="odd"><td>15-04-2013</td><td>17:28</td><td>Manchester Piccadilly Depot</td><td>Collected from customer</td> </tr>
<tr class="even"><td>15-04-2013</td><td>00:00</td><td>WDM Online</td><td></td> </tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>A</th>
</tr>
<tbody>
<tr>
<td>B</td>
</tr>
</tbody>
</thead>
</table>
最后 collection.xsl 是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<output>
<xsl:for-each select="table[@class='sticky-enabled']/tbody/tr">
<tracking>
<date><xsl:value-of select="td[1]" /></date>
<time><xsl:value-of select="td[2]" /></time>
<event><xsl:value-of select="td[3]" /></event>
<extra><xsl:value-of select="td[4]" /></extra>
</tracking>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
如果我运行它,则 $xml 为空。如果我编辑 collection.html 并删除第一个和最后一个表(即只留下我试图访问的那个),那么它就可以工作。因此,我怀疑问题出在:
<xsl:for-each select="table[@class='sticky-enabled']/tbody/tr">