1

I unable to write xpath to find Element due to presence of multiple namespace.

XML:

<?xml version="1.0" encoding="utf-8"?>
<Mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.asam.net/XILAPI/Mapping/1.0">
  <FrameworkLabelList>
    <FrameworkLabel Id="Hazard_Switch" Type="StringVariable">
      <CustomProperties />
    </FrameworkLabel>
  </FrameworkLabelList>
  <TestbenchLabelList PortId="">
    <TestbenchLabel Id="BdyChassIO_slave/Model Root/BCM/Exterior_Lamp/HZS_CN4_Pin02_PHY/Control/Value">
      <TestbenchSimpleType Type="StringValue" />
    </TestbenchLabel>
  </TestbenchLabelList>
  <MappingTable>
    <RasterMappingList>
      <RasterMapping PortId="" FrameworkRasterName="" TestbenchRasterName="" />
    </RasterMappingList>
    <LabelMappingList>
      <LabelMapping>
        <TestbenchLabelReference LabelId="BdyChassIO_slave/Model Root/BCM/Exterior_Lamp/HZS_CN4_Pin02_PHY/Control/Value" PortId="">
          <FromSimple>
            <OneToOne />
          </FromSimple>
        </TestbenchLabelReference>
        <FrameworkLabelReference LabelId="Hazard_Switch" />
      </LabelMapping>
    </LabelMappingList>
    <StringMappingList>
      <StringMappingGroup PortId="">
        <StringMapping FrameworkString="" TestbenchString="" />
      </StringMappingGroup>
    </StringMappingList>
  </MappingTable>
</Mapping>

I know how to write xpath or simple path to findout particular Element from XML.

import xml.etree.ElementTree as ET
file_path = r'D:\Dummy\Test.xml'
namespaces = {}
def register_all_namespaces(filename):
            global namespaces
            # get namespace from xml file
            namespaces = dict([node for _, node in ET.iterparse(filename, events=['start-ns'])])
            #register namespace
            for ns in namespaces:
                ET.register_namespace(ns, namespaces[ns])
                print ns
                
register_all_namespaces(file_path)  
tree = ET.parse(file_path)
root = tree.getroot() 
fwl =  root.find('xsd/FrameworkLabelList/FrameworkLabel',namespaces)
print(fwl)
    

I want to know how to write xpath to find out "FrameworkLabel" tag.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Lalit kumar
  • 382
  • 4
  • 13

0 Answers0