python elementtree 命名空间_如何在python中使用ElementTree访问包含名称空间的xml中的属性值...

XML file:

I want to get the Terminal.ConnnectivityNode element's attribute value and Terminal element's attribute value also as output from the above xml. I have tried in below way!

Python code:

from elementtree import ElementTree as etree

tree= etree.parse(r'N:\myinternwork\files xml of bus systems\cimxmleg.xml')

cim= "{http://iec.ch/TC57/2008/CIM-schema-cim13#}"

rdf= "{http://www.w3.org/1999/02/22-rdf-syntax-ns#}"

Appending the below line to the code

print tree.find('{0}Terminal'.format(cim)).attrib

output1: : Is as expected

{'{http://www.w3.org/1999/02/22-rdf-syntax-ns#}ID': 'A_T1'}

If we Append with this below line to above code

print tree.find('{0}Terminal'.format(cim)).attrib['rdf:ID']

output2: key error in rdf:ID

If we append with this below line to above code

print tree.find('{0}Terminal/{0}Terminal.ConductivityEquipment'.format(cim))

output3 None

How to get output2 as A_T1 & Output3 as #A_CN1?

What is the significance of {0} in the above code, I have found that it must be used through net didn't get the significance of it?

解决方案

First off, the {0} you're wondering about is part of the syntax for Python's built-in string formatting facility. The Python documentation has a fairly comprehensive guide to the syntax. In your case, it simply gets substituted by cim, which results in the string {http://iec.ch/TC57/2008/CIM-schema-cim13#}Terminal.

The problem here is that ElementTree is a bit silly about namespaces. Instead of being able to simply supply the namespace prefix (like cim: or rdf:), you have to supply it in XPath form. This means that rdf:id becomes {http://www.w3.org/1999/02/22-rdf-syntax-ns#}ID, which is very clunky.

ElementTree does support a way to use the namespace prefix for finding tags, but not for attributes. This means you'll have to expand rdf: to {http://www.w3.org/1999/02/22-rdf-syntax-ns#} yourself.

In your case, it could look as following (note also that ID is case-sensitive):

tree.find('{0}Terminal'.format(cim)).attrib['{0}ID'.format(rdf)]

Those substitutions expand to:

tree.find('{http://iec.ch/TC57/2008/CIM-schema-cim13#}Terminal').attrib['{http://www.w3.org/1999/02/22-rdf-syntax-ns#}ID']

With those hoops jumped through, it works (note that the ID is A_T1 and not #A_T1, however). Of course, this is all really annoying to have to deal with, so you could also switch to lxml and have it mostly handled for you.

Your third case doesn't work simply because 1) it's named Terminal.ConductingEquipment and not Terminal.ConductivityEquipment, and 2) if you really want A_CN1 and not A_EF2, that's the ConnectivityNode and not the ConductingEquipment. You can get A_CN1 with tree.find('{0}Terminal/{0}Terminal.ConnectivityNode'.format(cim)).attrib['{0}resource'.format(rdf)].

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值