Giles
Giles

Reputation: 1668

NSXMLElement nodesForXPath .//<name> returns more matches than descendants

I have a strange issue using the nodesForXPath method on an NSXMLElement.
I am using the call to find all descendants of the element matching a name. E.g.:

NSError *error = nil;
NSArray *decendants = [self nodesForXPath:@".//xr" error:&error];

This generally seems to work throughout my code.
Occasionally however I get more matches returned than there are element descendants.
In this case it seems to be finding all items matching the name from the current element to the document end.

This only seems to happen in cases where there is only one 'layer' of descendants.
I.e. none of the element's child elements have children.

It is a huge document that I am processing (80MB).
I am stripping out some elements as I go, but I am normalising nodes before searching.

[self normalizeAdjacentTextNodesPreservingCDATA:NO];

Can anyone suggest what the cause of this issue may be? Areas to investigate?

Thank you.

Upvotes: 0

Views: 197

Answers (1)

dgatwood
dgatwood

Reputation: 10407

Not just you. After some experimentation, it looks like nodesForXPath, objectsForXQuery, and friends are badly broken when you specify a descendant selector. Instead of returning the matching descendants of the current element, they sometimes return every matching element beginning with the descendants of the current element, and continuing until the end of the document.

This seems to occur only if the searched-for element appears as an immediate child of the element. (Your mileage may vary—I haven't tested this extensively.)

I've reported the bug as rdar://18317629, and have documented it in OpenRadar. Until that bug gets fixed, you'll have to iterate the DOM tree manually or carefully avoid searching for descendants in elements that contain matching elements as their immediate children.

Upvotes: 1

Related Questions