php - Removing parent nodes based on child in XML -
this question has answer here:
- php simplexml - remove xpath node 4 answers
i have xml looks following:
$data = '<search-params> <date>2013-05-20</date> </search-params> <results> <date>2013-05-21 14:31:00</date> <prices> <for> <players>1</players> </for> <amount>20</amount> </prices> <prices> <for> <players>2</players> </for> <amount>35</amount> </prices> <prices> <for> <players>3</players> </for> <amount>49.5</amount> </prices> <prices> <for> <players>4</players> </for> <amount>60</amount> </prices> </results> <results> <date>2013-05-21t14:40:00</date> <prices> <for> <players>1</players> </for> <amount>20</amount> </prices> </results>';
now, want strip out of based on rule.
so, if have <players>
child node of 4, should return <results>
nodes have value.
is possible in in php?
i can load using simplexml_load_string($data)
. problem how find information , remove nodes.
thanks
nobody answered, will. please, read comments.
// first, xml must wraped root tag. $data = '<root> <search-params> <date>2013-05-20</date> </search-params> <results> <date>2013-05-21 14:31:00</date> <prices> <for> <players>1</players> </for> <amount>20</amount> </prices> <prices> <for> <players>2</players> </for> <amount>35</amount> </prices> <prices> <for> <players>3</players> </for> <amount>49.5</amount> </prices> <prices> <for> <players>4</players> </for> <amount>60</amount> </prices> </results> <results> <date>2013-05-21t14:40:00</date> <prices> <for> <players>1</players> </for> <amount>20</amount> </prices> </results> </root>'; // instancing simplexmlelement within xml(obviously) $xml = new simplexmlelement($data); // xpath $xpath = $xml->xpath("results[prices/for[contains(players,'4')]]"); /** * explained xpath: * <results> * <prices>/<for> * contains <players> content equal 4 */ foreach($xpath $node){ // fun echoes <results> node echo $node->asxml(); }
Comments
Post a Comment