|
Programming mode XSLT python.
|
|
|
|
|
Monday, 14 July 2008
|
HTML clipboard Introduction The purpose of this tutorial is to create an HTML file based on XML and XSL to be given. There are many XML processor in different languages. In java, found Xalan-J and Xalan-C + + exists for the C + +. But the Python language is very simple to learn and very powerful. It is object-oriented and we'll see a few lines that it is possible to achieve our year. We will discover that in our home and point by point. You must have notions of python and it is preferable that you know XML and XSL in order to better understand this exercise but not essential. You must also have the Python environment and 4suite which contains the library PyXml. The file python. from Ft.Lib.Uri import OsPathToUri from Ft.Xml import InputSource from Ft.Xml.Xslt import Processor import os.path "" Class allowing HTML conversion of an XML file through its XSL "" HtmlConverter class: def __init__ (self, xml, xsl): "" Manufacturer of HtmlConcerter. Takes two filenames. The first is the xml file and the second is the file xsl who allow the creation of html file. "" self.xmlFile = XML self.xslFile xsl = def toHtml (self, htmlFile): "" Write in an HTML file processing XML file. If True is returned, is that everything went well. If False is returned, errors have occurred. "" if (os.path.exists (self.xslFile) == False): print ( 'The file XSL n \' is not ') False Return if (os.path.exists (self.xmlFile) == False): print ( 'The XML file n \' is not ') False Return styuri = OsPathToUri (self.xslFile) srcuri = OsPathToUri (self.xmlFile) STY = InputSource.DefaultFactory.fromUri (styuri) SRC = InputSource.DefaultFactory.fromUri (srcuri) proc = Processor.Processor () proc.appendStylesheet (STY) proc.run result = (SRC) try: fileResult = open (htmlFile, "w") fileResult.write (result) fileResult.close () except IOError: False Return Return True if __name__ == "__main__": directory = "C: / Documents and Settings / jle / My Documents / SiteS / javascript / withxml /" xml = directory + "tutorial-xml-xsl-javascript.xml" xsl = directory + "tutorial-xml-xsl-javascript.xsl" html = directory + "tutorial-xml-xsl-javascript.html" htmlC = HtmlConverter (xml, xsl) print htmlC.toHtml (HTML)
Above, the complete file python that you can call HtmlConverter. We will not dévouvrir not différetes functions provided by the script. It will be possible thereafter to add new methods to deal with several xml files with a xsl, but I let you in this exercise. from Ft.Lib.Uri import OsPathToUri from Ft.Xml import InputSource from Ft.Xml.Xslt import Processor import os.path The first 4 lines import various modules and classes we need. "OsPathToUri" can transform a String on a path. "InputSource" we will build a file on the basis of string past. The "processor" is the class that will carry out the transformation of XML. HtmlConverter class: def __init__ (self, xml, xsl): self.xmlFile = XML self.xslFile xsl = It will then create a class with a manufacturer who will take the parameters of the XML file name and file name XSL. It must be the complete path for these files. def toHtml (self, htmlFile): if (os.path.exists (self.xslFile) == False): print ( 'The file XSL n \' is not ') False Return if (os.path.exists (self.xmlFile) == False): print ( 'The XML file n \' is not ') False Return styuri = OsPathToUri (self.xslFile) srcuri = OsPathToUri (self.xmlFile) STY = InputSource.DefaultFactory.fromUri (styuri) SRC = InputSource.DefaultFactory.fromUri (srcuri) proc = Processor.Processor () proc.appendStylesheet (STY) proc.run result = (SRC) try: fileResult = open (htmlFile, "w") fileResult.write (result) fileResult.close () except IOError: False Return Return True This method, which passes on the filename html to create, will achieve the transformation of XML file. You start by checking the files (XML and XSL) exist through: if (os.path.exists (self.xslFile) == False): print ( 'The file XSL n \' is not ') False Return if (os.path.exists (self.xmlFile) == False): print ( 'The XML file n \' is not ') False Return Then we will create files on the basis of their names: styuri = OsPathToUri (self.xslFile) srcuri = OsPathToUri (self.xmlFile) STY = InputSource.DefaultFactory.fromUri (styuri) SRC = InputSource.DefaultFactory.fromUri (srcuri) It creates the XSLT processor, it passes the XSL file to serve the transformation and we get the result in a variable "result". proc = Processor.Processor () proc.appendStylesheet (STY) proc.run result = (SRC) This result is then written in the HTML file. The following code is just the hand that will help launch the class. I create here filenames, I create the class and j'appel processing method. if __name__ == "__main__": directory = "to / my / directory /" xml = directory + "tutorial-xml-xsl-javascript.xml" xsl = directory + "tutorial-xml-xsl-javascript.xsl" html = directory + "tutorial-xml-xsl-javascript.html" htmlC = HtmlConverter (xml, xsl) print htmlC.toHtml (HTML) Conclusion Using XSLT python is fairly simple to implement. The result is more than satisfactory and speed of execution is the rendez-vous. Python is a language which requires tremendous focus. The websites are becoming more timely maintenance and creation through XML and XSL-Python. You can retrieve the file python and xml files, xsl javascript and making a right click and "Save as".
|