bv.ebizzinfotech
bv.ebizzinfotech

Reputation: 62

XPath: How to select specific attributes value of node?

Using XPath,How to select specific attributes value of node(where attribute = quantity)?

For Example:

$xml = new SimpleXMLElement('
  <option name="Size">
  <value optionID="147" quantity="1">6</value>
  <value optionID="111" quantity="1">7</value>
  <value optionID="145" quantity="0">8</value>
  <value optionID="112" quantity="0">9</value>
  <value optionID="146" quantity="0">10</value>
  <value optionID="416" quantity="0">11</value>
  </option>
');

Right Now I using the following code for getting value of size:

foreach ($product->xpath('//option[@name="Size"]) as $item)
{    
    foreach ($item->children() as $child)
    {
      echo $child ."\n";
    }
}

Now i want to get value of quantity with size. So plese help me how to make change in XPath Query for getting "quantity" value?

Upvotes: 1

Views: 2309

Answers (1)

paul trmbrth
paul trmbrth

Reputation: 20748

You can get "quantity" attributes of children "value" nodes using this XPath expression:

//option[@name="Size"]/value/@quantity

Upvotes: 1

Related Questions