Python XPath/libxml2名称空间查询 [英] Python XPath / libxml2 namespace query

查看:88
本文介绍了Python XPath/libxml2名称空间查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试解析London Underground Linestatus XML提要"-收效甚微.我本来希望使用XPath可以轻松",但是我得到的是空节点.

I have been trying to parse the London Underground Linestatus XML "feed" - with little success. I would have expected this to ne "easy" using XPath, but I am getting empty nodes.

我相当确定我没有正确处理uk名称空间.

I am fairly sure sure that I am not dealing with the uk namespace correctly.

这是我的(相当简单的代码):

Here is my (rather simple code):

import libxml2
from urllib2 import urlopen

data = urlopen('http://cloud.tfl.gov.uk/TrackerNet/LineStatus').read()

try:
    doc = libxml2.parseDoc(data)
except (libxml2.parserError, TypeError):
    print "Problems loading XML"

context = doc.xpathNewContext()
context.xpathRegisterNs("uk", "http://webservices.lul.co.uk")

record_nodes = context.xpathEval('//uk:LineStatus')

for node in record_nodes:
    print "******************************"

record_nodes循环被忽略. xml已正确解析.

The record_nodes loop is being ignored. The xml is being parsed correctly.

有人可以帮忙澄清一下吗?

Can someone please shed some light on this.

推荐答案

您需要在名称空间URI的末尾添加一个正斜杠.正确的URI是http://webservices.lul.co.uk/.

You need to add a forward slash at the end of the namespace URI. The correct URI is http://webservices.lul.co.uk/.

这篇关于Python XPath/libxml2名称空间查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆