XPath with GDataXMLNode in Objective-C

I am creating an iOS application where I use the GDataXML library to parse xml in Objective C.

I have the following xml (which I get as a response to soap):

<?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> 

I have a problem:

when I write an xpath expression for a given xml like this, I get a MakeRequestResponse node:

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

However, I cannot get this node or other children below when I write the actual name of the node:

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

I am not sure what the problem is. Could this be a namespace related issue?

+4
source share
1 answer

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

The short answer is that when an XPath expression is evaluated, it is assumed that any names without prefixes are in "no namespace". But MakeRequestResponse not in a "no namespace" and therefore is not selected.

Decision

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

 soap:Body/x:MakeRequestResponse 

Or, otherwise you can have this expression:

 soap:Body/*[name() = 'MakeRequestResponse'] 
+8
source

Source: https://habr.com/ru/post/1381320/


All Articles