How to create XML,XSL files using python

Viewed 198

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

0 Answers
Related