-2

I have problem with getting specific name from xml file using xpath. Here is xml code:

<Zones>
 <Zone ID="AFI" Quantity="67" Length="140" Open="0">
   <Name LngID="IT">Name 1</Name>
   <Name LngID="EN">Name 2</Name>
   <Name LngID="DE">Name 3</Name>
 </Zone>
 <Zone ID="SLE" Quantity="1" Length="2" Open="0">
   <Name LngID="IT">Name 4</Name>
   <Name LngID="EN">Name 5</Name>
   <Name LngID="DE">Name 6</Name>
 </Zone>
</Zones>

And here is my php code:

foreach($area->Zones->Zone as $zone) {

  $ZONE_ID = $zone['ID'];
  $zone_lang = $zone->xpath("/Name[@LngID='EN']");
  $ZONE_NAME = var_dum($zone_lang);

  echo $ZONE_ID;
  echo " - ";
  echo $ZONE_NAME;

}

I need only EN name of Name item (Name LngID="EN" -> Name 2 and Name 5), but that code didn't work (I get Array value).

Any suggestion? Thank's in advice!

Adrian
  • 992
  • 2
  • 16
  • 46
  • `var_dum($zone_lang);` & check what you getting there cause xpath return an array. so you need `$zone_lang[0]`. – Rikesh May 17 '13 at 11:31
  • Yes, `xpath` returns an array (even if there's only one element). `xpath(...)[0]` refers to the first matched element. – Passerby May 17 '13 at 11:31
  • Edit your post again with your tried code. – Rikesh May 17 '13 at 11:42
  • 1
    http://php.net/simplexmlelement.xpath - it returns an array, this is well document: http://php.net/arrays - Your problem is exactly like in [PHP script to echo VLC now playing XML attributes](http://stackoverflow.com/q/16498059/367456) which *also* contains an answer that shows you how you can [extend simplexml to work more towards your need](http://stackoverflow.com/a/16512153/367456). – hakre May 17 '13 at 15:36

2 Answers2

1

Your xpathexpression is wrong, no / before Name:

$xml = simplexml_load_string($x); // assuming XML in $x
foreach($xml->Zone as $zone)
    echo "$zone[ID] - " . $zone->xpath("Name[@LngID='EN']")[0] . "<br />";

see it working: http://codepad.viper-7.com/dHSKPT

Edit thanks to Silkfire: PHP < 5.4 do:

foreach($xml->Zone as $zone) {

$name = $zone->xpath("Name[@LngID='EN']");
echo "$zone[ID] - $name[0]<br />";
}

see it working in PHP 5.2.17: http://codepad.viper-7.com/nHVrmV

Or with list:

foreach($xml->Zone as $zone) {

list($name) = $zone->xpath("Name[@LngID='EN']");
echo "$zone[ID] - $name<br />";
}
hakre
  • 193,403
  • 52
  • 435
  • 836
michi
  • 6,565
  • 4
  • 33
  • 56
0

Try this:

  $zone_lang = (string)array_pop($zone->xpath('Name[@LngID="EN"]'));
silkfire
  • 24,585
  • 15
  • 82
  • 105
  • 1
    I like that use of array_pop() +1 – michi May 17 '13 at 12:04
  • That's what I'm looking for :) Thank's Silkfire – Adrian May 17 '13 at 12:15
  • Just use `[0]`, it's the first entry. And array_pop *is wrong* because you don't use a variable here. This code gives NOTICES = Bad Practice, please do not suggest on Stackoverflow (at least not as *the only way to go*) (read: place a warning, show better code upfront etc. pp.) – hakre May 17 '13 at 15:27
  • array_pop will only give you errors in STRICT mode, which is not commonly used. – silkfire May 17 '13 at 18:12