views:

181

answers:

1

I'm having a hard time with XPath here.. Given the following XPath queries:

$xpath->query('//input[@name="' . $field . '"]');
$xpath->query('//select[@name="' . $field . '"]');

Is is possible to combine them into one single query? I want to get the value of the field, however I don't know if the field with be a input, select, textarea...

The way I'm doing it now is like this:

$input = $xpath->query('//input[@name="' . $field . '"]');

if (empty($input) === true)
{
    $select = $xpath->query('//select[@name="' . $field . '"]');

    if (empty($select) === true)
    {
     // ...
    }
}

However it seems to cumbersome, I'm sure there must be a way to merge all the queries into one.

+3  A: 

Use the '|' to join the queries.

$v = '[@name="' . $field . '"]';
$input = $xpath->query('//input' . $v. ' | //select' . $v);

if (empty($input) === true)
{
     // ...    
}

EDIT: Thought I would add this in for more reference. http://www.w3schools.com/XPath/xpath_operators.asp

ChaosPandion
Thanks! That got the job done, also can you tell me if there is a similar function to getAttribute() but instead of the attribute it returns the tag name? I just realized that I need it to access select options and textarea contents since they have no value attribute.
Alix Axel