xml - XPath query with PHP -
here's xml code i'm working with:
<inventory> <drink> <lemonade supplier="mother" id="1"> <price>$2.50</price> <amount>20</amount> </lemonade> <lemonade supplier="mike" id="4"> <price>$3.00</price> <amount>20</amount> </lemonade> <pop supplier="store" id="2"> <price>$1.50</price> <amount>10</amount> </pop> </drink> </inventory>
then wrote simple code practice working xpath:
<?php $xmldoc = new domdocument(); $xmldoc->load('sample.xml'); $xpathvar = new domxpath($xmldoc); $queryresult = $xpathvar->query('//lemonade/price'); foreach($queryresult $result) { echo $result->textcontent; } ?>
that code working well, outputting lemonade price values expected. when change query string select elements attribute set value,
//lemonade[supplier="mother"]/price
or
//lemonade[id="1"]/price
it won't work, no output @ all. doing wrong?
try this:
//lemonade[@id="1"]/price
or
//lemonade[@supplier="mother"]/price
without "@" looks child elements name instead of attributes.
Comments
Post a Comment