使用python 2.7 lxml中的命名空间访问xml文件中的值

时间:2016-03-25 20:48:23

标签: python namespaces lxml

我正在关注此链接以尝试获取多个标记的值:

Parsing XML with namespace in Python via 'ElementTree'

在此链接中,访问根标记没有问题,如下所示:

import sys
from lxml import etree as ET


doc = ET.parse('file.xml')

namespaces_rdf = {'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'} # add more as needed
namespaces_dcat = {'dcat': 'http://www.w3.org/ns/dcat#'} # add more as needed
namespaces_dct = {'dct': 'http://purl.org/dc/terms/'}

print doc.findall('rdf:RDF', namespaces_rdf)
print doc.findall('dcat:Dataset', namespaces_dcat)
print doc.findall('dct:identifier', namespaces_dct)

输出:

[]
[<Element {http://www.w3.org/ns/dcat#}Dataset at 0x2269b98>]
[]

我只能访问dcat:Dataset,我看不到如何访问rdf的值:about

以后访问dct:identifier

当然,一旦我访问了这个信息,我需要访问dcat:发布信息

这是我的示例文件,使用ckanext-dcat:

生成
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
  xmlns:dct="http://purl.org/dc/terms/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:dcat="http://www.w3.org/ns/dcat#"
>
  <dcat:Dataset rdf:about="http://www.myweb.com/dataset/ec631628-2f46-4f17-a685-d62a37466c01">
    <dct:identifier>ec631628-2f46-4f17-a685-d62a37466c01</dct:identifier>
    <dct:description>FOO-Description</dct:description>
    <dct:title>FOO-title</dct:title>
    <dcat:keyword>keyword1</dcat:keyword>
    <dcat:keyword>keyword2</dcat:keyword>
    <dct:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2014-10-08T08:55:04.566618</dct:issued>
    <dct:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2015-06-25T11:04:10.328902</dct:modified>
    <dcat:distribution>
      <dcat:Distribution rdf:about="http://www.myweb.com/dataset/ec631628-2f46-4f17-a685-d62a37466c01/resource/f5707551-6bf3-468f-9a96-b4184cc51d1f">
        <dct:title>FOO-title-1</dct:title>
        <dct:description>FOO-Description-1</dct:description>
        <dcat:accessURL>http://www.myweb.com/dataset/ec631628-2f46-4f17-a685-d62a37466c01/resource/f5707551-6bf3-468f-9a96-b4184cc51d1f/download/myxls.xls</dcat:accessURL>
        <dct:format>XLS</dct:format>
      </dcat:Distribution>
    </dcat:distribution>
    <dcat:distribution>
      <dcat:Distribution rdf:about="http://www.myweb.com/dataset/ec631628-2f46-4f17-a685-d62a37466c01/resource/74c1acc8-b2b5-441b-afb2-d072d0d00a7f">
        <dct:format>XLS</dct:format>
        <dct:title>FOO-title-2</dct:title>
        <dct:description>FOO-Description-2</dct:description>
        <dcat:accessURL>http://www.myweb.com/dataset/ec631628-2f46-4f17-a685-d62a37466c01/resource/74c1acc8-b2b5-441b-afb2-d072d0d00a7f/download/myxls.xls</dcat:accessURL>
      </dcat:Distribution>
    </dcat:distribution>
  </dcat:Dataset>
</rdf:RDF>

有关如何访问此信息的任何想法? 感谢

更新: 好吧,我需要访问 rdf:about

<dcat:Dataset rdf:about="http://www.myweb.com/dataset/ec631628-2f46-4f17-a685-d62a37466c01">

所以这段代码取自:

Parse xml with lxml - extract element value

for node in doc.xpath('//dcat:Dataset', namespaces=namespaces):
   # Iterate over attributes
   for attrib in node.attrib:
            print '@' + attrib + '=' + node.attrib[attrib]

我得到了这个输出:

[<Element {http://www.w3.org/ns/dcat#}Dataset at 0x23d8ee0>]
@{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about=http://www.myweb.com/dataset/ec631628-2f46-4f17-a685-d62a37466c01

所以,问题是:

我如何询问该属性是否才能获取此值,因为在其他文件中我有多个标记。

更新2:修正了我如何得到价值(克拉克符号)

for node in doc.xpath('//dcat:Dataset', namespaces=namespaces):
   # Iterate over attributes
   for attrib in node.attrib:
      if attrib.endswith('about'):
        #do my jobs

嗯,差不多完了,但我还有最后一个问题:我需要知道我何时访问

<dct:title>

属于哪个,我有:

<dcat:Dataset rdf:about="http://www.myweb.com/dataset/ec631628-2f46-4f17-a685-d62a37466c01">
       <dct:title>FOO-title</dct:title>

<dcat:Distribution rdf:about="http://www.myweb.com/dataset/ec631628-2f46-4f17-a685-d62a37466c01/resource/f5707551-6bf3-468f-9a96-b4184cc51d1f">
        <dct:title>FOO-title-1</dct:title>

<dcat:Distribution rdf:about="http://www.myweb.com/dataset/ec631628-2f46-4f17-a685-d62a37466c01/resource/74c1acc8-b2b5-441b-afb2-d072d0d00a7f">
        <dct:title>FOO-title-2</dct:title>

如果我这样做,我会得到:

for node in doc.xpath('//dct:title', namespaces=namespaces):
   print node.tag, node.text

{http://purl.org/dc/terms/}title FOO-title
{http://purl.org/dc/terms/}title FOO-title-1
{http://purl.org/dc/terms/}title FOO-title-2

由于

1 个答案:

答案 0 :(得分:1)

xpath()方法与namespaces命名参数一起使用:

namespaces = {
    'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    'dcat': 'http://www.w3.org/ns/dcat#',
    'dct': 'http://purl.org/dc/terms/'
}

print(doc.xpath('//rdf:RDF', namespaces=namespaces))
print(doc.xpath('//dcat:Dataset', namespaces=namespaces))
print(doc.xpath('//dct:identifier', namespaces=namespaces))