【问题标题】:Error in retrieving root element in xml using lxml python使用lxml python检索xml中的根元素时出错
【发布时间】:2015-03-19 09:02:34
【问题描述】:

我有以下 xml: val='''

<bookstore>
   <book>
    <title lang="en">Harry Potter</title>
    <price>29.99</price>
  </book>
  <book>
    <title lang="en">Learning XML</title>
    <price>39.95</price>
  </book>
</bookstore>

我想检索第一个元素,即“书店”,我正在做:

etree.parse(StringIO(xml1))
val.xpath('bookstore')

但它给出 [] 空列表,但当我为书做 val.xpath('book') 时,相同的脚本正在工作。

如何检索第一个元素??

【问题讨论】:

  • 每个 Python 文档 This module provides limited support for XPath expressions for locating elements in a tree. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the module. docs.python.org/2/library/…

标签: python xml xpath lxml


【解决方案1】:

你可以试试:

/bookstore  # child

//bookstore  # descendant at any depth

我不认为您在 atm 上正确定位节点

【讨论】:

  • 但我不知道它是如何为“书”元素工作的,而不是为它的父“书店”工作
猜你喜欢
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多