【问题标题】:Python libxml2 XPath/Namespace HelpPython libxml2 XPath/命名空间帮助
【发布时间】:2010-11-29 18:35:39
【问题描述】:

我正在尝试学习如何使用这个示例 XML 文件从 Python 执行 XPath 查询:http://pastie.org/1333021 我刚刚添加了一个命名空间,因为我的实际应用程序使用它。

基本上,我想执行一个返回节点子集的顶级查询,然后查询子集(比本示例的规模大得多)

所以这是我的代码,首先找到所有<food> 节点,然后遍历每个节点的描述。

#!/usr/bin/python2

import libxml2

doc = libxml2.parseFile("simple.xml")
context = doc.xpathNewContext()

context.xpathRegisterNs("db", "http://examplenamespace.com")
res = context.xpathEval("//db:food")

for node in res:
    # Query xmlNode here
    print "Got Food Node:"
    desc = node.xpathEval('db:description') # this is wrong?
    print desc

所以这本质上是一个命名空间问题,如果我从 XML 文件中删除 xlns 属性并仅使用基本的 XPATH 查询而不使用 db: 它工作正常。顶部查询 //db:food 工作正常,但第二个查询失败。

请有人更正我的命名空间/查询语法。

非常感谢

【问题讨论】:

    标签: python libxml2


    【解决方案1】:

    我通常不使用 libxml2,我更喜欢 lxml.etree。

    玩了一会。您节点上的 xpathEval 方法每次都会创建一个新上下文,显然没有您注册的命名空间。

    您可以像这样将上下文重置到不同的位置:

    >>> import libxml2
    >>> from urllib2 import urlopen
    >>> data = urlopen('http://pastie.org/pastes/1333021/download').read()
    >>>
    >>> doc = libxml2.parseMemory(data,len(data))
    >>>
    >>> context = doc.xpathNewContext()
    >>> context.xpathRegisterNs("db", "http://examplenamespace.com")
    0
    >>>
    >>> for res in context.xpathEval("//db:food"):
    ...     context.setContextNode(res)
    ...     print "Got Food Node:"
    ...     desc = context.xpathEval('./db:description')[0]
    ...     print desc
    ...
    Got Food Node:
    <description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
    Got Food Node:
    <description>light Belgian waffles covered with strawberries and whipped cream</description>
    Got Food Node:
    <description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
    Got Food Node:
    <description>thick slices made from our homemade sourdough bread</description>
    Got Food Node:
    <description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
    

    【讨论】:

    • 非常感谢,我想我对上下文变化感到困惑 :)
    猜你喜欢
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多