XPath query with PHP XPath query with PHP php php

XPath query with PHP


Try this:

//lemonade[@id="1"]/price

or

//lemonade[@supplier="mother"]/price

Without the "@" it looks for child elements with that name instead of attributes.


This is only tangentially related, but when you use XPath on a document for which you know the structure, don't use "//some-element-name". It's very nice for a quick example, but when you hit a huge xml file with that query, particularly if it is followed by something complex, you will quickly run into performance issues.

inventory/drink/lemonade[@supplier="mother"]/price


you have to use the @ sign to indicate attribute within the predicate like so: //lemonade[@supplier="mother"]/price, that's all.