Home > database >  How to add a root inside of main root of XML in Pyton
How to add a root inside of main root of XML in Pyton

Time:01-23

I have the following xml which i have export using the following:

df.to_xml('test.xml', index=False, row_name='instance', root_name='file')

which produces an xml file like:

<file>
  <instance>
    <ID>1</ID>
    <name>John</name>
    <age>32</age>
    <city>London</city>
  </instance>
....
</file>

How can i add an extra root (<NAMES>) for underneath <file> so my output is below:

<file>
<NAMES>
  <instance>
    <ID>1</ID>
    <name>John</name>
    <age>32</age>
    <city>London</city>
  </instance>
....
</NAMES>
</file>

CodePudding user response:

Add 'Files' as subelement to the root and to save it you can use standard file object.

import xml.etree.ElementTree as ET
root = ET.fromstring(df.to_xml(index=False, row_name='instance', root_name='file'))
file = ET.Element("file")
files = ET.SubElement(file, 'Files')
for e in root:
  files.append(e)
f = open('test.xml','w')
f.write(ET.tostring(file))

CodePudding user response:

Pandas XML IO does support XSLT 1.0 (the special-purpose language to transform XML files) for to_xml. See docs using the stylesheet argument which is only support by the default lxml parser (which you do use).

Below XSLT runs the identity transform to copy document as is then re-styles the <file> node to add a child <NAMES> element:

strXSLT = """\
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/> 

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

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

</xsl:stylesheet>"""

df.to_xml(
    'test.xml', 
    index = False, 
    row_name = 'instance', 
    root_name = 'file',
    stylesheet = strXSLT
)
  • Related