Marcin
Marcin

Reputation: 61

Replace XML element with text

I have a strange, xml-like file which looks like this:

<line>
^FX czesc dostawcy^FS
^BY4,3,90^FT30,430^BCN,,N,N,N,D
^FD<param id="P_BARCODEIKEA" />^FS
^FT30,480^A0N,34^FH\^FD<param id="P_ARTYKULNAZWA" />^FS
^FT30,700^A0N,30^FH\^FD<param id="P_TODAY" />, <param id="P_CURRENTSHIFT" />,      
^FT520,600^A0N,120,120^FH\^FD<param id="P_LABELNUMBER" />^FS
^FT700,600^A0N,120,120^FH\^FD`1^SFnd,1^FS
^FX 30,410 GB800,0,3^FS
</line>

How can I replace, for example \<param id="P_ARTYKULNAZWA" \/\> with a text node in Python?

example: if parameter P_ARTYKULNAZWA = "10.10.10", would like to replace "<param id="P_ARTYKULNAZWA" />" with "10.10.10"

Upvotes: 0

Views: 132

Answers (1)

tdelaney
tdelaney

Reputation: 77347

This XSLT transform will replace elements with text. It can be integrated into a python script or you could use any conformant xslt processor. I kept the xslt selection narrow to your example, but the selection can be broadened as needed.

import lxml.etree

xml_text = '''\
<line>
^FX czesc dostawcy^FS
^BY4,3,90^FT30,430^BCN,,N,N,N,D
^FD<param id="P_BARCODEIKEA" />^FS
^FT30,480^A0N,34^FH\^FD<param id="P_ARTYKULNAZWA" />^FS
^FT30,700^A0N,30^FH\^FD<param id="P_TODAY" />, <param id="P_CURRENTSHIFT" />,      
^FT520,600^A0N,120,120^FH\^FD<param id="P_LABELNUMBER" />^FS
^FT700,600^A0N,120,120^FH\^FD`1^SFnd,1^FS
^FX 30,410 GB800,0,3^FS
</line>'''

xslt_text='''\
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    
  <xsl:output method="xml"/>
    
  <xsl:template match="param[@id='P_ARTYKULNAZWA']">
    <xsl:value-of select="@id"/>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>'''

doc = lxml.etree.XML(xml_text)
xslt = lxml.etree.XSLT(lxml.etree.XML(xslt_text))
new_doc = xslt(doc)
print(lxml.etree.tostring(new_doc, pretty_print=True).decode('utf-8'))

Result

<line>
^FX czesc dostawcy^FS
^BY4,3,90^FT30,430^BCN,,N,N,N,D
^FD<param>P_BARCODEIKEA</param>^FS
^FT30,480^A0N,34^FH\^FDP_ARTYKULNAZWA^FS
^FT30,700^A0N,30^FH\^FD<param>P_TODAY</param>, <param>P_CURRENTSHIFT</param>,      
^FT520,600^A0N,120,120^FH\^FD<param>P_LABELNUMBER</param>^FS
^FT700,600^A0N,120,120^FH\^FD`1^SFnd,1^FS
^FX 30,410 GB800,0,3^FS
</line>

Upvotes: 1

Related Questions