views:

77

answers:

2

Hello!

I'm using xpath and LibXML in an iPhone app to find some nodes in an xml document. I'm a noob in xpath so probably i am doing something wrong. Here is the xml:


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <soap:Body>
    <GetUserByEmailResponse xmlns="http://tempuri.org/"&gt;
      <GetUserByEmailResult>
        <id>4006</id>
        <name>Kudor Gyozo</name>
        <email/>
        <image/>
        <facebookId>0</facebookId>
      </GetUserByEmailResult>
    </GetUserByEmailResponse>
  </soap:Body>
</soap:Envelope>

The xpath expression is //GetUserByEmailResult. No nodes are returned. I expect to get <GetUserByEmailResult> back. If I test the same stuff here it works ok.

UPDATE1: This is what i get back from a .net web service. Is there a way to ignore namespaces in libxml?

+1  A: 

I'm not familiar with the iphone library, but you'll probably have to declare the namsepace / prefix somewhere: If you register 'http://tempuri.org/' with 'someprefix', you can search for:

//someprefix:GetUserByEmailResult

A possible workaround is (but not advisable):

//*[local-name()='GetUserByEmailResult']
Wrikken
+1  A: 

The GetUserByEmailResult element is in the http://tempuri.org/ namespace so you would need to add that namespace to your XPath search.

You can do so using the xmlXPathRegisterNs function:

xmlXPathRegisterNs(context,  BAD_CAST "temp", BAD_CAST "http://tempuri.org/");

Then your XPath would have to be changed to

//temp:GetUserByEmailResult
0xA3
Yes this works thanks a lot. BTW is there a way to ignore namespaces?
kudor gyozo
@kudor gyozo: Yes, you can ignore namespaces by only checking the local names in your XPath expressions as suggested in Wrikken's answer.
0xA3