-1

I have an XML which looks like the 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, what I want to do is to strip out some of that based on a rule.

So, If I have <players> child node of 4, it should only return the <results> nodes that have that value.

Is this possible in in PHP?

I can load that using simplexml_load_string($data). The problem is how to find the information and remove the nodes.

Thanks

user1970557
  • 515
  • 1
  • 9
  • 23
  • 1
    **[XPath](http://php.net/manual/pt_BR/simplexmlelement.xpath.php)** – Wesley Schleumer de Góes May 21 '13 at 13:58
  • Yes, it is possible in PHP with XPath. And yes, simplexml supports xpath 1.0. To remove a node with simplexml, please see: [PHP SimpleXML - Remove xpath node](http://stackoverflow.com/q/2442314/367456) and [Remove a child with a specific attribute, in SimpleXML for PHP](http://stackoverflow.com/q/262351/367456). -1 from my end because this question does not show any research effort; it is unclear or not useful. – hakre May 21 '13 at 15:40

1 Answers1

1

Nobody answered, so i will. Please, read the comments.

// First, your XML must be wraped with some 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 the SimpleXMLElement within the XML(obviously)
$xml = new SimpleXMLElement($data);

// XPath
$xpath = $xml->xpath("results[prices/for[contains(players,'4')]]"); 
/**
 * eXplained XPath:
 *      <results> that 
 *          <prices>/<for>
 *              contains <players> that content is equal 4
 */

foreach($xpath as $node){
    // just for fun echoes the <results> node
    echo $node->asXml();
}