【问题标题】:PHP -> XPath with conditionsPHP -> 带条件的 XPath
【发布时间】:2013-11-27 12:00:43
【问题描述】:

我正在寻找一种在特定条件下执行 xpath 表达式的方法:

这是我的代码:

<table>
    <table>
       <thead>
          <tr>
             <td>
                <table>
                  <tr align="center" valign="top">
                    <td colspan="1" rowspan="1">
                      <img alt="WorkInstruction_Icon" src=".\I_WI_Text.png"/>
                    </td>
                    <td>
                       <p>
                          <span>GEHAEUSE re einsetzen</span>
                       </p>
                    </td>
                  </tr>
                  <tr>
                    <td colspan="1" rowspan="1" align="left">
                      <a href=".\SCR_27Aug2013_101146_002.jpg">
                        <img src=".\SCR_27Aug2013_101146_002.jpg" />
                      </a>
                    </td>
                  </tr>
                </table>
             </td>
          </tr> 
       </thead>
    </table>
    <!-- MORE TABLES MY XPath has to evaluate -->
</table

我希望我的 XPath-Query/Skript 执行以下操作:

检查这个表的第一个img-tag的alt-Attribute,如果它包含WorkInstruction,那么做一个Array-Entry:

Array(

[0] => WorkInstruction Gehaeuse re einsetzen, 

[1] => WorkInstruction SCR_27Aug2013_101146_002.jpg)

)

“Gehaeuse re einsetzen”和“SCR_27Aug2013”​​部分来自 span-Tag 和 a-Tag。

数组不一定是这样的。唯一重要的是我可以将“Gehaeuse re einsetzen”和其他条目识别为 WorkInstructions。

【问题讨论】:

    标签: php html xml xpath conditional-statements


    【解决方案1】:
    $dom = new DOMDocument();
    $dom->loadHtml($html);
    $xpath = new DOMXpath($dom);
    
    $result = array();
    $nodes = $xpath->evaluate('//table[tr[1]/td[1]/img[1][contains(@alt, "WorkInstruction")]]');
    foreach ($nodes as $tableNode) {
      $result[] = array(
        $xpath->evaluate('string(tr[1]/td[2])', $tableNode),
        $xpath->evaluate('string(tr[2]/td[1]/a/@href)', $tableNode)
      );
    }
    
    var_dump($result);
    

    输出:

    array(1) {
      [0]=>
      array(2) {
        [0]=>
        string(21) "GEHAEUSE re einsetzen"
        [1]=>
        string(30) ".\SCR_27Aug2013_101146_002.jpg"
      }
    }
    

    【讨论】:

    • 谢谢,ThomasWeinert,为您提供帮助!我用另一种方式解决了我的问题,但你的代码给了我必要的提示!它是关于可以在 XPath 中使用的 contextNodes。
    【解决方案2】:

    我自己找到了解决问题的方法,但 ThomasWeinert 提供的代码给了我提示。这是关于您可以在 XPath 中使用的 contextNodes:

    function loadAndParse($link){
        //Parser die 3. Baut auf die Icons auf!
    
        //xpath-Dokument erstellen
        $doc = new DOMDocument();
        $doc -> loadHTMLFile($link);
        $xpath = new DOMXpath($doc);
    
        $stepInfo = array();
    
        $first = $xpath->query('/html/body/table[last()]');
        $second = $xpath->query('thead/tr/td/table', $first->item(0));
    
        foreach ($second as $sectab) {
            $secdata = $xpath->query('tr/td/img/@alt', $sectab);
    
            if(preg_match("/WorkInstruction/", $secdata->item(0)->nodeValue)){
                $nodeList = $xpath->query('tr/*[text()] | tr/td//a/@href', $sectab);
                $length = $nodeList->length;
                for($j=0; $j<$length; $j++){
                    $workIns[] = trim($nodeList->item($j)->nodeValue);
                    $stepInfo["workIns"] = $workIns;
                }
            }
            if(preg_match("/DataCollect/", $secdata->item(0)->nodeValue)){
                $nodeList = $xpath->query('tr/*[text()]', $sectab);
                $length = $nodeList->length;
                for($j=0; $j<$length; $j++){
                    $dataCol[] = trim($nodeList->item($j)->nodeValue);
                    $stepInfo["dataCol"] = $dataCol;
                }
            }
            if(preg_match("/SIGN-OFF/", $secdata->item(0)->nodeValue)){
                $nodeList = $xpath->query('tr/*[text()]', $sectab);
                $length = $nodeList->length;
                for($j=0; $j<$length; $j++){
                    $signOff[] = trim($nodeList->item($j)->nodeValue);
                    $stepInfo["signOff"] = $signOff;
                }
            }
            if(preg_match("/ALERT/", $secdata->item(0)->nodeValue)){
                $nodeList = $xpath->query('tr/*[text()]', $sectab);
                $length = $nodeList->length;
                for($j=0; $j<$length; $j++){
                    $warning[] = trim($nodeList->item($j)->nodeValue);
                    $stepInfo["warning"] = $warning;
                }
            }
        }
        return $stepInfo;
    }
    

    现在您可以将获得的数据保存在自己的数组中,放入任何类型的多维数组中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 2018-01-05
      • 2019-11-21
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多