3

Actually, my workflow is, I'll get some data from backend python, and I have to use that data and replicate it into an HTML page or in a pdf format as per the user's wish. So I have created a python function in which the XML will be there and be saved automatically in our backend.

Here I'll provide my .py file where I wrote my code which generated XML code.

import xml.etree.ElementTree as xml

def GenerateXML(filename):

    root = xml.Element("customers")
    c1= xml.Element("customer")
    root.append(c1)
    type1= xml.SubElement(c1,"place")
    type1.text = "UK"

    Amount1 = xml.SubElement(c1,"Amount")
    Amount1.text="4500"

    tree=xml.ElementTree(root)
    with open(filename,"wb") as f:
        tree.write(f)

if __name__  == "__main__":
    GenerateXML("fast.xml")

The result for this code will generate a backend file named fast.xml, which contains

#fast.xml

<customers>
  <customer>
    <place>uk</place>
    <Amount>4500</Amount>
  </customer>
</customers>

Creating an XML file is done, but attaching an XSL file to the XML is the issue, can we do it with python, as we created .XML file

For example, I have another XML file, which has an XSL file for it:

XML file

<?xml-stylesheet type = "text/xsl" href = "demo1.xsl"?>
<class>
  <student>
     <firstname>Graham</firstname>
     <lastname>Bell</lastname>
     <nickname>Garry</nickname>
  </student>
  <student>
     <firstname>Albert</firstname>
     <lastname>Einstein</lastname>
     <nickname>Ally</nickname>
  </student>
  <student>
     <firstname>Thomas</firstname>
     <lastname>Edison</lastname>
     <nickname>Eddy</nickname>
  </student>
</class>

IN HTML it looks with a tabular form and with background colors. but how to do it with an automated XML file

can anyone provide me a solution for this?

Thanks in Advance

Tranbi
  • 11,407
  • 6
  • 16
  • 33
prithvi
  • 49
  • 1
  • 5
  • You might need to switch to mini dom to insert a processing instruction node for the `` pi. – Martin Honnen Apr 13 '21 at 10:13
  • Inserting a processing instruction before the root element is fairly easy with lxml: https://stackoverflow.com/q/61252951/407651 – mzjn Aug 10 '21 at 09:46
  • You could also do it by adding `f.write('\n'.encode("UTF-8"))` immediately before `tree.write(f)`. See https://stackoverflow.com/a/8868551/407651. – mzjn Aug 10 '21 at 10:01

0 Answers0