问题
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">
<soap:Body>
<GetUserByEmailResponse xmlns="http://tempuri.org/">
<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:
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
回答2:
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']
来源:https://stackoverflow.com/questions/3830553/libxml-doesnt-find-any-nodes-for-my-xpath-expression