Skip to content

Instantly share code, notes, and snippets.

Created April 3, 2015 17:27
Show Gist options
  • Save anonymous/e81c73fa79f66ef935f1 to your computer and use it in GitHub Desktop.
Save anonymous/e81c73fa79f66ef935f1 to your computer and use it in GitHub Desktop.
SimpleXML Xpath Foreach Problem
<?php
/*
expected output:
Weekend
Something
actual output:
Weekend
Something
Weekend
Something
*/
$xml_string = "<?xml version='1.0' encoding='UTF-8'?>
<root>
<note>
<to>Tove</to>
<from>Jani</from>
<content>
<summary>Weekend</summary>
<body>Don't forget me this weekend!</body>
</content>
</note>
<note>
<to>Bob</to>
<from>Alice</from>
<content>
<summary>Something</summary>
<body>Something something.</body>
</content>
</note>
</root>";
$xml = simplexml_load_string($xml_string);
$notes = $xml->xpath("//note");
echo "\n";
foreach ($notes as $note) {
$summaries = $note->xPath("//content/summary");
foreach ($summaries as $summary) {
echo "$summary \n";
}
}
echo "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment