1

I have a big mystery here,

I have a Gdataxml document property:

GDataXMLDocument *doc;

I'm adding a new element to doc, interestingly, this method below looks perfect for other elements but not for the element I just added:

GDataXMLElement *newValueDefElement = [GDataXMLNode elementWithName:@"valuedefinition"];
[variableElement addChild:newValueDefElement];

and now when I query:

NSString *path = [NSString stringWithFormat:@"//inferenceresponse/state/variable[pageId=%d]/valuedefinition",pageID];       
NSArray *valueElement = [self.doc nodesForXPath:path error:nil];

Now array comes with zero objects! new added element NOT found! but I can see it in debug as xml string, how on earth it can not find something which I can see it is there on the log? it is a cache problem or a namespace problem or a bug in GDataXML? again..Problem is adding a new child and it is somehow not updated in the doc, but I can get the other elements under same root when use the same Xpath query standard

in NSlog I can see that the new element is added to doc.

NSData *xmlData2 = self.doc.XMLData;
NSString *s= [[[NSString alloc] initWithBytes:[xmlData2 bytes] length:[xmlData2 length] encoding:NSUTF8StringEncoding] autorelease];
NSLog(s);

Also How can self.doc.XMLData give something different than [self.doc nodesForXPath]? so it fools me to thing my doc is ok but maybe I corrupted the doc or a wrong namespace while adding removing some elements in a previous method?

my xml starts like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<inferenceresponse xmlns="">
<state goalreached="false">
..
..

Update

I just found a (hacky) solution; when I convert "doc" to NSData with "doc.XMLData" and then again convert back to doc, then it works! but this should not be real solution, that's lame to do that conversion back and forth to get a correct document object. What is the problem here? I guess it can not fix the namespaces for new child.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Spring
  • 11,333
  • 29
  • 116
  • 185
  • What combination of programming-language, Xpath-engine and XML/XPath API are you using? Also, it is unlikely you'll get any response, because you have put this question in "community wiki", which by definition doesn't give any rep for answers. It would be a good idea to delete this question and resubmit it again (not in "community wiki") and add the additional information I am asking for in this comment. – Dimitre Novatchev Aug 13 '11 at 16:26
  • @Dimitre Novatchev objective-c and GdataXML's xpath support, I am not aware that it is in community wiki! what will happen to bounty then? – Spring Aug 13 '11 at 16:34
  • I see, @XDeveloper. In this case this isn't an XPath question at all, but a question about GDataXML -- could you please, retag, deleting the "xpath" tag? – Dimitre Novatchev Aug 13 '11 at 16:47

1 Answers1

1

Your problem is here:

<inferenceresponse xmlns="">

The empty namespace attribute is obviously confusing the libxml XPath evaluation. If you step through GDataXMLNode's nodesForXPath:namespaces:error:, xmlXPathEval indeed returns an empty nodes set.

If you have control over the XML generation, I've got correct XPath results removing the empty attribute.

<inferenceresponse>

If modifying the server response is too hard, you can edit GDataXMLNode.m: Find the method fixQualifiedNamesForNode:graftingToTreeNode: in GDataXMLNode implementation and replace the line

if (foundNS != NULL) {
    // we found a namespace, so fix the ns pointer and the local name

with

if (foundNS != NULL && foundNS->href != NULL && strlen((char *)foundNS->href) != 0) {
    // we found a namespace, so fix the ns pointer and the local name
Jilouc
  • 12,684
  • 4
  • 46
  • 43
  • Don't know, but may be related to the empty xmlns attribute: http://stackoverflow.com/questions/135000/how-to-prevent-blank-xmlns-attributes-in-output-from-nets-xmldocument – Jilouc Aug 17 '11 at 11:32
  • How do I remove it, I create that element in java WS like this Element responseRoot = new Element(inferenceResponseTag); responseRoot.addContent(createQuestionSection(response)); – Spring Aug 18 '11 at 08:05
  • What's in `createQuestionSection`? From what I've seen, children need to be added with the same namespace as their parent to avoid the empty xmlns attribute serialization. – Jilouc Aug 18 '11 at 08:32
  • no namespace related code is in createQuestionSection, or even in all class, I use spring WS maybe I can set the NS from there but have to resaerch for it – Spring Aug 18 '11 at 09:00
  • Thanks! worked, can I be sure this new if statement does not break anything else? and whats is the point of doing this? – Spring Aug 18 '11 at 14:24
  • I don't think it should break anything else. There's a lot of stuff done in GDataXMLNode to fix the namespace mess when you add a child. And obviously it's not enough. – Jilouc Aug 18 '11 at 14:33