我正在尝试查找值book
的所有abc
元素,即name
标记值。我使用了xpath:
val= xml1.xpath('//bookstore/book/name[text()="abc"]')
但它正在返回无。
<bookstore>
<book>
<name>abc</name>
<price>30</price>
</book>
<book>
<name>Learning XML</name>
<price>56</price>
</book>
</bookstore>
答案 0 :(得分:3)
为书籍标签添加了Id属性。
root.xpath("//bookstore/book/name[text()='abc']
它会列出name
text
所有abc
元素的列表,而不是父元素。
检查以下内容:
>>> data = """<bookstore>
... <book id="1">
... <name>abc</name>
... <price>30</price>
... </book>
... <book id="2">
... <name>Learning XML</name>
... <price>56</price>
... </book>
... </bookstore> """
>>> root = PARSER.fromstring(data)
>>> root.xpath("//bookstore/book")
[<Element book at 0xb726d144>, <Element book at 0xb726d2d4>]
>>> root.xpath("//bookstore/book/name[text()='abc']")
[<Element name at 0xb726d9b4>]
>>> root.xpath("//bookstore/book/name[text()='abc']/parent::*")
[<Element book at 0xb726d7d4>]
>>> root.xpath("//bookstore/book/name[text()='abc']/parent::*")[0].attrib
{'id': '1'}
Python初学者:
name
标记进行迭代。text
代码的name
等于abc
。name
标签的父级并附加到列表变量。代码:
>>> root = PARSER.fromstring(data)
>>> abc_parent = []
>>> for i in root.getiterator("name"):
... if i.text=="abc":
... abc_parent.append(i.getparent())
...
>>> print abc_parent
[<Element book at 0xb726d2d4>]
>>> abc_parent[0].attrib
{'id': '1'}
答案 1 :(得分:2)
这是一种方法:
from lxml import etree
# Create an ElementTree instance
tree = etree.parse("bookstore.xml")
# Get all 'book' elements that have a 'name' child with a string value of 'abc'
books = tree.xpath('book[name="abc"]')
# Print name and price of those books
for book in books:
print book.find("name").text, book.find("price").text
在问题中使用XML时的输出:
abc 30