Question: Is there a way in xpath to select an element and than select children of the element?

i.e. given a parent result, how can I find a child that is relative to `this` paraent

suppose the following code:

```html
<div class="product-general" prod-id="4407">
  <img src="..."/>
  <div class="prod-name">Black Dog</div>
</div>
```

in `jQuery` one can do:

```js
parent = $('.product-general')
id = parent.attr('prod-id')
name = $('.prod-name', parent).text()
```

I have the following code (php):

```
    $results = $xpath->query("//*[@class='product-general']");
    for ($i = 0; $i < $results->length; $i++) {
        $parent = $results->item($i)->nodeValue;
        // todo: get prod-id and prod-name
    }

```

Is it possible to get the prod-id and prod-name that are relative to the parent?

NOTE: 
I know I can do it as follow:

```
    $results = $xpath->query("//*[@class='product-general']/@data-pid");
    for ($i = 0; $i < $results->length; $i++) {
        $pid = $results->item($i)->nodeValue;
        $results2 = $xpath->query("//*[@class='product-general' and @data-pid='".$pid."']//*[contains(@class,'prod-name')]");//
        $name= $results2->item(0)->nodeValue;
        $this->products[] = ['pid' => $pid, 'name' => $name];
    }

```

But I want to know if there is something more elegant, like the JQ example

Also, this solution is painfully slow for a document with 1000 products

10x

Answer: 
The second param to the `query` is a `$contextnode` that way you can use **relative XPath** like this:

```
foreach ($articles as $article) {
    $href = $xpath->query('./a/@href', $article)->item(0)->nodeValue;
    # $href = $xpath->evaluate('string(./a/@href)', $article); # using evaluate
}

```
I came up with this:

```
$results = $xpath->query("//*[@id='searchPage']//article");
for ($i = 0; $i < $results->length; $i++) {
    $counter = $i+1;
    $href = $xpath->query("//*[@id='searchPage']//article[".$counter."]/a/@href")->item(0)->nodeValue;
}

```

This code will loop on all `article` in `searchPage` and extract their `<a href="...">`

The key idea is that by adding the `[".$counter."]` to the query u can use the current loop index as a context for searching sub-elements

NOTE that `$counter = $i+1;`