0

我想做以下工作,但很难。我如何ns在 for 循环中循环?

ns = ['one', 'two']

tree = etree.parse(file) 
for items in tree.findall('.//' + ns):
    print(items)

编辑:寻找没有嵌套 for 循环的解决方案。

编辑:

   ns = ['{http://purl.org/dc/elements/1.1/}identifier[@{http://www.w3.org/2001/XMLSchema-instance}type]',
           '{http://purl.org/dc/elements/1.1/}title',
           '{http://purl.org/dc/elements/1.1/}description',
           '{http://purl.org/dc/elements/1.1/}subject',
           '{http://purl.org/dc/elements/1.1/}type',
           '{http://purl.org/dc/terms/}educationLevel']
   tree = etree.parse(file)
   for leaf in tree.xpath('//*[local-name()="record"]'):
       for items in leaf.findall('.//' + ns[0]):
           print(items)
4

1 回答 1

1

使用xpath

from lxml import etree

nsmap = {
    'xmlns': 'http://www.openarchives.org/OAI/2.0/',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'dct': 'http://purl.org/dc/terms/',
    'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
}
ns = [
    'dc:identifier[@xsi:type]',
    'dc:title',
    'dc:description',
    'dc:subject',
    'dc:type',
    'dct:educationLevel'
]
tree = etree.parse(file)
xpath = '|'.join('.//xmlns:record//{}'.format(n) for n in ns)
for items in tree.xpath(xpath, namespaces=nsmap):
    print(items)
于 2013-08-16T15:35:26.460 回答