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

时间:2016-05-24 05:38:11

标签: 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)
}

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

1 个答案:

答案 0 :(得分:1)

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

$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');
}