【发布时间】: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/…