使用DOMDocument在现有HTML表中创建新列

问题描述 投票:2回答:1

尝试使用DOMDocument和DOMXpath在现有表中创建新列

$doc = new DOMDocument();
$doc->loadHTMLFile("example.html");

以下是example.html中的表格结构:

<table id="transactions" class="table">
    <thead>
        <tr>
            <th class="image"></th>
            <th class="title"><span>Title</span></th>
        </tr>
    </thead>
    <tbody>
        <tr id="tbody-tr">
            <td class="image">
                <img src="http://www.example.com/image.jpg">
            </td>
            <td class="title">Title
            </td>
            <td class="date">12/16/2017
            </td>
        </tr>
        <tr id="tbody-tr">
            <td class="image">
                <img src="http://www.example.com/image.jpg">
            </td>
            <td class="title">Title
            </td>
            <td class="date">12/16/2017
            </td>
        </tr>
    </tbody>
</table>

在这种情况下,表的xpath查询将是

$table =  $xpath->query('//*[@id="transactions"]');

对于<tr id="tbody-tr">元素

$tr = $xpath->query('/*[@id="tbody-tr"]');

而对于<td>行元素 - / td [1] / td [2] / td [3]

$td = $xpath->query('//*[@id="tbody-tr"]/td[3]');

我正在尝试在<td class="example"></td><td class="title">之间为所有<td class="date">实例创建一个额外的列(<tr id="tbody-tr">)(大约有50个,但为了这个问题而缩小了)。

我是DOMDocument和HTML Parsing Manipulation的新手,我正在努力弄清楚如何做到这一点。

我假设我要么必须使用我的xpath->query循环它,就像

foreach ($ttrows as $row) {
    $td = $doc->createElement('td');
    $parent = $row->parentNode;
    $parent->appendChild($row);
    $td->setAttribute('class','example');
}

或使用DOMDocuments getElementById或类似的东西。

foreach ($table = $doc->getElementById('tbody-tr') as $table) {
    $td = $doc->createElement('td');
    $td->setAttribute('class', 'example');
    $td->appendChild($table);
    $table->insertBefore($td, $table);
}

正如我所说的,我是DOMDocument的新手,因此这两个例子几乎都没有......但我认为我正朝着正确的方向前进(我希望)。

我相信,这个表格结构相当合理,可以在DOMDocument中做这样的事情。有人可以对此有所了解,我的试验和错误让我得不到什么结果。

解:

foreach ($td as $row) {
    $td = $doc->createElement('td', 'text to be inserted');
    $td->setAttribute('class','example');
    $row->parentNode->insertBefore($td, $row);
}

请注意以上内容

$td = $xpath->query('//*[@id="tbody-tr"]/td[3]');

是第三个<td>date

php dom xpath domdocument domxpath
1个回答
1
投票

请记住,您不能/不应该在同一页面上有多个ID。

然后只需使用$tr->insertBefore($td, $tr->childNodes->item(3));

这意味着,当前节点在第3个td之前插入新的dom节点。

<?php
foreach ($doc->getElementsByTagName('tr') as $tr) {
    $td = $doc->createElement('td');
    $td->setAttribute('class', 'example');

    $tr->insertBefore($td, $tr->childNodes->item(3));
}

https://3v4l.org/TthE7

另外要考虑的是添加thead > th或它会打破表的外观。

foreach ($doc->getElementsByTagName('tr') as $tr) {

    // insert into thead > th
    if ($tr->childNodes->item(0)->nodeName == 'th') {
        $th = $doc->createElement('th');
        $th->setAttribute('class', 'example');
        $th->nodeValue = 'Example';
        $tr->insertBefore($th, $tr->childNodes->item(3));
    } 
    // insert into body > td
    else {
        $td = $doc->createElement('td');
        $td->setAttribute('class', 'example');

        $tr->insertBefore($td, $tr->childNodes->item(3));
    }
}

https://3v4l.org/1sj7s

© www.soinside.com 2019 - 2024. All rights reserved.