LibXML doesn't find any nodes for my xpath expression

寵の児 提交于 2021-02-17 04:40:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!