Reputation: 97
I have the following XML. (Attribute Names are changed because of Privacy reasons)
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:Reference Uri="https://someURL.xml">
<edmx:Include Alias="Common" Namespace="com.sss.vocab.Common.v1"/>
</edmx:Reference>
<edmx:Reference Uri="https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Core.V1.xml">
<edmx:Include Alias="Core" Namespace="Org.OData.Core.V1"/>
</edmx:Reference>
<edmx:DataServices>
<Schema Namespace="MechanicalTechnicalService" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityContainer Name="EntityContainer">
<EntitySet Name="Cars" EntityType="MechanicalTechnicalService.Cars">
</EntitySet>
<EntitySet Name="Plane" EntityType="MechanicalTechnicalService.Plane">
</EntitySet>
<EntitySet Name="Ships" EntityType="MechanicalTechnicalService.Ships"/>
<EntitySet Name="Customer" EntityType="MechanicalTechnicalService.Customer"/>
<EntitySet Name="Letter" EntityType="MechanicalTechnicalService.Letter"/>
<EntitySet Name="Farm" EntityType="MechanicalTechnicalService.Farm">
</EntitySet>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
I want to access the Name Attribute of all EntitySets but i dont get any response. I am aware that namespaces have to be considered. I tried something like this:
namespace = {'edm' : 'http://docs.oasis-open.org/odata/ns/edm'}
print(root.findall('edm:EntitySet',namespace))
Here I dont get any response. But when using this:
namespace = {'edmx' : 'http://docs.oasis-open.org/odata/ns/edmx'}
print(root.findall('edmx:Reference',namespace))
I have access to the element. I am aware of the discussion here: Parsing XML with namespace in Python via 'ElementTree'
But in my case the solutions proposed here were not successfull. I just want to access all EntitySet tags in this XML document and save their names.
Upvotes: 1
Views: 188
Reputation: 24930
You are almost there. Try:
for n in root.findall('.//edm:EntitySet',namespace):
print(n.attrib["Name"])
Or, if you have the current version of python, you can use wildcards for namespaces:
for n in root.findall('.//{*}EntitySet'):
print(n.attrib["Name"])
In either case, the output should be
Cars
Plane
Ships
Customer
Letter
Farm
Upvotes: 2