Or Weinberger
Or Weinberger

Reputation: 7472

Extracting a value using xpath

how can I get the value of this input using xpath?

<input type='text' name='name' value='john' /> 

Thanks

Upvotes: 0

Views: 205

Answers (3)

brian_d
brian_d

Reputation: 11360

With SimpleXML,

$input_element = $your_xml_doc->xpath("//input[@name='name']");
$value = (String) $input_element[0]->attributes()->value;

Upvotes: 1

mqchen
mqchen

Reputation: 4193

You need to use the attribute selector, eg: //input/@value

There's more about selecting nodes here: w3schools.com and a nice tool for testing your XPath here.

Upvotes: 2

tsadiq
tsadiq

Reputation: 402

i think

//input/@value 

would be enough if you had only this piece of code... but maybe you have more html around and you need to target :

//input[@name='name']/@value

Upvotes: 2

Related Questions