php - DOMXPath 查询以获取表列中复选框的名称

标签 php domxpath

我有以下html表格结构

<table id="gvPOItems">
        <tbody>
        <tr>
            <td class="grdItemClass">
              <span class="CSSchkupdatethis">
                <input id="gvPOItems_cbxForShipping_0" type="checkbox" name="gvPOItems$ctl02$cbxForShipping">
               </span>
             </td>
             <td class="grdItemClass">
                <span id="gvPOItems_lblProductID_0">685029</span>
             </td>
             <td>
               <img src="" alt="Product Photo">
             </td>
             <td class="grdItemClass">SWA400000009-W</td>
             <td class="grdItemClass">
               <span id="gvPOItems_lblPOItemID_0">
                 1162934PLN44532493
                </span>
             </td>
             <td class="grdItemClass">
                 Small Wonder Training Set White - 300 ml
             </td>
             <td class="grdItemClass">
               0 Months+, BPA Free, This training set is simple, easy-to-use and can be used for both girls and boys
             </td>
             <td class="grdItemClass">110.00</td>
             <td class="grdItemClass">13.20</td>
             <td class="grdItemClass">0.00000000000</td>
             <td class="grdItemClass">96.80</td>
             <td class="grdItemClass">
               <span id="gvPOItems_lblQuantity_0">1</span>
             </td>
             <td class="grdItemClass">96.80</td>
             <td class="grdItemClass">&nbsp;</td>
             <td class="grdItemClass">&nbsp;</td>
             <td>
               <select name="gvPOItems$ctl02$ddlStatus" id="gvPOItems_ddlStatus_0">
                  <option value="0">Select</option>
                  <option value="Accepted">Accepted</option>
                  <option value="OutOfStock">OutOfStock</option>
                </select>
                <span id="gvPOItems_RequiredFieldValidator1_0" style="display:none;">
                *
                </span>
              </td>
              <td class="grdItemClass">48</td>
        </tr>
    </tbody>

我正在尝试使用 xpath 查询获取 ex: gvPOItems$ctl02$cbxForShipping 列中复选框的名称

我使用的代码是

<? php 
  function xpath($content) {
    $dom = new DOMDocument();
    $dom->strictErrorChecking = false;
    $dom->loadHTML($content);
    return new DOMXPath($dom);
  }

  $countNode = $xpath->query('//table[@id="gvPOItems"]//tr');
  for($i=0;$i<=$countNode->length;$i++){
      $q= '//table[@id="gvPOItems"]/tr['.$i.']//td';
      $node = $xpath->query($q);
      var_dump($node->item(0));
  }

但是,我得到的输出是

object(DOMNodeList)#169 (1) {
  ["length"]=> int(17)
}

获取列中所有复选框名称的正确查询应该是什么?

最佳答案

我已经更改了查询。您可以直接选择输入。

$xml = file_get_contents('test.xml');

function xpath($content)
{
    $dom = new DOMDocument();
    $dom->strictErrorChecking = FALSE;
    $dom->loadHTML($content);
    return new DOMXPath($dom);
}

$xpath = xpath($xml);

$countNode = $xpath->query('//table[@id="gvPOItems"]//tr');
var_dump($countNode);

$q = '//input[@type="checkbox"]'; // whole document
$q = '//table[@id="gvPOItems"]//input[@type="checkbox"]'; // only in the table
$nodeList = $xpath->query($q);
// var_dump($node);// ->item(0));
// var_dump($node->item(0));

foreach($nodeList as $node)
{
    echo "path=" . $node->getNodePath();
    echo "\n";
    echo "name=" . $node->getAttribute('name');
}

关于php - DOMXPath 查询以获取表列中复选框的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37405189/

相关文章:

php - 从外部页面获取div,然后从中删除另一个div

php - SlimPHP v3 如何在 View 中显示 flash 消息

PHP xpath 包含类和不包含类

php - ViewerJS 服务器拒绝连接

php - Fancybox - 重新加载页面或加载新页面

php - 使用 PHP 从 DOM 节点而不是其子节点获取内容

PHP DOMXPath 正在剥离匹配文本中的我的标签

php - 动态 HTML 的 DOMXPath 查询

php - Azure 表存储 Rest API

javascript - PHP:如何在 php 中使用 GET 将值存储到数组