2

The following is the root tag of the XML. When I parse this XML file using xml.etree.ElementTree, I'm unable to retain the attributes with xmlns prefix.

<ecore:EPackage xmi:version="2.0" 
                xmlns:xmi="http://www.omg.org/XMI" 
                xmlns:ComIbmCompute.msgnode="ComIbmCompute.msgnode" 
                xmlns:ComIbmWSInput.msgnode="ComIbmWSInput.msgnode" 
                xmlns:ComIbmWSReply.msgnode="ComIbmWSReply.msgnode" 
                xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" 
                xmlns:eflow="http://www.ibm.com/wbi/2005/eflow" 
                xmlns:utility="http://www.ibm.com/wbi/2005/eflow_utility" 
                nsURI="POC_UDP_ESQL.msgflow" 
                nsPrefix="POC_UDP_ESQL.msgflow">

I tried registering namespace using register_namespace method in Python, then I'm able retain namespaces which I'm using in the xml (ecore:EPackage) and unable to retain the other attributes (xmlns:ComIbmCompute.msgnode="ComIbmCompute.msgnode") which I'm not using in the xml.

XML output I'm getting:

<ecore:EPackage xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
    xmlns:xmi="http://www.omg.org/XMI" xmi:version="2.0" nsURI="POC_UDP_ESQL.msgflow" nsPrefix="POC_UDP_ESQL.msgflow">
    <eClassifiers xmi:type="eflow:FCMComposite" name="FCMComposite_1" nodeLayoutStyle="RECTANGLE">
        <eSuperTypes href="http://www.ibm.com/wbi/2005/eflow#//FCMBlock"/>
        <eStructuralFeatures xmi:type="ecore:EAttribute" xmi:id="Property.id" name="id" lowerBound="1" defaultValueLiteral="1">
            <eType xmi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
        </eStructuralFeatures>
        <eStructuralFeatures xmi:type="ecore:EAttribute" xmi:id="Property.value" name="value" lowerBound="1" defaultValueLiteral="esql">
            <eType xmi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
        </eStructuralFeatures>
        <composition>
            <nodes xmi:type="ComIbmWSInput.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_1" location="176,238" URLSpecifier="/test/udp/esql/1">
                <translation xmi:type="utility:ConstantString" string="HTTP Input"/>
            </nodes>
            <nodes xmi:type="ComIbmWSReply.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_2" location="781,234">
                <translation xmi:type="utility:ConstantString" string="HTTP Reply"/>
            </nodes>
            <nodes xmi:type="ComIbmCompute.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_3" location="465,233" computeExpression="esql://routine/#POC_Using_ESQL_Compute.Main">
                <translation xmi:type="utility:ConstantString" string="Compute"/>
            </nodes>
            <connections xmi:type="eflow:FCMConnection" xmi:id="FCMConnection_1" targetNode="FCMComposite_1_3" sourceNode="FCMComposite_1_1" sourceTerminalName="OutTerminal.out" targetTerminalName="InTerminal.in"/>
            <connections xmi:type="eflow:FCMConnection" xmi:id="FCMConnection_2" targetNode="FCMComposite_1_2" sourceNode="FCMComposite_1_3" sourceTerminalName="OutTerminal.out" targetTerminalName="InTerminal.in"/>
        </composition>
        <stickyBoard/>
    </eClassifiers>
</ecore:EPackage>
  • Input and the expected output xml(Just trying to parse the input and write into a file):

    <?xml version="1.0" encoding="UTF-8"?>
    <ecore:EPackage xmi:version="2.0"
        xmlns:xmi="http://www.omg.org/XMI"
        xmlns:ComIbmCompute.msgnode="ComIbmCompute.msgnode"
        xmlns:ComIbmWSInput.msgnode="ComIbmWSInput.msgnode"
        xmlns:ComIbmWSReply.msgnode="ComIbmWSReply.msgnode"
        xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
        xmlns:eflow="http://www.ibm.com/wbi/2005/eflow"
        xmlns:utility="http://www.ibm.com/wbi/2005/eflow_utility" nsURI="POC_UDP_ESQL.msgflow" nsPrefix="POC_UDP_ESQL.msgflow">
        <eClassifiers xmi:type="eflow:FCMComposite" name="FCMComposite_1" nodeLayoutStyle="RECTANGLE">
            <eSuperTypes href="http://www.ibm.com/wbi/2005/eflow#//FCMBlock"/>
            <eStructuralFeatures xmi:type="ecore:EAttribute" xmi:id="Property.id" name="id" lowerBound="1" defaultValueLiteral="1">
                <eType xmi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
            </eStructuralFeatures>
            <eStructuralFeatures xmi:type="ecore:EAttribute" xmi:id="Property.value" name="value" lowerBound="1" defaultValueLiteral="esql">
                <eType xmi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
            </eStructuralFeatures>
            <composition>
                <nodes xmi:type="ComIbmWSInput.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_1" location="176,238" URLSpecifier="/test/udp/esql/1">
                    <translation xmi:type="utility:ConstantString" string="HTTP Input"/>
                </nodes>
                <nodes xmi:type="ComIbmWSReply.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_2" location="781,234">
                    <translation xmi:type="utility:ConstantString" string="HTTP Reply"/>
                </nodes>
                <nodes xmi:type="ComIbmCompute.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_3" location="465,233" computeExpression="esql://routine/#POC_Using_ESQL_Compute.Main">
                    <translation xmi:type="utility:ConstantString" string="Compute"/>
                </nodes>
                <connections xmi:type="eflow:FCMConnection" xmi:id="FCMConnection_1" targetNode="FCMComposite_1_3" sourceNode="FCMComposite_1_1" sourceTerminalName="OutTerminal.out" targetTerminalName="InTerminal.in"/>
                <connections xmi:type="eflow:FCMConnection" xmi:id="FCMConnection_2" targetNode="FCMComposite_1_2" sourceNode="FCMComposite_1_3" sourceTerminalName="OutTerminal.out" targetTerminalName="InTerminal.in"/>
            </composition>
            <stickyBoard/>
        </eClassifiers>
    </ecore:EPackage>

  • Python code:

    import xml.etree.ElementTree as xml
    
    messageFlowFile = xml.parse('sample.xml')
    print(messageFlowFile.getroot())
    xml.register_namespace('xmi','http://www.omg.org/XMI')
    xml.register_namespace('ecore','http://www.eclipse.org/emf/2002/Ecore')
    #xml.register_namespace('eflow','http://www.ibm.com/wbi/2005/eflow')
    #xml.register_namespace('utility','http://www.ibm.com/wbi/2005/eflow_utility')
    messageFlowRoot = messageFlowFile.getroot()
    
    print(messageFlowRoot.attrib)
    
    
    messageFlowFile.write('NewMessage.xml', encoding='UTF-8', xml_declaration=True)

Akhil
  • 21
  • 3
  • Use lxml instead of ElementTree. See https://stackoverflow.com/q/45990761/407651 – mzjn Oct 25 '22 at 07:21
  • I tried lxml, same result in that case as well – Akhil Oct 25 '22 at 07:38
  • Where is your Python code? What are you doing to the XML to get the output you mention? – Parfait Oct 25 '22 at 21:11
  • Updated with the code and input – Akhil Oct 26 '22 at 04:45
  • Note that namespace declarations are different from "normal" attributes. They are not included in the `.attrib` dictionary. – mzjn Oct 26 '22 at 05:13
  • Okay, but they are not available in the output file as well – Akhil Oct 26 '22 at 05:34
  • Thank you @mzjn, I'm able to retain the existing namespaces, but unable to add a new attribute with xmlns prefix. trying to add xmlns:UDP_Properties_getUDPProperties.subflow="UDP_Properties/getUDPProperties.subflow" tried etree.register_namespace('xmlns:UDP_Properties_getUDPProperties.subflow', 'UDP_Properties/getUDPProperties.subflow') Error: ValueError: Invalid tag name 'xmlns:UDP_Properties_getUDPProperties.subflow' Please help me here. – Akhil Oct 26 '22 at 09:46
  • I think you should ask a new question about that. – mzjn Oct 26 '22 at 10:06
  • https://stackoverflow.com/questions/74206503/adding-a-new-attribute-with-xmlns-prefix – Akhil Oct 26 '22 at 10:46

1 Answers1

0

By using lxml, I was able to achieve this task


    from lxml import etree
    
    tree = etree.parse('sample.xml')
    #Making some modifications
    tree.write('newMessage.xml')

Akhil
  • 21
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – user11717481 Oct 30 '22 at 07:51