2014-10-19 69 views
1

兩個不同的元素我選擇<a id="categoryBrandIcon">有:如何選擇使用XPath查詢

$item = $xpath->query("//a[@id='categoryBrandIcon']")->item(0); 

如何修改代碼以選擇<a id="categoryBrandIcon">和所有<input type="hidden">標籤?

如果input[@type='hidden']選擇輸入,但我不知道如何鏈接這兩個。

使用CSS選擇,我會做這個a#categoryBrandIcon, input[type="hidden"]

+0

可能重複(http://stackoverflow.com/questions/12562597/two-conditions-using-or-in-xpath) – 2014-10-19 15:11:41

回答

2

是的,你可以在一個單一的將它們結合起來xpath查詢通過管道。例如:

$sample_markup = ' 
<div class="container"> 
    <a id="categoryBrandIcon" href="#">Test</a> 
    <input type="hidden" /> 
    <input type="text" /> 
    <h1>Test</h1> 
</div> 
'; 

$dom = new DOMDocument(); 
$dom->loadHTML($sample_markup); 
$xpath = new DOMXPath($dom); 

$elements = $xpath->query("//a[@id='categoryBrandIcon'] | //input[@type='hidden']"); 
foreach($elements as $e) { 
    // loop thru found elements 
} 
的[使用或XPATH兩個條件]
+0

你會如何刪除它們withiout循環使用? – 2014-10-19 15:14:52

+0

@Ultra你能不能改說一下? '刪除他們沒有循環使用'?你打算如何掃描那些找到的元素? – Ghost 2014-10-19 15:18:16

+0

要訪問每個節點(並刪除它)我需要foreach循環 - 有沒有辦法將它們作爲一個組訪問並刪除它們? – 2014-10-19 15:19:41

0

您可以將兩個XPath查詢用豎線,所以才鏈中的兩個查詢一起:

//a[@id='categoryBrandIcon'] | //input[@type='hidden']