简体   繁体   English

在python lxml中设置和访问名称空间

[英]Setting and accessing namespaces in python lxml

I am writing a script that processes a rdf:skos file with python3 and lxml : 我正在编写一个使用python3和lxml处理rdf:skos文件的脚本:

I learnt that I need to pass to the findall procedure the namespaces that the XML mentions. 我了解到我需要将XML提到的命名空间传递给findall过程。 (Ok, strange, since the XML files lists these in the header, so this seems like an unnecessary step but anyway). (好吧,很奇怪,因为XML文件在标头中列出了这些内容,所以这似乎是不必要的步骤,但无论如何)。

When calling 打电话时

for concept in root.findall('.//skos:Concept', namespaces=root.nsmap):

that works, because a root.nsmap is constructed by lxml . 之所以有效,是因为root.nsmap是由lxml构造的。

But then later in my code I also need to perform a test on xml:lang 但是后来在我的代码中,我还需要对xml:lang进行测试

for pl in concept.findall(".//skos:prefLabel[@xml:lang='en']", namespaces=root.nsmap):

and here python tells me python告诉我

SyntaxError: prefix 'xml' not found in prefix map

Ok, true, in my skos file there is no extra declaration for the xml namespace. 好的,是的,在我的skos文件中,没有xml命名空间的额外声明。 So I try to add it to the root.nsmap dict 所以我尝试将其添加到root.nsmap字典

root.nsmap['xml'] = "http://www.w3.org/XML/1998/namespace"

but that too doesn't work 但这也行不通

nsmap = {'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'uneskos': 'http://purl.org/umu/uneskos#', 'iso-thes': 'http://purl.org/iso25964/skos-thes#', 'dcterms': 'http://purl.org/dc/terms/', 'skos': 'http://www.w3.org/2004/02/skos/core#', 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#'}

Seems I am not allowed to modify the root.nsmap ? 似乎不允许我修改root.nsmap吗?

Anyone an idea how this is done? 有人知道这是怎么做的吗? I have processed tons of XML in the past with Perl XML::Twig which is very very comfortable and I assmue, the Python community has (at least) similarly comfortable ways to do that ... but how? 过去,我使用Perl XML :: Twig处理了大量的XML,这非常非常舒适,我敢保证,Python社区(至少)具有类似的舒适方式来实现此目的……但是如何?

Any hint appreciated. 任何提示表示赞赏。

Modifying root.nsmap has no effect. 修改root.nsmap无效。 But you can create another dictionary and modify that one. 但是您可以创建另一本词典并对其进行修改。 Example: 例:

from lxml import etree

doc = """
<root xmlns:skos="http://www.w3.org/2004/02/skos/core#">
   <skos:prefLabel xml:lang='en'>FOO</skos:prefLabel>
   <skos:prefLabel xml:lang='de'>BAR</skos:prefLabel>
</root>"""

root = etree.fromstring(doc)
nsmap = root.nsmap
nsmap["xml"] = "http://www.w3.org/XML/1998/namespace" 

en = root.find(".//skos:prefLabel[@xml:lang='en']", namespaces=nsmap)
print(en.text)

Output: 输出:

FOO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM