I would like to filter an xml feed so I only import data in the right language.
The structure is:
<profile_lang lang="en">
<description>English description</description>
</profile_lang>
<profile_lang lang="fr">
<description>French</description>
</profile_lang>
<profile_lang lang="nl">
<description>Dutch text</description>
</profile_lang>
etc.
What should I need to do import only the dutch (lang="nl") description?
xpath is a great way of targeting/filtering/searching specific elements in a XML structures
$xml = '<root><profile_lang lang="en"><description>English description</description></profile_lang><profile_lang lang="fr"><description>French</description></profile_lang><profile_lang lang="nl"><description>Dutch text</description></profile_lang></root>';
$simple = simplexml_load_string($xml);
$description = $simple->xpath('/root/profile_lang[@lang="nl"]/description');
echo $description[0]; // Dutch text
You can come up with your own path structures to suit your needs
/root/profile_lang[@lang="nl"]/description
//profile_lang[@lang="nl"]/description
//profile_lang[@lang="nl"]
//description[../@lang="nl"]
Try
$xml = <<<XML
<profile_lang lang="en">
<description>English description</description>
</profile_lang>
<profile_lang lang="fr">
<description>French</description>
</profile_lang>
<profile_lang lang="nl">
<description>Dutch text</description>
</profile_lang>
XML;
$xml = new SimpleXMLElement($xml);
$nodes = $xml->xpath('//*[@lang="en"]');
echo 'Found ', count($nodes), ' node(s) with lang "en".';