XPath namespace pain

I’ve been playing around some with the Google Maps API, via ym4r. I discovered, however, that its geocoding API doesn’t do anything with the “accuracy” information that Google gives you, which tells you if the address you fed it actually means much. The XML looks something like this:

<Placemark>
  <address>Italy</address>
  <AddressDetails Accuracy="1" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">

I opened up geocoding.rb, and had a look, figuring it had to be pretty simple, and indeed, with REXml’s XPath stuff, you can easily grab the elements you want. For example:

data_country = data['//CountryNameCode']

So… data['//AddressDetails'].attributes['Accuracy'] ought to give me what I want, right? Nope. The namespace in the sub element confuses everything. After some googling, I finally found the recipe:

data_accuracy = data['//*[local-name()="AddressDetails"]'].attributes['Accuracy']

Kind of a mess, but it works, I have my accuracy attribute, and I submitted a patch to the ym4r guy.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s