XPath and the default namespace

113 views
Skip to first unread message

Logan Allred

unread,
Feb 15, 2009, 12:15:40 PM2/15/09
to KissXML
I'm trying to move a Cocoa framework to the iPhone using KissXML. The
XML it parses (not under my control) uses mostly the default
namespace. Since libxml2 doesn't support XPath 2.0 queries, most of my
existing XPath expressions don't work in KissXML when they do in
NSXML.

Is anyone else in this predicament? How are you dealing with it? Are
you stripping the default namespace to the XPath will resolve the
nodes, or are you remapping the default namespace to a prefix and
changing all of your XPath queries?

Any help would be appreciated.

thanks,
Logan

Robbie Hanson

unread,
Feb 16, 2009, 6:23:35 PM2/16/09
to kis...@googlegroups.com
Can you send us an example XML document, and an example XPath 2.0 query?

-Robbie Hanson
-Deusty Designs

Eric J

unread,
Apr 3, 2009, 2:20:24 PM4/3/09
to KissXML
I'm having a similar problem. I've gotten around it by doing an
idiotic string replacement on the namespace declaration, but obviously
I'd like to do it "right."

Here's a sample XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<adc_database xmlns="http://www.accuweather.com" >

<citylist us="7" intl="1" extra_cities="0">
<location cnt="1" city="Waco" state="GA" location="30182"></
location>
<location cnt="2" city="Waco" state="KY" location="40385"></
location>
<location cnt="3" city="Waco" state="MO" location="64869"></
location>
<location cnt="4" city="Waco" state="NC" location="28169"></
location>
<location cnt="5" city="Waco" state="NE" location="68460"></
location>
<location cnt="6" city="Waco" state="TX" location="76701"></
location>
<location cnt="7" city="Waconia" state="MN" location="55387"></
location>
<location cnt="1" city="Waco" state="Canada(Quebec)"
location="NAM|CA|QC|Waco"></location>
</citylist>
<copyright>Copyright 2009 AccuWeather.com</copyright>
<use>This document is intended only for use by authorized licensees
of AccuWeather.com. Unauthorized use is prohibited. All Rights
Reserved.</use>
<product>dudefella</product>
<redistribution>Redistribution Prohibited.</redistribution>
</adc_database>

and here's the XPath:
//citylist/*


When I did this with NSXml, I was able to ignore the namespace --
everything "just worked." Under TouchXML, I had to add the following
commands to map the generic namespace:

NSDictionary * xmlMappings = [[NSDictionary alloc]
initWithObjectsAndKeys:@"http://www.accuweather.com", @"pre", nil];
NSArray * xmlNodes = [[NSArray alloc] initWithArray:[xmlDoc
nodesForXPath:@"//pre:citylist/*" namespaceMappings:xmlMappings
error:&error] copyItems:NO];

... and of course the XPath had to be changed to address the new
namespace prefix. So.
Reply all
Reply to author
Forward
0 new messages