【问题标题】:Parsing XPath within non standard XML using lxml Python使用 lxml Python 在非标准 XML 中解析 XPath
【发布时间】:2012-02-26 23:00:11
【问题描述】:

我正在尝试创建一个包含来自 Google 专利的所有专利信息的数据库。到目前为止,我的大部分工作都是在 Python to parse non-standard XML file 中使用来自 MattH 的这个非常好的答案。我的 Python 太大而无法显示,所以它链接了here

源文件are here: 一堆 xml 文件一起附加到一个具有多个标题的文件中。问题是在解析这个具有多个 xml 和 dtd 声明的不寻常的“非标准”XML 文件时尝试使用正确的 xpath 表达式。我一直在尝试使用"-".join(doc.xpath 在解析出所有内容时将其绑定在一起,但输出会为<document-id><classification-national> 创建由连字符分隔的空格,如下所示

<references-cited> <citation> 
<patcit num="00001"> <document-id>
<country>US</country> 
<doc-number>534632</doc-number> 
<kind>A</kind>
<name>Coleman</name> 
<date>18950200</date> 
</document-id> </patcit>
<category>cited by examiner</category>
<classification-national><country>US</country>
<main-classification>249127</main-classification></classification-national>
</citation>

注意并非所有子代都存在于每个 &lt;citation&gt; 中,有时它们根本不存在。

如何在尝试在 &lt;citation&gt; 下的多个条目的每个数据条目之间放置连字符时解析此 xpath?

【问题讨论】:

  • 请减少您的问题。如果我理解你,问题在于用 Python 构建正确的 XPath 表达式。
  • 为了便于阅读,我再次编辑了问题。

标签: python xml xpath lxml


【解决方案1】:

从此 XML (references.xml),

<references-cited> 
  <citation> 
    <patcit num="00001"> 
      <document-id>
        <country>US</country> 
        <doc-number>534632</doc-number> 
        <kind>A</kind>
        <name>Coleman</name> 
        <date>18950200</date> 
      </document-id> 
    </patcit>
    <category>cited by examiner</category>
    <classification-national>
      <country>US</country>
      <main-classification>249127</main-classification>
    </classification-national>
  </citation>

  <citation>
    <patcit num="00002">
      <document-id>
        <country>US</country>
        <doc-number>D28957</doc-number>
        <kind>S</kind>
        <name>Simon</name>
        <date>18980600</date>
      </document-id>
    </patcit>
    <category>cited by other</category>
  </citation>
</references-cited>

您可以获取&lt;citation&gt;的每个后代的文本内容,其内容如下:

from lxml import etree

doc = etree.parse("references.xml")
cits = doc.xpath('/references-cited/citation')

for c in cits:
    descs = c.xpath('.//*')
    for d in descs:
        if d.text and d.text.strip():
            print "%s: %s"  %(d.tag, d.text)
    print

输出:

country: US
doc-number: 534632
kind: A
name: Coleman
date: 18950200
category: cited by examiner
country: US
main-classification: 249127

country: US
doc-number: D28957
kind: S
name: Simon
date: 18980600
category: cited by other

这种变化:

import sys
from lxml import etree

doc = etree.parse("references.xml")
cits = doc.xpath('/references-cited/citation')

for c in cits:
    descs = c.xpath('.//*')
    for d in descs:
        if d.text and d.text.strip():
            sys.stdout.write("-%s"  %(d.text))
    print

结果如下:

-US-534632-A-Coleman-18950200-cited by examiner-US-249127
-US-D28957-S-Simon-18980600-cited by other

【讨论】:

  • 我感觉我们已经很接近了,但是当我尝试这种方法时,它没有产生任何输出。我已经编辑了上面的问题,以正确解释我的问题。
  • 您是按原样尝试我的示例,还是尝试在另一个 XML 文档上使用我的 Python 代码?
  • 我在另一个 XML 上使用了它。只是你给我的最后 4 行,从 decs = doc.xpath
  • 我认为出现问题的原因是因为我使用的 xml 基于 .zip 并且其中有多个 xml 声明。这种文件类型就是这样,这就是为什么顶部引用的 MattH 创建的 python 能够解析的原因。此外,当正确编辑 xml 时,您的 python 完全按原样运行,但在 shell 或单独的文件中没有打印输出
  • 您的问题是关于 XPath 问题。您不能在“非标准”XML 上评估 XPath 表达式。我的例子有效。
猜你喜欢
  • 2020-04-10
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多