How to access attribute value in xml containing namespace using ElementTree in python

Viewed 4283

XML file:

<?xml version="1.0" encoding="iso-8859-1"?>
<rdf:RDF xmlns:cim="http://iec.ch/TC57/2008/CIM-schema-cim13#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<cim:Terminal rdf:ID="A_T1">
<cim:Terminal.ConductingEquipment rdf:resource="#A_EF2"/>
<cim:Terminal.ConnectivityNode rdf:resource="#A_CN1"/>
</cim:Terminal>
</rdf:RDF>

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?

1 Answers
Related