def _compile_xsl(XSLT_FILE, XSLT_COMPILED_FILE): """ compiling docbook stylesheet reference: http://155.210.85.193:8010/ccia/nodes/2005-03-18/compileXslt?xslt=/akara/akara.xslt """ from Ft.Xml.Xslt.Processor import Processor from Ft.Xml.Xslt import Stylesheet from Ft.Xml import InputSource from Ft.Lib import Uri # New docbook processor db_processor = Processor() # Docbook Stylesheet my_sheet_uri = Uri.OsPathToUri(XSLT_FILE, 1) sty_isrc = InputSource.DefaultFactory.fromUri(my_sheet_uri) # Append Stylesheet db_processor.appendStylesheet(sty_isrc) # Pickled stylesheet will be self.abs_db_compiled_xsl file db_root = db_processor.stylesheet.root fw = file(XSLT_COMPILED_FILE, 'wb') cPickle.dump(db_root, fw) # , protocol=2) fw.close()
def processDocumentNode( c ): '''this executes the stylesheet node against the current node''' try: if not styleNodeSelected( c ): return proc = Processor() stylenode = stylenodes[ c ] pos = c.p c.selectPosition( stylenode ) sIO = getString( c ) mdom1 = minidom.parseString( sIO ) sIO = str( mdom1.toxml() ) hstring = str( stylenode.h ) if hstring == "": hstring = "no headline" stylesource = InputSource.DefaultFactory.fromString( sIO, uri = hstring) proc.appendStylesheet( stylesource ) c.selectPosition( pos ) xmlnode = pos.v xIO = getString( c ) mdom2 = minidom.parseString( xIO ) xIO = str( mdom2.toxml()) xhead = str( xmlnode.headString ) if xhead == "": xhead = "no headline" xmlsource = InputSource.DefaultFactory.fromString( xIO, uri = xhead ) result = proc.run( xmlsource ) nhline = "xsl:transform of " + str( xmlnode.headString ) p2 = pos.insertAfter() # tnode ) p2.setBodyString(result) p2.setHeadString(nhline) c.redraw() except Exception as x: g.es( 'exception ' + str( x )) c.redraw()
def processDocumentNode( c ): '''this executes the stylesheet node against the current node''' try: if not styleNodeSelected( c ): return proc = Processor() stylenode = stylenodes[ c ] pos = c.p c.selectPosition( stylenode ) sIO = getString( c ) mdom1 = minidom.parseString( sIO ) sIO = str( mdom1.toxml() ) hstring = str( stylenode.h ) if hstring == "": hstring = "no headline" stylesource = InputSource.DefaultFactory.fromString( sIO, uri = hstring) proc.appendStylesheet( stylesource ) c.selectPosition( pos ) xmlnode = pos.v xIO = getString( c ) mdom2 = minidom.parseString( xIO ) xIO = str( mdom2.toxml()) xhead = str( xmlnode.headString ) if xhead == "": xhead = "no headline" xmlsource = InputSource.DefaultFactory.fromString( xIO, uri = xhead ) result = proc.run( xmlsource ) nhline = "xsl:transform of " + str( xmlnode.headString ) p2 = pos.insertAfter() # tnode ) p2.setBodyString(result) p2.setHeadString(nhline) c.redraw() except Exception as x: g.es( 'exception ' + str( x )) c.redraw()
def applyXslt(self, xslStylesheet, topLevelParams=None, extFunctionMap=None, baseUri='file:', styleSheetCache=None): extFunctionMap = extFunctionMap or {} from Ft.Xml.Xslt.Processor import Processor processor = Processor() if styleSheetCache: styleSheet = styleSheetCache.getValue(xslStylesheet, baseUri) processor.appendStylesheetInstance(styleSheet, baseUri) else: processor.appendStylesheet( InputSource.DefaultFactory.fromString( xslStylesheet, baseUri)) #todo: fix baseUri for (k, v) in extFunctionMap.items(): namespace, localName = k processor.registerExtensionFunction(namespace, localName, v) return processor.runNode(self.dom, None, 0, topLevelParams), processor.stylesheet
def _compile_xsl(XSLT_FILE, XSLT_COMPILED_FILE): """ compiling docbook stylesheet reference: http://155.210.85.193:8010/ccia/nodes/2005-03-18/compileXslt?xslt=/akara/akara.xslt """ from Ft.Xml.Xslt.Processor import Processor from Ft.Xml.Xslt import Stylesheet from Ft.Xml import InputSource from Ft.Lib import Uri # New docbook processor db_processor = Processor() # Docbook Stylesheet my_sheet_uri = Uri.OsPathToUri(XSLT_FILE, 1) sty_isrc = InputSource.DefaultFactory.fromUri(my_sheet_uri) # Append Stylesheet db_processor.appendStylesheet(sty_isrc) # Pickled stylesheet will be self.abs_db_compiled_xsl file db_root = db_processor.stylesheet.root fw = file(XSLT_COMPILED_FILE, 'wb') cPickle.dump(db_root, fw) # , protocol=2) fw.close()
def __init__(self, config, parent): C3Object.__init__(self, config, parent) xfrPath = self.get_path(None, "xsltPath") dfp = self.get_path(None, "defaultPath") path = os.path.join(dfp, xfrPath) xfr = InputSource.DefaultFactory.fromStream(file(path), "file://" + dfp) processor = Processor() processor.appendStylesheet(xfr) self.processor = processor
def transform(xml, xsl): """ A simple wrapper for 4XSLT. """ from Ft.Xml.Xslt.Processor import Processor from Ft.Xml.InputSource import DefaultFactory proc = Processor() xslObj = DefaultFactory.fromString(xsl, "http://rantelope.com/") proc.appendStylesheet(xslObj) xmlObj = DefaultFactory.fromString(xml) return proc.run(xmlObj)
def build_dom_and_apply_style_sheet(self, xsl, file): #doc = xml.dom.minidom.Document() doc = implementation.createRootNode('file:///article.xml') self.app.ann_frame.build_xml(doc) xsltproc = Processor() xsltproc.appendStylesheet(DefaultFactory.fromUri(Uri.OsPathToUri(xsl))) output = xsltproc.runNode(doc, 'file:///article.xml') outFile = open(file,'w') outFile.write(output) outFile.close()
def build_dom_and_apply_style_sheet(self, xsl, file): #doc = xml.dom.minidom.Document() doc = implementation.createRootNode('file:///article.xml') self.app.ann_frame.build_xml(doc) xsltproc = Processor() xsltproc.appendStylesheet(DefaultFactory.fromUri(Uri.OsPathToUri(xsl))) output = xsltproc.runNode(doc, 'file:///article.xml') outFile = open(file, 'w') outFile.write(output) outFile.close()
def GET(self, query_string): try: xml = urllib2.urlopen('http://localhost:8080/marcxml/%s' % query_string).read() xml = InputSource.DefaultFactory.fromString(xml) xslt = InputSource.DefaultFactory.fromUri(XSLT_URIS['mods']) processor = Processor() processor.appendStylesheet(xslt) web.header('Content-Type', 'application/xml') print processor.run(xml) except urllib2.HTTPError, e: if e.code == 404: web.notfound() else: raise
def Test(tester): # We don't use test_harness.XsltTest and friends because they hide away # the API details we're testing in this module. # See http://bugs.4suite.org/641693 tester.startGroup("Test multiple stylesheet invokation") xp = Processor() xp.appendStylesheet( DefaultFactory.fromString(stylesheet_string, uri="data:ss")) result1 = xp.run(DefaultFactory.fromString(source_string, uri="data:src1")) result2 = xp.run(DefaultFactory.fromString(source_string, uri="data:src2")) tester.compare(result1, EXPECTED_1) tester.compare(result2, EXPECTED_1) tester.groupDone() return
def __transform_4suite(self, file, xsl_file, output, params): import codecs from Ft.Xml import InputSource from Ft.Xml.Xslt.Processor import Processor document = InputSource.DefaultFactory.fromUri(file) stylesheet = InputSource.DefaultFactory.fromUri(xsl_file) # there's also a fromString() method processor = Processor() processor.appendStylesheet(stylesheet) result = processor.run(document, topLevelParams=params) (utf8_encode, utf8_decode, utf8_reader, utf8_writer) = codecs.lookup("utf-8") write_obj = utf8_writer(open(output, 'w')) write_obj.write(result) write_obj.close()
def Test(tester): # We don't use test_harness.XsltTest and friends because they hide away # the API details we're testing in this module. # See http://bugs.4suite.org/641693 tester.startGroup("Test multiple stylesheet invokation") xp = Processor() xp.appendStylesheet(DefaultFactory.fromString(stylesheet_string, uri="data:ss")) result1 = xp.run(DefaultFactory.fromString(source_string, uri="data:src1")) result2 = xp.run(DefaultFactory.fromString(source_string, uri="data:src2")) tester.compare(result1, EXPECTED_1) tester.compare(result2, EXPECTED_1) tester.groupDone() return
import Ft from Ft.Xml.InputSource import DefaultFactory import trace,sys from xml.xpath import Context, Util ancienset = Ft.Xml.Xslt.XsltContext.XsltContext.set def mongetattr(ins,attname): print "Aie!" return None def monset(ins,d): print "On défini un truc sur le contexte" ancienset(ins,d) def monsetattr(ins,attname,val): print "HeHo !" print Ft.Xml.Xslt.XsltContext.XsltContext.__dict__.keys() Ft.Xml.Xslt.XsltContext.XsltContext.__dict__['set'] = monset Ft.Xml.Xslt.XsltContext.XsltContext.__dict__['addDocument'] = monsetattr Ft.Xml.Xslt.XsltContext.XsltContext.__dict__['__setattr__'] = monsetattr print Ft.Xml.Xslt.XsltContext.XsltContext.__dict__.keys() xsltproc = Processor() xsltproc.appendStylesheet(DefaultFactory.fromUri("file:///home/t0rt00se/Travail/SILR3/pTrans/testset/persons_to_xhtml_list.xsl")) html = xsltproc.run(DefaultFactory.fromUri("file:///home/t0rt00se/Travail/SILR3/pTrans/testset/persons.xml")) print html
if options.format is not None: try: from Ft.Lib import Uri from Ft.Xml.InputSource import DefaultFactory from Ft.Xml.Xslt.Processor import Processor except: print "Couldn't import modules to generate HTML." processor = Processor() if options.format == "html": xform = DefaultFactory.fromString(html_xform, "uri") elif options.format == "text": xform = DefaultFactory.fromString(text_xform, "uri") processor.appendStylesheet(xform) try: ro = processor.run(DefaultFactory.fromString(output, "uri")) except Exception, e: print "Failed to generate HTML. (%s)" % e ro = None if options.outputFile is not None and ro is not None: f = file(options.outputFile, 'w') f.write(ro) f.close() else: if ro is not None: print ro else:
def mongetattr(ins, attname): print "Aie!" return None def monset(ins, d): print "On défini un truc sur le contexte" ancienset(ins, d) def monsetattr(ins, attname, val): print "HeHo !" print Ft.Xml.Xslt.XsltContext.XsltContext.__dict__.keys() Ft.Xml.Xslt.XsltContext.XsltContext.__dict__['set'] = monset Ft.Xml.Xslt.XsltContext.XsltContext.__dict__['addDocument'] = monsetattr Ft.Xml.Xslt.XsltContext.XsltContext.__dict__['__setattr__'] = monsetattr print Ft.Xml.Xslt.XsltContext.XsltContext.__dict__.keys() xsltproc = Processor() xsltproc.appendStylesheet( DefaultFactory.fromUri( "file:///home/t0rt00se/Travail/SILR3/pTrans/testset/persons_to_xhtml_list.xsl" )) html = xsltproc.run( DefaultFactory.fromUri( "file:///home/t0rt00se/Travail/SILR3/pTrans/testset/persons.xml")) print html
def error(self, message): print "B³±d: " + message def fatal(self, message): print "B³±d krytyczny: " + message # Otwórz pliki HTML i XSTL jako strumienie. html = open('mojblog.html') xsl = open('HTMLnaRSS.xsl') # Przetwórz strumienie i utwórz z nich Ÿród³a wejœciowe. parsedxml = InputSource.DefaultFactory.fromStream(html, "mojblog.html") parsedxsl = InputSource.DefaultFactory.fromStream(xsl, "HTMLnaRSS.xsl") # Utwórz nowy procesor, do³¹cz do niego arkusz stylu a nastêpnie przekszta³æ XML. processor = Processor() processor.appendStylesheet(parsedxsl) HTML = processor.run(parsedxml) # Zapisz wyjœciowy dokument RSS w pliku. output = open("kanalRSS.xml", 'w') output.write(HTML) output.close # Dokonaj walidacji kana³u RSS. parser = xmlval.XMLValidator() parser.set_error_handler(docErrorHandler(parser)) parser.parse_resource("kanalRSS.xml")
if options.format is not None: try: from Ft.Lib import Uri from Ft.Xml.InputSource import DefaultFactory from Ft.Xml.Xslt.Processor import Processor except: print "Couldn't import modules to generate HTML." processor = Processor() if options.format == "html": xform = DefaultFactory.fromString(html_xform, "uri") elif options.format == "text": xform = DefaultFactory.fromString(text_xform, "uri") processor.appendStylesheet(xform) try: ro = processor.run(DefaultFactory.fromString(output, "uri")) except Exception, e: print "Failed to generate HTML. (%s)" % e ro = None if options.outputFile is not None and ro is not None: f = file(options.outputFile, 'w') f.write(ro) f.close() else: if ro is not None: print ro else: