1

I select <a id="categoryBrandIcon"> with:

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

How do I modify the code to select <a id="categoryBrandIcon"> and all <input type="hidden"> tags?

Selecting inputs if input[@type='hidden'] but I don't know how to chain those two.

With CSS selectors I would do this a#categoryBrandIcon, input[type="hidden"]

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
Szymon Toda
  • 4,454
  • 11
  • 43
  • 62
  • possible duplicate of [Two conditions using OR in XPATH](http://stackoverflow.com/questions/12562597/two-conditions-using-or-in-xpath) – i alarmed alien Oct 19 '14 at 15:11

2 Answers2

2

Yes you could combine them in a single xpath query thru pipe. Example:

$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
}
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • How you would remove them withiout loop usage? – Szymon Toda Oct 19 '14 at 15:14
  • @Ultra could you kindly rephrase it? `remove them without loop usage`? how are you going to scan thru those found elements? – Kevin Oct 19 '14 at 15:18
  • To access each node (and delete it for example) I need foreach loop - is there a way to access them as a group and delete them? – Szymon Toda Oct 19 '14 at 15:19
  • @Ultra like `$e->parentNode->removeChild($e)` ? i don't think there is such function that does mass delete nodes – Kevin Oct 19 '14 at 15:21
1

You can combine two xpath queries with a vertical bar, so just chain your two queries together:

//a[@id='categoryBrandIcon'] | //input[@type='hidden']
i alarmed alien
  • 9,412
  • 3
  • 27
  • 40