python - 使用 lxml.etree 在命名空间的 xml 元素中查找文本

标签 python xpath lxml xml-namespaces elementtree

我尝试使用 lxml.etree 来解析 XML 文件并将文本查找到 XML 的元素中。

XML 文件可以是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/
     http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
 <responseDate>2002-06-01T19:20:30Z</responseDate> 
 <request verb="ListRecords" from="1998-01-15"
      set="physics:hep"
      metadataPrefix="oai_rfc1807">
      http://an.oa.org/OAI-script</request>
 <ListRecords>
  <record>
    <header>
      <identifier>oai:arXiv.org:hep-th/9901001</identifier>
      <datestamp>1999-12-25</datestamp>
      <setSpec>physics:hep</setSpec>
      <setSpec>math</setSpec>
    </header>
    <metadata>
     <rfc1807 xmlns=
    "http://info.internet.isi.edu:80/in-notes/rfc/files/rfc1807.txt" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation=
       "http://info.internet.isi.edu:80/in-notes/rfc/files/rfc1807.txt
    http://www.openarchives.org/OAI/1.1/rfc1807.xsd">
    <bib-version>v2</bib-version>
    <id>hep-th/9901001</id>
    <entry>January 1, 1999</entry>
    <title>Investigations of Radioactivity</title>
    <author>Ernest Rutherford</author>
    <date>March 30, 1999</date>
     </rfc1807>
    </metadata>
    <about>
      <oai_dc:dc 
      xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ 
      http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
    <dc:publisher>Los Alamos arXiv</dc:publisher>
    <dc:rights>Metadata may be used without restrictions as long as 
       the oai identifier remains attached to it.</dc:rights>
      </oai_dc:dc>
    </about>
  </record>
  <record>
    <header status="deleted">
      <identifier>oai:arXiv.org:hep-th/9901007</identifier>
      <datestamp>1999-12-21</datestamp>
    </header>
  </record>
 </ListRecords>
</OAI-PMH>

对于以下部分,我们假设 doc = etree.parse("/tmp/test.xml")其中 text.xml 包含上面粘贴的 xml。

首先,我尝试找到所有 <record>元素使用 doc.findall(".//record")但它返回一个空列表。

其次,对于给定的单词,我想检查它是否在 <dc:publisher> 中. 为了实现这一点,我首先尝试做与之前相同的事情:doc.findall(".//publisher")但我有同样的问题...我很确定所有这些都与 namespace 相关联,但我不知道如何处理它们。

我已经阅读了 libxml tutorial , 并尝试了 findall 的示例基本 xml 文件(没有任何命名空间)上的方法,它成功了。

最佳答案

正如 Chris 已经提到的,您还可以使用 lxml 和 xpath。由于 xpath 不允许您像 {http://www.openarchives.org/OAI/2.0/}record(所谓的“James Clark notation”*)那样完整地写命名空间名称,您将必须使用前缀,并为 xpath 引擎提供前缀到命名空间 uri 的映射。

使用 lxml 的示例(假设您已经拥有所需的 tree 对象):

nsmap = {'oa':'http://www.openarchives.org/OAI/2.0/', 
         'dc':'http://purl.org/dc/elements/1.1/'}
tree.xpath('//oa:record[descendant::dc:publisher[contains(., "Alamos")]]',
            namespaces=nsmap)

这将选择所有具有后代元素 {http://purl.org/dc 的 {http://www.openarchives.org/OAI/2.0/}record 元素/elements/1.1/}dc 包含单词“Alamos”。

[*] 这来自 article James Clark 解释 XML 命名空间的地方,每个不熟悉命名空间的人都应该阅读这篇文章! (虽然是很久以前写的)

关于python - 使用 lxml.etree 在命名空间的 xml 元素中查找文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10798680/

相关文章:

html - 快速有效地解析损坏的 HTML 的方法?

python - lxml.html 使用 XPath 和变量解析

python - 使用 xml 在 Python 中解析 xml(一种正确的方法)

python - 从 peer 包中导入 conftest.py

python - 使用不同的参数调用所有父类的 __init__

python - 导入错误: cannot import name GdkX11

Python 和 sqlite3 - 添加数千行

xml - 如何使用xpath检索节点的第X个特定位置?

xml - xslt编程时 'or'和 '|'有什么区别?

java - 错误 : java. lang.ClassCastException : org. jsoup.nodes.Document 无法转换为 org.w3c.dom.Node