我在iPhone应用程序中使用xpath和LibXML来查找xml文档中的一些节点。我是xpath中的菜鸟,所以我可能做错了什么。 这是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>
xpath表达式为//GetUserByEmailResult
。没有返回节点。我希望得到<GetUserByEmailResult>
。如果我测试相同的东西here它就可以了。
UPDATE1:这是我从.net网络服务中获取的内容。有没有办法忽略libxml中的命名空间?
答案 0 :(得分:3)
我不熟悉iphone库,但您可能必须在某处声明namsepace /前缀:如果您使用'someprefix'注册'http://tempuri.org/',您可以搜索:
//someprefix:GetUserByEmailResult
可能的解决方法是(但不建议):
//*[local-name()='GetUserByEmailResult']
答案 1 :(得分:2)
GetUserByEmailResult
元素位于http://tempuri.org/
命名空间中,因此您需要将该命名空间添加到XPath搜索中。
您可以使用xmlXPathRegisterNs
功能执行此操作:
xmlXPathRegisterNs(context, BAD_CAST "temp", BAD_CAST "http://tempuri.org/");
然后你的XPath必须改为
//temp:GetUserByEmailResult