simpleXml select element by attribute value

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm trying to use simpleXml to parse a KML file and save some of the
KML data to a MySQL database.

I want to select a 'Style' element that has an attribute 'id' equal to
my PHP variable '$styleUrl.

A snippet of the KML to show how the Style elements are structured:

  <Style id=3D"style75">
  <Style id=3D"style76">
... stuff snipped here for brevity!

So i have loaded the KMl file:

$kml=3Dsimplexml_load_file('mykml.kml', 'SimpleXMLElement',

I query for all 'Style' tags with an 'id' attribute equal to my
$styleUrl variable:

echo $query; // outputs Style[@id=3D'style76']/IconStyle/Icon/href

echo count($href); // outputs 1

print_r($href); // outputs nothing

echo $href[0]; // outputs nothing

What is wrong with my xpath query?

I've Googled the afternoon away and tried all sorts but nothing works
I've only been using simpleXml and xpath for a day so this stuff is
all new to me.

Thanks a lot for any help.


Re: simpleXml select element by attribute value

Martin wrote:
Quoted text here. Click to load it

Do you have a statement similar to:

<?xml version="1.0" encoding="iso-8859-1"?>

at the start of the file to indicate the encoding?  If not, simplexml
will default to UTF-8.

Are you getting any errors?  Are you displaying errors?  In your php.ini
file on your development system you should have:


I suspect you may be running into a problem loading the file because it
contains a non-UTF-8 character in the .png name.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline