3

I am trying to parse an Atom feed. I have extended NSXMLNode with this method through a category:

- (NSString *)stringValueForNodeAtXPath:(NSString *)XPath error:(NSError **)error {
  NSArray *nodes = [self nodesForXPath:XPath error:error];
  if ([nodes count] == 0) { return nil; }
  return [[nodes lastObject] stringValue];
}

This method returns the string value of a node when only one node is expected.

An Atom feed's entry looks like this:

<entry>
  <title>A title</title>
  <id>Unique id</id>
</entry>

I have an NSXMLElement object like the one above. However, when trying to get either the title or the id of the entry, -[NSXMLNode nodesForXPath:error:] returns an empty array (and therefor my extension returns nil):

_uniqueId = [[XMLElement stringValueForNodeAtXPath:@"/entry/id" error:error] copy];

What could be the problem? Is my XPath query wrong? There is clearly an id in the entry, and entry is the root element.


Yes, NSLog-ing XMLElement confirms my last sentence.

  • 1
    Seems like there is a default namespace in your real sourceXML document. It is the most FAQ how to write XPath expressions for nodes in an XML document that has a default namespace. Just search "xpath default namespace". – Dimitre Novatchev Oct 08 '11 at 16:53
  • possible duplicate of [XPath with GDataXMLNode in Objective C](http://stackoverflow.com/questions/8138142/xpath-with-gdataxmlnode-in-objective-c) – Wayne Dec 01 '11 at 22:47
  • Or here: http://stackoverflow.com/questions/501171/no-nodes-selected-from-atom-xml-document-using-xpath – Wayne Dec 01 '11 at 22:50

0 Answers0