0

I have an xml where I'm making some changes and want to add an attribute to the root tag with xmlns prefix. Unable to do it.

Input xml:

<?xml version="1.0" encoding="UTF-8"?>
<Data xmi:version="2.0"
        xmlns:xmi="http://www.omg.org/XMI"
        xmlns:ComIbmCompute.msgnode="ComIbmCompute.msgnode"
        xmlns:utility="http://www.ibm.com/wbi/2005/eflow_utility">
        <eClassifiers xmi:type="eflow:FCMComposite" name="FCMComposite_1" nodeLayoutStyle="RECTANGLE">
            <eSuperTypes href="http://www.ibm.com/wbi/2005/eflow#//FCMBlock"/>
            
            <stickyBoard/>
        </eClassifiers>
</Data>

Need to add new attribute:

xmlns:UDP_Properties_getUDPProperties.subflow="UDP_Properties/getUDPProperties.subflow".

I don't have an xmlns defined in the xml to set an attribute with xmlns prefix

Expected output:

<?xml version="1.0" encoding="UTF-8"?>
<Data xmi:version="2.0"
        xmlns:xmi="http://www.omg.org/XMI"
        xmlns:ComIbmCompute.msgnode="ComIbmCompute.msgnode"
        xmlns:UDP_Properties_getUDPProperties.subflow="UDP_Properties/getUDPProperties.subflow"
        xmlns:utility="http://www.ibm.com/wbi/2005/eflow_utility">
        <eClassifiers xmi:type="eflow:FCMComposite" name="FCMComposite_1" nodeLayoutStyle="RECTANGLE">
            <eSuperTypes href="http://www.ibm.com/wbi/2005/eflow#//FCMBlock"/>
            
            <stickyBoard/>
        </eClassifiers>
</Data>
mzjn
  • 48,958
  • 13
  • 128
  • 248
Akhil
  • 21
  • 3
  • Where is your attempted Python code for [mcve]? – Parfait Oct 26 '22 at 14:02
  • There is no obvious way to do this, AFAIK (apart from resorting to plain text manipulation). Working with namespaces using XML APIs is sometimes surprisingly hard. – mzjn Oct 26 '22 at 15:01
  • @mzjn, can you give me any reference link for plain text manipulation – Akhil Oct 27 '22 at 05:47
  • Treat the XML as a string on which you execute `replace()`, similar to https://stackoverflow.com/a/20956523/407651. Or maybe you can use XSLT: https://stackoverflow.com/a/51660868/407651. – mzjn Oct 27 '22 at 06:46
  • It seems you are working with Ecore and XMI and you try to register a new metamodel in your XMI for further usage I suppose. Did you try pyecore? It's an implementation of Ecore in Python which is compatible with EMF (the API and XMI), that will automatically deal with that for you as long as you import your new metamodel and relates to elements of it in your base metamodel. – Vincent Aranega Oct 27 '22 at 15:55

0 Answers0