I'm having trouble accessing a value from an XML file. Especially I can't extract the FrenchText. All I always get is the value from the GermanText. Does anyone have an idea, how to get to the value of the node containing the french string directly.
Consulting the web, I found this, but it only results in an error
echo $STRUCTURE_ITEM->NAME[@lang="fr"]
Any help would be greatly appreciated.
Here's my code
<?php
$myXMLData = '<?xml version="1.0" encoding="utf-8"?>
<CATALOG>
<CLASSIFICATION>
<STRUCTURE_ITEM>
<KEY>3605</KEY>
<NAME lang="de">GermanText1</NAME>
<NAME lang="fr">FrenchText1</NAME>
<PARENT_ID>worlds</PARENT_ID>
<SORT>0</SORT>
</STRUCTURE_ITEM>
<STRUCTURE_ITEM>
<KEY>3606</KEY>
<NAME lang="de">GermanText2</NAME>
<NAME lang="fr">FrenchText2</NAME>
<PARENT_ID>3605</PARENT_ID>
<SORT>0</SORT>
</STRUCTURE_ITEM>
</CLASSIFICATION>
</CATALOG>';
$xml=simplexml_load_string($myXMLData);
foreach($xml->CLASSIFICATION->children() as $STRUCTURE_ITEM) {
echo $STRUCTURE_ITEM->KEY . ", ";
echo $STRUCTURE_ITEM->NAME . ", ";
echo $STRUCTURE_ITEM->NAME . ", "; // <---- The problem lies here
echo $STRUCTURE_ITEM->PARENT_ID . "<br>";
} ;
?>
EDIT
Thanks for the valuable input. I've tried
$category_name_fr = $xml->xpath('//STRUCTURE_ITEM/NAME[@lang="fr"]');
Now I get the french values, but I get back an array containing all available french text. (FrenchText1, FrenchText2). But I just want the value of the current node.