def convert_content(self, req, input_type, source, output_type): #extract all data resources datadir = resource_filename(__name__, 'data') html = wiki_to_html(source, self.env, req) options = dict(output_xhtml=1, add_xml_decl=1, indent=1, tidy_mark=0, input_encoding='utf8', output_encoding='utf8', doctype='auto', wrap=0, char_encoding='utf8') xhtml = parseString(html.encode("utf-8"), **options) xhtml2dbXsl = u"""<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:import href=\"file:///""" + urllib.pathname2url(resource_filename(__name__, 'data/html2db/html2db.xsl')) + """\" /> <xsl:output method="xml" indent="no" encoding="utf-8"/> <xsl:param name="document-root" select="'chapter'"/> </xsl:stylesheet> """ normalizedHeadingsXsl = u"""<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:import href=\"file:///""" + urllib.pathname2url(resource_filename(__name__, 'data/headingsNormalizer/headingsNormalizer.xsl')) + """\" /> <xsl:output method="xml" indent="no" encoding="utf-8"/> <xsl:param name="defaultTopHeading" select="'""" + req.path_info[6:] + """'"/> </xsl:stylesheet> """ xhtml_xmldoc = libxml2.parseDoc(str(xhtml)) normalizedHeadingsXsl_xmldoc = libxml2.parseDoc(normalizedHeadingsXsl) normalizedHeadingsXsl_xsldoc = libxslt.parseStylesheetDoc(normalizedHeadingsXsl_xmldoc) xhtml2_xmldoc = normalizedHeadingsXsl_xsldoc.applyStylesheet(xhtml_xmldoc, None) nhstring = normalizedHeadingsXsl_xsldoc.saveResultToString(xhtml2_xmldoc) xhtml2dbXsl_xmldoc = libxml2.parseDoc(xhtml2dbXsl) xhtml2dbXsl_xsldoc = libxslt.parseStylesheetDoc(xhtml2dbXsl_xmldoc) docbook_xmldoc = xhtml2dbXsl_xsldoc.applyStylesheet(xhtml2_xmldoc, None) dbstring = xhtml2dbXsl_xsldoc.saveResultToString(docbook_xmldoc) xhtml_xmldoc.freeDoc() normalizedHeadingsXsl_xsldoc.freeStylesheet() xhtml2dbXsl_xsldoc.freeStylesheet() xhtml2_xmldoc.freeDoc() docbook_xmldoc.freeDoc() return (dbstring, 'text/plain') #application/docbook+xml
def xslt_convert( xml, xsl ): if not _have_xslt: Error( _err_msg ) libxml2.lineNumbersDefault( 1 ) libxml2.substituteEntitiesDefault( 1 ) try: styledoc = libxml2.parseFile( xsl ) except libxml2.parserError: Error("Cannot parse XSL stylesheet: '%s'" % xsl ) style = libxslt.parseStylesheetDoc( styledoc ) try: doc = libxml2.parseFile( xml ) except libxml2.parserError: Error("Unable to parse XML document: '%s'" % xml ) result = style.applyStylesheet( doc, None ) s = style.saveResultToString( result ) style.freeStylesheet() doc.freeDoc() result.freeDoc() return s
def parseConf(self): libxml2.initParser() doc = libxml2.parseFile(self.conf["main"]["dataPath"]+"/PostGIS/"+self.dbfile+".xml") styledoc = libxml2.parseFile(self.conf["main"]["dataPath"]+"/PostGIS/conn.xsl") style = libxslt.parseStylesheetDoc(styledoc) res=style.applyStylesheet(doc, None) self.db_string=res.content.replace("PG: ","")
def __transform_xmllint(self, file, xsl_file, output, params = {}): import libxml2 import libxslt new_params = {} keys = params.keys() for key in keys: new_params[key] = '"%s"' % params[key] params = new_params try: xml_doc = file # parse stylesheet styledoc = libxml2.parseFile(xsl_file) style = libxslt.parseStylesheetDoc(styledoc) # parse doc doc = libxml2.parseFile(xml_doc) result = style.applyStylesheet(doc, params) style.saveResultToFilename(output, result, 0) style.freeStylesheet() doc.freeDoc() result.freeDoc() except libxml2.parserError: return 1, '' return 0, ''
def sxw2rml(sxw_file, xsl, output='.', save_pict=False): import libxslt import libxml2 tool = PyOpenOffice(output, save_pict = save_pict) res = tool.unpackNormalize(sxw_file) styledoc = libxml2.parseDoc(xsl) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseMemory(res,len(res)) result = style.applyStylesheet(doc, None) root = result.xpathEval("/document/stylesheet") if root: root=root[0] images = libxml2.newNode("images") for img in tool.images: node = libxml2.newNode('image') node.setProp('name', img) node.setContent( base64.encodestring(tool.images[img])) images.addChild(node) root.addNextSibling(images) try: xml = style.saveResultToString(result) return xml except: return result
def _translate_file_to(xslFilename, inFile, outFile, params=None): # parse the stylesheet file styledoc = libxml2.parseFile(xslFilename) # setup the stylesheet instance style = libxslt.parseStylesheetDoc(styledoc) try: # parse the inline generated XML file doc = libxml2.parseFile(inFile) # apply the stylesheet instance to the document instance result = style.applyStylesheet(doc, params) style.saveResultToFilename(outFile, result, 0) success = 1 # free instances result.freeDoc() style.freeStylesheet() doc.freeDoc() except: print "Can't parse: %s, skipping" % inFile success = 0 return success
def get_kbt_items(taxonomyfilename, templatefilename, searchwith=""): """ Get items from taxonomy file using a templatefile. If searchwith is defined, return only items that match with it. @param taxonomyfilename: full path+name of the RDF file @param templatefile: full path+name of the XSLT file @param searchwith: a term to search with """ import libxml2 import libxslt styledoc = libxml2.parseFile(templatefilename) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseFile(taxonomyfilename) result = style.applyStylesheet(doc, None) strres = style.saveResultToString(result) style.freeStylesheet() doc.freeDoc() result.freeDoc() ritems = [] if len(strres) == 0: return [] else: lines = strres.split("\n") for line in lines: if searchwith: if line.count(searchwith) > 0: ritems.append(line) else: if len(line) > 0: ritems.append(line) return ritems
def __init__(self): # set up the W3C schema parser self.error_handler = ErrorHandler() filepath = os.path.join(os.path.dirname(__file__), 'data', 'isotc211', 'gmx', 'gmx.xsd') ctxt_parser = libxml2.schemaNewParserCtxt(filepath) self.isotc211_schema = ctxt_parser.schemaParse() # set up the ISO TS 19139 A1 constraints schematron filepath = os.path.join(os.path.dirname(__file__), 'data', 'ISOTS19139A1Constraints_v1.3.xsl') doc = libxml2.parseFile(filepath) self.isots19139_xslt = libxslt.parseStylesheetDoc(doc) # set up the MEDIN Metadata Profile schematron filepath = os.path.join(os.path.dirname(__file__), 'data', 'MedinMetadataProfile_v1.8.xsl') doc = libxml2.parseFile(filepath) self.medin_xslt = libxslt.parseStylesheetDoc(doc)
def parse_xml(self): try: import libxml2 import libxslt import urllib2 xslt_path = os.path.join(os.path.dirname(__file__), "") xslt_path = "%s/xslt/%s" % (xslt_path, XML_XSD_CFDI_VERSION) xslt_path_file = "%s/%s" % (xslt_path, XML_XSLT_CDF_NAME) xslt_complements_path = "%s/complementos" % xslt_path response = open(xslt_path_file) xslt = response.read() xslt = xslt.replace('{{XSLT_COMPLEMENTS_PATH}}', xslt_complements_path) styledoc = libxml2.parseMemory(xslt, len(xslt)) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseMemory(self.xml_invoice, len(self.xml_invoice)) result = style.applyStylesheet(doc, None) self.original_string = str(result) self.original_string = self.original_string.replace( '<?xml version="1.0" encoding="UTF-8"?>\n', '') self.original_string = self.original_string.replace('\n', '') self.original_string = self.original_string.replace('&', '&') self.original_string = self.original_string.replace('"', '"') self.original_string = self.original_string.replace('<', '<') self.original_string = self.original_string.replace('>', '>') self.original_string = self.original_string.replace(''', '´') self.original_string = self.original_string.strip() print self.original_string return self.original_string except Exception, e: pprint.pprint(e)
def parseConf(self): libxml2.initParser() doc = libxml2.parseFile(self.conf["main"]["dataPath"] + "/PostGIS/" + self.dbfile + ".xml") styledoc = libxml2.parseFile(self.conf["main"]["dataPath"] + "/PostGIS/conn.xsl") style = libxslt.parseStylesheetDoc(styledoc) res = style.applyStylesheet(doc, None) self.db_string = res.content.replace("PG: ", "")
def _transformFilesXslt(transformFile, srcDir, destDir, outFile, fileList): """ Run the list of files through an XSLT transform """ print "Running XSLT processor using ", transformFile if not os.path.exists(destDir): os.mkdir(destDir) for file in fileList: srcFile = srcDir + os.sep + file destFile = destDir + os.sep + file destFile = os.path.join(os.path.dirname(destFile), outFile) try: os.makedirs(os.path.dirname(destFile)) except Exception, e: pass if os.path.exists(srcFile): print ("Transforming " + srcFile) styledoc = libxml2.parseFile(transformFile) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseFile(srcFile) result = style.applyStylesheet(doc, None) style.saveResultToFilename(destFile, result, 0) style.freeStylesheet() doc.freeDoc() result.freeDoc()
def apply_processor(self, type, doc, output_xpath_filter=None): processor = self.processors[type] # XXX TO CLEAN UP filepath = os.path.join(sfatables_config, 'processors', processor) # XXX styledoc = libxml2.parseFile(filepath) style = libxslt.parseStylesheetDoc(styledoc) result = style.applyStylesheet(doc, None) if (output_xpath_filter): p = result.xpathNewContext() xpath_result = p.xpathEval(output_xpath_filter) if (xpath_result == []): raise Exception("Could not apply processor %s."%processor) stylesheet_result = xpath_result p.xpathFreeContext() else: stylesheet_result = result #style.saveResultToString(result) style.freeStylesheet() #doc.freeDoc() #result.freeDoc() return stylesheet_result
def Write(self, filename, xslt = None): if self.status != 2 and self.status != 3: raise RuntimeError, "XMLOut: XML document is not closed" if xslt == None: # If no XSLT template is give, write raw XML self.xmldoc.saveFormatFileEnc(filename, self.encoding, 1) return else: # Load XSLT file and prepare the XSLT parser xsltdoc = libxml2.parseFile(xslt) parser = libxslt.parseStylesheetDoc(xsltdoc) # imitate libxml2's filename interpretation if filename != "-": dstfile = codecs.open(filename, "w", encoding=self.encoding) else: dstfile = sys.stdout # # Parse XML+XSLT and write the result to file # resdoc = parser.applyStylesheet(self.xmldoc, None) # Decode the result string according to the charset declared in the XSLT file xsltres = parser.saveResultToString(resdoc).decode(parser.encoding()) # Write the file with the requested output encoding dstfile.write(xsltres.encode(self.encoding)) if dstfile != sys.stdout: dstfile.close() # Clean up resdoc.freeDoc() xsltdoc.freeDoc()
def get_kbt_items(taxonomyfilename, templatefilename, searchwith=""): """ Get items from taxonomy file using a templatefile. If searchwith is defined, return only items that match with it. @param taxonomyfilename: full path+name of the RDF file @param templatefile: full path+name of the XSLT file @param searchwith: a term to search with """ styledoc = libxml2.parseFile(templatefilename) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseFile(taxonomyfilename) result = style.applyStylesheet(doc, None) strres = style.saveResultToString(result) style.freeStylesheet() doc.freeDoc() result.freeDoc() ritems = [] if len(strres) == 0: return [] else: lines = strres.split("\n") for line in lines: if searchwith: if line.count(searchwith) > 0: ritems.append(line) else: if len(line) > 0: ritems.append(line) return ritems
def merge_bibcodes(bibcodes, print_adsxml=False, print_marcxml=False, write_xml_to_disk=False): """ Returns a merged version of the record identified by bibcode. """ # Extract the record from ADS. records = ADSRecords('full', 'XML') for bibcode in bibcodes: records.addCompleteRecord(bibcode) ads_xml_obj = records.export() if print_adsxml: print ads_xml_obj.serialize('UTF-8') if write_xml_to_disk: with open('/tmp/adsxml.xml', 'w') as f: f.write(ads_xml_obj.serialize('UTF-8')) # Convert to MarcXML. stylesheet = libxslt.parseStylesheetDoc(libxml2.parseFile(XSLT)) xml_object = stylesheet.applyStylesheet(ads_xml_obj, None) if print_marcxml: print xml_object.serialize('UTF-8') if write_xml_to_disk: with open('/tmp/marcxml.xml', 'w') as f: f.write(xml_object.serialize('UTF-8')) merged_records, bibcodes_with_problems = merge_records_xml(xml_object) return merged_records
def run_xslt(xml_file, xslt_file, out_file=None): """ Transforms the specified XML file using the specified XSLT stylesheet. If optional out_file specified, writes output to file; otherwise, returns styled metadata as a string. Examples: >>> run_xslt('/usr/local/apache/htdocs/pacioos/metadata/iso/NS01agg.xml', '/usr/local/apache/htdocs/pacioos/metadata/xslt/iso2fgdc.xsl', '/usr/local/apache/htdocs/pacioos/metadata/fgdc/NS01agg.xml') >>> metadata_styled_string = run_xslt('/usr/local/apache/htdocs/pacioos/metadata/iso/NS01agg.xml', '/usr/local/apache/htdocs/pacioos/metadata/xslt/iso2fgdc.xsl') """ import libxml2 import libxslt # Render the XML into the specified style using its XSLT stylesheet: # NOTE: The return object from the parser is an XML DOM object data # structure (DOM = Document Object Model): metadata_xml_dom = libxml2.parseFile(xml_file) xsl_dom = libxml2.parseFile(xslt_file) stylesheet = libxslt.parseStylesheetDoc(xsl_dom) metadata_styled_dom = stylesheet.applyStylesheet(metadata_xml_dom, None) if out_file: stylesheet.saveResultToFilename(out_file, metadata_styled_dom, 0) else: metadata_styled_string = stylesheet.saveResultToString(metadata_styled_dom) return metadata_styled_string
def _translate_string_to(xslFilename, inString, outFile, params=None): # do the transformation # parse the stylesheet file styledoc = libxml2.parseFile(xslFilename) # setup the stylesheet instance style = libxslt.parseStylesheetDoc(styledoc) # parse the inline generated XML file doc = libxml2.parseDoc(inString) # apply the stylesheet instance to the document instance result = style.applyStylesheet(doc, params) style.saveResultToFilename(outFile, result, 0) # use to dump directly to a string, problem is that it insists on # outputting an XML declaration "<?xml ...?>", can't seem to # suppress this # outString = result.serialize() # free instances result.freeDoc() style.freeStylesheet() doc.freeDoc()
def processRawData(xsl_uri, features, bbox): """ Downloads the data from XAPI and turns it into a Python object. """ # Download data to temporary file xapi_uri = "http://open.mapquestapi.com/xapi/api/0.6/*[%s][bbox=%s]" % (features, bbox) if ('-v' in sys.argv): print " : Downloading %s" % (xapi_uri) urllib.urlretrieve(xapi_uri,'temp.xml') # Translate XML to CSV (easier to then read into py object) if ('-v' in sys.argv): print " : Processing data..." osmdoc = libxml2.parseFile('temp.xml') styledoc = libxml2.parseFile(xsl_uri) style = libxslt.parseStylesheetDoc(styledoc) result = style.applyStylesheet(osmdoc, None) style.saveResultToFilename('temp.csv', result, 0) # Encode HTML elements f = open('temp.csv', 'r') safe_content = escape(f.read()) f = open('temp.csv', 'w') f.write(safe_content) # Read CSV file into dict pdata = csv.DictReader(open('temp.csv', 'rb'), delimiter=' ') return pdata
def main(): libxml2.lineNumbersDefault(1) libxml2.substituteEntitiesDefault(1) # URL to fetch the OSM data from map_source_data_url="http://xapi.openstreetmap.org/api/0.6/*[power=generator][bbox=-0.51,51.20,0.35,51.80]" # Filename for OSM map data xml_filename = "generators.xml" # Filename for XSLT to extract POIs xsl_filename = "trans_csv_generators.xsl" # Download the map.osm file from the net, if we don't already have one. #if os.path.isfile(xml_filename): #print "Not downloading map data. '%s' already exists."%xml_filename #else: #print "Downloading OSM data." #print "'%s' -> '%s'"%(map_source_data_url,xml_filename) #urllib.urlretrieve(map_source_data_url,xml_filename) # Read the XML into memory. We will use it many times. osmdoc = libxml2.parseFile(xml_filename) # Read the XSLT styledoc = libxml2.parseFile(xsl_filename) style = libxslt.parseStylesheetDoc(styledoc) # Extract POIs to layer text files result = style.applyStylesheet(osmdoc, {"key":"power", "value":"generator"}) style.saveResultToFilename("temp.csv", result, 0)
def applyXSLT(self, fileName): sourceXMLFile = fileName sourceDoc = libxml2.parseFile(sourceXMLFile) styleDoc = libxml2.parseFile("base.xsl") style = libxslt.parseStylesheetDoc(styleDoc) result = style.applyStylesheet(sourceDoc, None) print result
def apply_processor(self, type, doc, output_xpath_filter=None): processor = self.processors[type] # XXX TO CLEAN UP filepath = os.path.join(sfatables_config, 'processors', processor) # XXX styledoc = libxml2.parseFile(filepath) style = libxslt.parseStylesheetDoc(styledoc) result = style.applyStylesheet(doc, None) if (output_xpath_filter): p = result.xpathNewContext() xpath_result = p.xpathEval(output_xpath_filter) if (xpath_result == []): raise Exception("Could not apply processor %s." % processor) stylesheet_result = xpath_result p.xpathFreeContext() else: stylesheet_result = result #style.saveResultToString(result) style.freeStylesheet() #doc.freeDoc() #result.freeDoc() return stylesheet_result
def upgrade(self, version_attribute, file_version, software_version, upgrades_repository): if upgrades_repository and os.path.exists(upgrades_repository): files = map(lambda f: upgrades_repository + "/" + f, os.listdir(upgrades_repository)) files = filter(lambda f: isfile(f) and ".xsl" in f, files) for upgradefile in file_version.upgradeChain( software_version, files): log.inform("upgrade: '%s' with '%s'", self.path, upgradefile) styledoc = libxml2.parseFile(upgradefile) style = libxslt.parseStylesheetDoc(styledoc) result = style.applyStylesheet(self.doc, None) if not self.upgrade_dry_run: style.saveResultToFilename(self.path, result, compression=0) result.freeDoc() # apparently deallocated by freeStylesheet # styledoc.freeDoc() style.freeStylesheet() if not self.upgrade_dry_run: self.reload() else: log.inform("upgrade: '%s' is not a directory, ignored", upgrades_repository) if not self.upgrade_dry_run: self.headerSet("/child::*/@" + version_attribute, str(software_version)) self.save()
def test(conf, inputs, outputs): libxml2.initParser() xcontent = ( "<connection><dbname>" + inputs["dbname"]["value"] + "</dbname><user>" + inputs["user"]["value"] + "</user><password>" + inputs["password"]["value"] + "</password><host>" + inputs["host"]["value"] + "</host><port>" + inputs["port"]["value"] + "</port></connection>" ) doc = libxml2.parseMemory(xcontent, len(xcontent)) styledoc = libxml2.parseFile(conf["main"]["dataPath"] + "/" + inputs["type"]["value"] + "/conn.xsl") # print >> sys.stderr,conf["main"]["dataPath"]+"/"+inputs["type"]["value"]+"/conn.xsl" style = libxslt.parseStylesheetDoc(styledoc) result = style.applyStylesheet(doc, None) # print >> sys.stderr,"("+result.content+")" ds = osgeo.ogr.Open(result.content) if ds is None: conf["lenv"]["message"] = zoo._("Unable to connect to ") + inputs["name"]["value"] return 4 else: outputs["Result"]["value"] = zoo._("Connection to ") + inputs["name"]["value"] + zoo._(" successfull") ds = None return 3
def generate_exam(exam_fname, exam_part, answers): # FIXME: comprobar que existen estos campos styledoc = libxml2.parseFile(os.path.join(XSL_DIR, 'exam_gen.xsl')) style = libxslt.parseStylesheetDoc(styledoc) params = dict( rootdir = '"' + os.getcwd() + '/"', part = '"%d"' % exam_part) if answers: params['answers'] = '"1"' try: doc = libxml2.parseFile(exam_fname) except libxml2.parserError as e: logging.error("parsing file '%s'" % e) sys.exit(2) result = style.applyStylesheet(doc, params) xmldoc = style.saveResultToString(result) style.freeStylesheet() doc.freeDoc() result.freeDoc() # FIXME: comprobar que la transformación fue correcta y generó el # fichero 'target' return xmldoc
def _xml_company_text(code, name): company = CompanyPage.objects.get(name=name) language = LanguageChoice.objects.get(code=code) pref = LanguagePref.objects.get(company=company, language=language) t = loader.get_template("xslt/company.xslt") c = Context({}) xslt = libxslt.parseStylesheetDoc(libxml2.parseDoc(t.render(c))) doc = "<%s>%s</%s>" % ("company", pref.description, "company") doc = libxml2.parseDoc(doc.encode("utf-8")) res = libxslt.stylesheet.applyStylesheet(xslt, doc, {}) content = res.content """ I don't know how to remove the stupid declaration yet, and I need this up and running today. Figure it out later and get rid of this """ p = re.compile('<\?xml version="1.0"\?>') content = p.sub("", content) info = dict() info['title'] = pref.title info['description'] = content t = loader.get_template('company/content.xml') c = Context({'language':info,}) return t.render(c)
def processRawData(xapi_uri, xsl_uri, features): """ Downloads the data from XAPI and turns it into a Python object. """ # Download data to temporary file and read the XML/XSL into memory if ('-v' in sys.argv): print "Downloading %s" % (xapi_uri) urllib.urlretrieve(xapi_uri,'temp.xml') osmdoc = libxml2.parseFile('temp.xml') styledoc = libxml2.parseFile(xsl_uri) style = libxslt.parseStylesheetDoc(styledoc) # Translate XML to CSV (easier to then read into py object) if ('-v' in sys.argv): print "Processing data..." for key,value in features.iteritems(): result = style.applyStylesheet(osmdoc,\ { "key":"'%s'"%key, "value":"'%s'"%value }) style.saveResultToFilename('temp.csv', result, 0) # Encode HTML elements f = open('temp.csv', 'r') safe_content = escape(f.read()) f = open('temp.csv', 'w') f.write(safe_content.encode('utf-8')) # Read CSV file into dict csv_file = open('temp.csv', 'rb') temp_data = csv_file.read() temp_data_unicoded = unicode(temp_data, 'utf-8') pdata = csv.DictReader(csv_file, delimiter=' ') for row in pdata: print row exit(42) return pdata
def xsl_transform(content, bDownloadImages): # 1 strTidiedHtml = tidy_and_premail(content) # 2 Settings for libxml2 for transforming XHTML entities to valid XML libxml2.loadCatalog(XHTML_ENTITIES) libxml2.lineNumbersDefault(1) libxml2.substituteEntitiesDefault(1) # 3 First XSLT transformation styleDoc1 = libxml2.parseFile(GDOCS2CNXML_XSL1) style1 = libxslt.parseStylesheetDoc(styleDoc1) # doc1 = libxml2.parseFile(afile)) doc1 = libxml2.parseDoc(strTidiedHtml) result1 = style1.applyStylesheet(doc1, None) #style1.saveResultToFilename(os.path.join('output', docFilename + '_meta.xml'), result1, 1) strResult1 = style1.saveResultToString(result1) style1.freeStylesheet() doc1.freeDoc() result1.freeDoc() # Parse XML with etree from lxml for TeX2MathML and image download etreeXml = etree.fromstring(strResult1) # 4 Convert TeX to MathML with Blahtex etreeXml = tex2mathml(etreeXml) # 5 Optional: Download Google Docs Images imageObjects = {} if bDownloadImages: etreeXml, imageObjects = downloadImages(etreeXml) # Convert etree back to string strXml = etree.tostring(etreeXml) # pretty_print=True) # 6 Second transformation styleDoc2 = libxml2.parseFile(GDOCS2CNXML_XSL2) style2 = libxslt.parseStylesheetDoc(styleDoc2) doc2 = libxml2.parseDoc(strXml) result2 = style2.applyStylesheet(doc2, None) #style2.saveResultToFilename('tempresult.xml', result2, 0) # just for debugging strResult2 = style2.saveResultToString(result2) style2.freeStylesheet() doc2.freeDoc() result2.freeDoc() return strResult2, imageObjects
def __xml_scan(node, env, path, arg): """ Simple XML file scanner, detecting local images and XIncludes as implicit dependencies. """ # Does the node exist yet? if not os.path.isfile(str(node)): return [] if env.get('DOCBOOK_SCANENT', ''): # Use simple pattern matching for system entities..., no support # for recursion yet. contents = node.get_text_contents() return sentity_re.findall(contents) xsl_file = os.path.join(scriptpath, 'utils', 'xmldepend.xsl') if not has_libxml2 or prefer_xsltproc: if has_lxml and not prefer_xsltproc: from lxml import etree xsl_tree = etree.parse(xsl_file) doc = etree.parse(str(node)) result = doc.xslt(xsl_tree) depfiles = [ x.strip() for x in str(result).splitlines() if x.strip() != "" and not x.startswith("<?xml ") ] return depfiles else: # Try to call xsltproc xsltproc = env.subst("$DOCBOOK_XSLTPROC") if xsltproc and xsltproc.endswith('xsltproc'): result = env.backtick(' '.join([xsltproc, xsl_file, str(node)])) depfiles = [ x.strip() for x in str(result).splitlines() if x.strip() != "" and not x.startswith("<?xml ") ] return depfiles else: # Use simple pattern matching, there is currently no support # for xi:includes... contents = node.get_text_contents() return include_re.findall(contents) styledoc = libxml2.parseFile(xsl_file) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.readFile(str(node), None, libxml2.XML_PARSE_NOENT) result = style.applyStylesheet(doc, None) depfiles = [] for x in str(result).splitlines(): if x.strip() != "" and not x.startswith("<?xml "): depfiles.extend(x.strip().split()) style.freeStylesheet() doc.freeDoc() result.freeDoc() return depfiles
def parse_xsl_string(self, xsl_string): try: # parse the stylesheet xml file into doc object styledoc = libxml2.parseDoc(xsl_string) # process the doc object as xslt self._style = libxslt.parseStylesheetDoc(styledoc) except Exception, ex: raise XMLError("Error parsing xsl: %s" % str(ex))
def transform_doc(doc, stylesheet, params={}): if stylesheet not in xsl_cache: xsl_doc = libxml2.parseFile(stylesheet) xsl = libxslt.parseStylesheetDoc(xsl_doc) if xsl is None: raise Exception("Failed to parse XML file into a stylesheet: %s" % stylesheet) xsl_cache[stylesheet] = xsl result = xsl_cache[stylesheet].applyStylesheet(doc, params) return result
def xslt(self, xslfile, xmldoc): styledoc = libxml2.parseFile(xslfile) style = libxslt.parseStylesheetDoc(styledoc) result = style.applyStylesheet(xmldoc, None) out = result.serialize() style.freeStylesheet() result.freeDoc() return out
def sortGuidelineEntry(entry): doc = libxml2.parseFile(entry) styledoc = libxml2.parseFile("./sort_guidelines.xsl") style = libxslt.parseStylesheetDoc(styledoc) result = style.applyStylesheet(doc, None) style.saveResultToFilename(entry, result, 0) msg = "Wrote sorted master modules list to %s" % entry print msg
def performXSLTransform(): styledoc = libxml2.parseFile(xsl_file) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseFile(xml_file) result = style.applyStylesheet(doc, None) style.saveResultToFilename(out_file, result, 0) style.freeStylesheet() doc.freeDoc() result.freeDoc()
def xslt(self,xslfile,xmldoc): styledoc = libxml2.parseFile(xslfile) style = libxslt.parseStylesheetDoc(styledoc) result = style.applyStylesheet(xmldoc, None) out = result.serialize() style.freeStylesheet() result.freeDoc() return out
def xmlTo(xml, xslt, *args, **kwargs): """ Converts xml to anything defined by xslt Keywords was passed to stylesheet as param """ xsl = libxslt.parseStylesheetDoc(xslt) result = xsl.applyStylesheet(xml, kwargs) xsl.freeStylesheet() return result
def getXSLT(xsl_filename): # parse the stylesheet xml file into doc object styledoc = libxml2.parseFile(xsl_filename) # process the doc object as xslt style = libxslt.parseStylesheetDoc(styledoc) return style
def transform(xml_file, xsl_file): xml_doc = libxml2.parseFile(xml_file) xsl_doc = libxml2.parseFile(xsl_file) xsl = libxslt.parseStylesheetDoc(xsl_doc) out_doc = xsl.applyStylesheet(xml_doc, None) print out_doc xsl.freeStylesheet() out_doc.freeDoc() xml_doc.freeDoc()
def begin_conv(): styledoc = libxml2.parseFile(xslttransform) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseFile(inputf) result = style.applyStylesheet(doc, None) style.saveResultToFilename(output, result, 0) style.freeStylesheet() doc.freeDoc() result.freeDoc()
def transform(xml_file, xsl_file): xml_doc = libxml2.parseFile(xml_file) xsl_doc = libxml2.parseFile(xsl_file) xsl = libxslt.parseStylesheetDoc(xsl_doc) out_doc = xsl.applyStylesheet(xml_doc ,None) print out_doc xsl.freeStylesheet() out_doc.freeDoc() xml_doc.freeDoc()
def xsltProcess(styleFile, inputFile, outputFile): """Transform an xml inputFile to an outputFile using the given styleFile""" styledoc = libxml2.parseFile(styleFile) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseFile(inputFile) result = style.applyStylesheet(doc, None) style.saveResultToFilename(outputFile, result, 0) style.freeStylesheet() doc.freeDoc() result.freeDoc()
def transform(xslt, source, target): xslt = 'uml2dj/%s' % xslt styledoc = libxml2.parseFile(xslt) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseFile(source) result = style.applyStylesheet(doc, None) style.saveResultToFilename(target, result, 0) style.freeStylesheet() doc.freeDoc() result.freeDoc()
def convertWithXslt(self, xsltFile, saveFile=None): """Convert to RDF with the specified XSL Template.""" if not self.html: print "convertWithXslt() err: HTML not downloaded!" return htmldoc = self.__convertXhtml() # TODO: Free print "Converting with: %s." % xsltFile xdoc = libxml2.parseFile(xsltFile) # TODO: Free self.xslt = libxslt.parseStylesheetDoc(xdoc) self.rdf = self.xslt.applyStylesheet(htmldoc, None)
def apply_sheet_file(sheet, fn): styledoc = libxml2.parseMemory(sheet, len(sheet)) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseFile(fn) result = style.applyStylesheet(doc, None) res = style.saveResultToString(result) style.freeStylesheet() doc.freeDoc() result.freeDoc() return res
def clean_cnxml(iCnxml, iMaxColumns=80): xsl = os.path.join(current_dir, 'utils_pretty.xsl') style_doc = libxml2.parseFile(xsl) style = libxslt.parseStylesheetDoc(style_doc) doc = libxml2.parseDoc(iCnxml) result = style.applyStylesheet(doc, None) pretty_cnxml = style.saveResultToString(result) style.freeStylesheet() doc.freeDoc() result.freeDoc() return pretty_cnxml
def transform(xml_file, xsl_file, out_file): """ xslt трансформация xml-документа""" print "Открываем документ " + xml_file xml_doc = libxml2.parseFile(xml_file) xsl_doc = libxml2.parseFile(xsl_file) xsl = libxslt.parseStylesheetDoc(xsl_doc) out_doc = xsl.applyStylesheet(xml_doc, None) print out_doc xsl.saveResultToFilename(out_file, out_doc, 0) xsl.freeStylesheet() out_doc.freeDoc() xml_doc.freeDoc()
def XSLTTrans(xmldoc, xsldoc, params = None): """Transforms the xmldoc with xsldoc, and returns the string.""" # print "params: %s" % params styledoc = libxml2.parseDoc(xsldoc) style = libxslt.parseStylesheetDoc(styledoc) doc2t = libxml2.parseDoc(xmldoc) result = style.applyStylesheet(doc2t, params) stringresult = style.saveResultToString(result) style.freeStylesheet() doc2t.freeDoc() result.freeDoc() return stringresult
def __init__(self, config): self.version = '0.3' self.smbios = None self.sharedir = config.installdir if hasattr(dmidecode, "fake"): return self.dmixml = dmidecode.dmidecodeXML() self.smbios = dmidecode.dmi.replace('SMBIOS ', '').replace(' present', '') xsltdoc = self.__load_xslt('rteval_dmi.xsl') self.xsltparser = libxslt.parseStylesheetDoc(xsltdoc)
def internal_render(self, req, name, content): if not name == 'docbook': return 'Unknown macro %s' % (name) style = libxslt.parseStylesheetDoc(libxml2.parseFile(self.stylesheet)) doc = libxml2.parseDoc(content.encode('UTF-8')) result = style.applyStylesheet(doc, None) html = style.saveResultToString(result) style.freeStylesheet() doc.freeDoc() result.freeDoc() return html[html.find('<body>') + 6:html.find('</body>')].strip()
def generate_latex_view(cad): styledoc = libxml2.parseFile(os.path.join(XSL_DIR, 'latex_view.xsl')) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.parseMemory(cad, len(cad)) xmldoc = style.applyStylesheet(doc, {}) retval = style.saveResultToString(xmldoc) style.freeStylesheet() doc.freeDoc() xmldoc.freeDoc() return retval