I have a dataframe like this:
col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8 | col9 | col10 |
---|---|---|---|---|---|---|---|---|---|
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
and i want to create an xml like this:
<?xml version='1.0' encoding='utf-8'?>
<root xmlns:xsi="http://www.example.com" xmlns="http://www.example.com">
<all>
<col>
<col1>...</col1>
<col2>...</col2>
<col3>...</col3>
<col4>...</col4>
<col5>...</col5>
<col6>...</col6>
<group1>
<col7>...</col7>
<col8>...</col8>
</group1>
<group2>
<col9>...</col9>
<col10>...</col10>
</group2>
</col>
<col>
<col1>...</col1>
<col2>...</col2>
<col3>...</col3>
<col4>...</col4>
<col5>...</col5>
<col6>...</col6>
<group1>
<col7>...</col7>
<col8>...</col8>
</group1>
<group2>
<col9>...</col9>
<col10>...</col10>
</group2>
</col>
</all>
</root>
my solution is to use stylesheet in to_xml function like this:
df.to_xml("example.xml", root_name='all', row_name='col', encoding='utf-8', xml_declaration=True, pretty_print=True, index=False, stylesheet='example.xslt')
but i have no idea how to write example.xslt file and how to set to_xml function to get desired xml. I am looking for suggestions and examples of xslt that might work
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>
to get the output to have the proper headers and pretty printed. But the answer is spot on otherwise. Thanks. – Cravens