1

我正在尝试以 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">
4

1 回答 1

0

您的“XML”格式不正确。因此,不能使用 XSLT 对其进行解析和转换。XML 文档必须有一个文档元素。您有三个<table>兄弟元素。删除其他表会生成一个可以转换的格式良好的 XML 文件。

尝试使用 XML 元素包装表格。

例如:

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

然后调整样式表以适应结构的变化,匹配文档元素而不是根节点:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
        <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>
于 2013-04-17T00:20:57.040 回答