def runTransform(self, data, stylesheetName, arg, defaultStylesheet=""): # Find the actual location for the stylesheet stylesheet = self.locateStylesheet(stylesheetName) if stylesheet == None: # if the stylesheet wasn't found try for the default style if defaultStylesheet != "": stylesheet = self.locateStylesheet(defaultStylesheet) if stylesheet == None: # TODO this should throw an exception self.log.error("Stylesheet implementation could not be found for %s" % stylesheetName) return "" resultXML = "" # Parse the stylesheet self.mutex.acquire() try: from time import clock # TODO: room for optimization, should also make sure it's OK for # libxslt to run with multiple threads. style = None try: starttime = clock() styledoc = libxml2.parseFile(stylesheet) style = libxslt.parseStylesheetDoc(styledoc) doc = XMLFragment(data) self.log.debug(str(clock() - starttime) + " seconds for xsl parse") except: self.log.exception("Error parsing stylesheet %s" % stylesheet) raise starttime = clock() result = XMLFragment( style.applyStylesheet( doc.getFragment(), {"arg": '"' + arg + '"', "site_id": '"' + self.configValue("/blog/id") + '"'} ) ) if not (result.serialize() == '<?xml version="1.0"?>\n'): resultXML = style.saveResultToString(result.getFragment()) self.log.debug(str(clock() - starttime) + " seconds for xsl execution") finally: if style != None: style.freeStylesheet() self.mutex.release() # styledoc.freeDoc() # result.freeDoc() return resultXML
def queryDocument(self, content, xpath): doc = XMLFragment(content) result = XMLFragment() results = doc.xpathEval(xpath) if len(results) > 0: root = result.getFragment().newChild(None, "results", None) for item in results: item.unlinkNode() item.reconciliateNs(doc.getDocument()) content = item.serialize() root.addChild(item) return result.serialize() else: return '<?xml version="1.0"?><results/>'