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

ren*_*ard 2 python xpath lxml elementtree xml-namespaces

我尝试使用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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

我已经阅读了libxml 教程,并尝试了findall在基本xml文件(没有任何名称空间)上使用方法的示例,并且该示例成功了。

Ste*_*ven 5

正如克里斯已经提到的,您还可以使用lxml和xpath。由于xpath不允许您像{http://www.openarchives.org/OAI/2.0/}record这样写完整的命名空间名称(所谓的“ James Clark表示法” *),因此您将必须使用前缀,并为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)
Run Code Online (Sandbox Code Playgroud)

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

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