Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath with GDataXMLNode in Objective C

I am making an iOS app where I'm using the GDataXML library for xml parsing in Objective C.

I have the following xml (that I get as an soap response):

<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
      <MakeRequestResponse xmlns="http://tempuri.org/">
        <MakeRequestResult>SOMERANDOMDATAHERE012912033905345346</MakeRequestResult>
      </MakeRequestResponse>
    </soap:Body>
  </soap:Envelope>

The problem I have is:

when I write the xpath expression for the given xml like this I get the MakeRequestResponse node:

NSArray *listOfNodes = [[responseDocument rootElement] nodesForXPath:@"soap:Body/*" error:&error];

However I'm unable to get this node or any children below when I write the actual node name:

NSArray *listOfNodes = [[responseDocument rootElement] nodesForXPath:@"soap:Body/MakeRequestResponse" error:&error];

I'm not sure what is the issue here. Could it be an namespace related issue?

like image 847
lgdev Avatar asked Feb 22 '23 09:02

lgdev


1 Answers

This is a FAQ (search for XPath and default namespace).

The short answer is that when an XPath expression is evaluated, any unprefixed names are assumed to be in "no namespace". But MakeRequestResponse is not in "no namespace" and thus isn't selected.

Solution:

Either register the "http://tempuri.org/" namespace and associate a prefix (say "x:") to it, then use:

soap:Body/x:MakeRequestResponse

Or, otherwise you can have an expression like this:

soap:Body/*[name() = 'MakeRequestResponse']
like image 60
Dimitre Novatchev Avatar answered Feb 25 '23 00:02

Dimitre Novatchev