我们从Python开源项目中,提取了以下33个代码示例,用于说明如何使用lxml.etree.XSLT。
def schematron(cls, schema): transforms = [ "xml/schematron/iso_dsdl_include.xsl", "xml/schematron/iso_abstract_expand.xsl", "xml/schematron/iso_svrl_for_xslt1.xsl", ] if isinstance(schema, file): compiled = etree.parse(schema) else: compiled = schema for filename in transforms: with resource_stream( __name__, filename) as stream: xform_xml = etree.parse(stream) xform = etree.XSLT(xform_xml) compiled = xform(compiled) return etree.XSLT(compiled)
def execute(path, cmd, uuid): filename = "%s/%s" % (OUTPUT_PATH, uuid) nmap_cmd = '%s %s -oA %s' % (path, cmd, filename) ops = NmapOptions() ops.parse_string(nmap_cmd) proc = subprocess.Popen(ops.render(), shell=False) proc.wait() print('\n[%s] Finished execution of command "%s"' % (datetime.datetime.now(), cmd)) dom = ET.parse("%s.xml" % filename) xsl_filename = dom.getroot().getprevious().getprevious().parseXSL() # need to add error checking transform = ET.XSLT(xsl_filename) html = transform(dom) html_file = open('%s.html' % filename, 'w') html.write(html_file) print('[%s] HTML report generated (%s.html)' % (datetime.datetime.now(), filename))
def _transform_to_html(self, content, xslt_package=None, xslt_path=None): xslt_package = xslt_package or __name__ xslt_path = xslt_path or \ '../templates/ckanext/spatial/gemini2-html-stylesheet.xsl' # optimise -- read transform only once and compile rather # than at each request with resource_stream(xslt_package, xslt_path) as style: style_xml = etree.parse(style) transformer = etree.XSLT(style_xml) xml = etree.parse(StringIO(content.encode('utf-8'))) html = transformer(xml) response.headers['Content-Type'] = 'text/html; charset=utf-8' response.headers['Content-Length'] = len(content) result = etree.tostring(html, pretty_print=True) return result
def _get_xslt(self, original=False): if original: config_option = \ 'ckanext.spatial.harvest.xslt_html_content_original' else: config_option = 'ckanext.spatial.harvest.xslt_html_content' xslt_package = None xslt_path = None xslt = config.get(config_option, None) if xslt: if ':' in xslt: xslt = xslt.split(':') xslt_package = xslt[0] xslt_path = xslt[1] else: log.error( 'XSLT should be defined in the form <package>:<path>' + ', eg ckanext.myext:templates/my.xslt') return xslt_package, xslt_path
def apply_xsl(mml, xsl): """Apply a xsl to a MathML string @param mml: a string with MathML code @param xsl: a string representing a path to a xsl (xml stylesheet) file. This file name is relative to the PYTHONPATH >>> from sympy.utilities.mathml import apply_xsl >>> xsl = 'mathml/data/simple_mmlctop.xsl' >>> mml = '<apply> <plus/> <ci>a</ci> <ci>b</ci> </apply>' >>> res = apply_xsl(mml,xsl) >>> ''.join(res.splitlines()) '<?xml version="1.0"?><mrow xmlns="http://www.w3.org/1998/Math/MathML"> <mi>a</mi> <mo> + </mo> <mi>b</mi></mrow>' """ from lxml import etree s = etree.XML(get_resource(xsl).read()) transform = etree.XSLT(s) doc = etree.XML(mml) result = transform(doc) s = str(result) return s
def to_panchromatic(self): if self.panchromatic(): warnings.warn("The simulation is already panchromatic") else: simulation = self.tree.xpath("//OligoMonteCarloSimulation")[0] simulation.tag = "PanMonteCarloSimulation" # ---------- Updating information --------------------------------- ## This function applies an XSLT transform to the ski file if an XPath condition evaluates to true. # The first argument is a string specifying an XPath 1.0 expression to be evaluated in the context of the XML # document representing the ski file; the expression value is converted to boolean according to XPath semantics. # If the value is true, the XSLT 1.0 transform specified in the second argument is applied to the XML document, # and the result replaces the original document. The second argument is a string containing one or more # \<xsl:template\> elements that specify the changes to be applied to the document. The \<xsl:stylesheet\> # element and the identity template are automatically added and must not be contained in the argument string. # The function returns true if the transform was applied, and false if it was not (i.e. the document is unchanged).
def transformif(self, condition, templates): needed = self.tree.xpath("boolean(" + condition + ")") if needed: prefix = '''<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>''' postfix = '''</xsl:stylesheet>''' transform = etree.XSLT(etree.XML(prefix + templates + postfix)) self.tree = transform(self.tree) return needed ## This function sets the number of photon packages on the MonteCarloSimulation element in the ski file # to the specified value
def ingest(self, file_path): """Ingestor implementation.""" file_size = self.result.size or os.path.getsize(file_path) if file_size > self.MAX_SIZE: raise ProcessingException("XML file is too large.") try: doc = etree.parse(file_path) except (ParserError, ParseError): raise ProcessingException("XML could not be parsed.") text = self.extract_html_text(doc.getroot()) transform = etree.XSLT(self.XSLT) html_doc = transform(doc) html_body = html.tostring(html_doc, encoding='unicode', pretty_print=True) self.result.flag(self.result.FLAG_HTML) self.result.emit_html_body(html_body, text)
def _detect(env): """ Detect all the command line tools that we might need for creating the requested output formats. """ global prefer_xsltproc if env.get('DOCBOOK_PREFER_XSLTPROC',''): prefer_xsltproc = True if ((not has_libxml2 and not has_lxml) or (prefer_xsltproc)): # Try to find the XSLT processors __detect_cl_tool(env, 'DOCBOOK_XSLTPROC', xsltproc_com, xsltproc_com_priority) __detect_cl_tool(env, 'DOCBOOK_XMLLINT', xmllint_com) __detect_cl_tool(env, 'DOCBOOK_FOP', fop_com, ['fop','xep','jw']) # # Scanners #
def __build_libxml2(target, source, env): """ General XSLT builder (HTML/FO), using the libxml2 module. """ xsl_style = env.subst('$DOCBOOK_XSL') styledoc = libxml2.parseFile(xsl_style) style = libxslt.parseStylesheetDoc(styledoc) doc = libxml2.readFile(str(source[0]),None,libxml2.XML_PARSE_NOENT) # Support for additional parameters parampass = {} if parampass: result = style.applyStylesheet(doc, parampass) else: result = style.applyStylesheet(doc, None) style.saveResultToFilename(str(target[0]), result, 0) style.freeStylesheet() doc.freeDoc() result.freeDoc() return None
def _detect(env): """ Detect all the command line tools that we might need for creating the requested output formats. """ global prefer_xsltproc if env.get('DOCBOOK_PREFER_XSLTPROC',''): prefer_xsltproc = True if ((not has_libxml2 and not has_lxml) or (prefer_xsltproc)): # Try to find the XSLT processors __detect_cl_tool(env, 'DOCBOOK_XSLTPROC', xsltproc_com) __detect_cl_tool(env, 'DOCBOOK_XMLLINT', xmllint_com) __detect_cl_tool(env, 'DOCBOOK_FOP', fop_com) # # Scanners #
def GetInstanceList(self, root, name, debug=False): instances = [] project = self.GetProject(debug) if project is not None: factory = InstancesPathFactory(instances) parser = etree.XMLParser() parser.resolvers.add(LibraryResolver(self, debug)) instances_path_xslt_tree = etree.XSLT( etree.parse( os.path.join(ScriptDirectory, "plcopen", "instances_path.xslt"), parser), extensions={ ("instances_ns", "AddInstance"): factory.AddInstance}) instances_path_xslt_tree( root, instance_type=etree.XSLT.strparam(name)) return instances
def GetPouInstanceTagName(self, instance_path, debug=False): project = self.GetProject(debug) factory = InstanceTagName(self) parser = etree.XMLParser() parser.resolvers.add(LibraryResolver(self, debug)) instance_tagname_xslt_tree = etree.XSLT( etree.parse( os.path.join(ScriptDirectory, "plcopen", "instance_tagname.xslt"), parser), extensions={("instance_tagname_ns", name): getattr(factory, name) for name in ["ConfigTagName", "ResourceTagName", "PouTagName", "ActionTagName", "TransitionTagName"]}) instance_tagname_xslt_tree( project, instance_path=etree.XSLT.strparam(instance_path)) return factory.GetTagName()
def GetVariableDictionary(self, object_with_vars, tree=False, debug=False): variables = [] factory = VariablesInfosFactory(variables) parser = etree.XMLParser() parser.resolvers.add(LibraryResolver(self, debug)) variables_infos_xslt_tree = etree.XSLT( etree.parse( os.path.join(ScriptDirectory, "plcopen", "variables_infos.xslt"), parser), extensions={("var_infos_ns", name): getattr(factory, name) for name in ["SetType", "AddDimension", "AddTree", "AddVarToTree", "AddVariable"]}) variables_infos_xslt_tree( object_with_vars, tree=etree.XSLT.strparam(str(tree))) return variables # Add a global var to configuration to configuration
def course_to_html(self): for i, section in enumerate(self.course.sections): section.title = self.content_to_html(section.title) section.title = "".join(ET.fromstring(section.title).itertext()) #section.title = html.fromstring(section.title).text_content() # clean up tags if not self.keepnums: section.remove_title_numbering() progress = str(i * 100 / len(self.course.sections)) + '%' print '\r > Applying XSLT to the course (' + progress + ')', sys.stdout.flush() for session in section.sessions: session.title = self.content_to_html(session.title) session.content = self.content_to_html(session.content) print '\r > Applying XSLT to the course (100%). Done.'
def XMLtoXHTML(XMLfile, XSLfile): ''' Turns an XML file to txt according to an XSL file XMLfile: The XML file that XSLfile: The XSL file that we use to tranform the XML file ''' from lxml import etree xmltree = etree.parse(XMLfile) xsl = etree.parse(XSLfile) transform = etree.XSLT(xsl) NewXmlTree = transform(xmltree) return NewXmlTree
def get_original(document, xslt): """Get the original chain given document path and xslt local path :param str document: local absolute path to document :param str xslt: local absolute path to xst file :return: new chain generated. :rtype: str """ dom = etree.parse(document) # TODO: cuando este probando - # fuente: # http://stackoverflow.com/questions/16698935/how-to-transform-an-xml-file-using-xslt-in-python xslt = etree.parse(xslt) transform = etree.XSLT(xslt) newdom = transform(dom) return newdom
def remove_empty_tags(xml, ouput_as_string=True): """Remove empty tags with xslt transformation. param: xml a string or a etree type return: unicode string or lxml.etree._XSLTResultTree """ # use Jinja env for getting the path of template file # pkg_resouces may be an alternative, but we already # have Jinja env = Environment( loader=PackageLoader('roulier', 'templates'), extensions=['jinja2.ext.with_']) template = env.get_template("remove_empty_tags.xsl") xsl = etree.parse(open(template.filename)) transform = etree.XSLT(xsl) if isinstance(xml, basestring): xml = etree.fromstring(xml) # else we asume xml is an lxml.etree if ouput_as_string: return unicode(transform(xml)) else: return transform(xml)
def __build_lxml(target, source, env): """ General XSLT builder (HTML/FO), using the lxml module. """ from lxml import etree xslt_ac = etree.XSLTAccessControl(read_file=True, write_file=True, create_dir=True, read_network=False, write_network=False) xsl_style = env.subst('$DOCBOOK_XSL') xsl_tree = etree.parse(xsl_style) transform = etree.XSLT(xsl_tree, access_control=xslt_ac) doc = etree.parse(str(source[0])) # Support for additional parameters parampass = {} if parampass: result = transform(doc, **parampass) else: result = transform(doc) try: of = open(str(target[0]), "wb") of.write(of.write(etree.tostring(result, pretty_print=True))) of.close() except: pass return None
def __build_lxml(target, source, env): """ General XSLT builder (HTML/FO), using the lxml module. """ from lxml import etree xslt_ac = etree.XSLTAccessControl(read_file=True, write_file=True, create_dir=True, read_network=False, write_network=False) xsl_style = env.subst('$DOCBOOK_XSL') xsl_tree = etree.parse(xsl_style) transform = etree.XSLT(xsl_tree, access_control=xslt_ac) doc = etree.parse(str(source[0])) # Support for additional parameters parampass = {} if parampass: result = transform(doc, **parampass) else: result = transform(doc) try: of = open(str(target[0]), "w") of.write(of.write(etree.tostring(result, pretty_print=True))) of.close() except: pass return None
def GetPouVariables(self, tagname, debug=False): pou_type = None project = self.GetProject(debug) if project is not None: factory = VariablesTreeInfosFactory() parser = etree.XMLParser() parser.resolvers.add(LibraryResolver(self, debug)) pou_variable_xslt_tree = etree.XSLT( etree.parse( os.path.join(ScriptDirectory, "plcopen", "pou_variables.xslt"), parser), extensions={("pou_vars_ns", name): getattr(factory, name) for name in ["SetRoot", "AddVariable"]}) obj = None words = tagname.split("::") if words[0] == "P": obj = self.GetPou(words[1], debug) elif words[0] != "D": obj = self.GetEditedElement(tagname, debug) if obj is not None: pou_variable_xslt_tree(obj) return factory.GetRoot() return None
def GetPouInterfaceReturnType(self, pou, tree=False, debug=False): # Verify that the pou has an interface if pou.interface is not None: # Return the return type if there is one return_type = pou.interface.getreturnType() if return_type is not None: factory = VariablesInfosFactory([]) parser = etree.XMLParser() parser.resolvers.add(LibraryResolver(self)) return_type_infos_xslt_tree = etree.XSLT( etree.parse( os.path.join(ScriptDirectory, "plcopen", "variables_infos.xslt"), parser), extensions={("var_infos_ns", name): getattr(factory, name) for name in ["SetType", "AddDimension", "AddTree", "AddVarToTree"]}) return_type_infos_xslt_tree( return_type, tree=etree.XSLT.strparam(str(tree))) if tree: return [factory.GetType(), factory.GetTree()] return factory.GetType() if tree: return [None, ([], [])] return None # Function that add a new confnode to the confnode list
def content_to_html(self, content): """Apply XSLT to the content of the course and returns the converted HTML text""" dom = ET.XML(content, ET.XMLParser()) xslt = ET.parse(self.xsl_file) transform = ET.XSLT(xslt) newdom = transform(dom) return re.sub('( )+', ' ', ET.tostring(newdom, pretty_print=True))
def transform(f): t = etree.XSLT(etree.parse("SAR_TO_FODS.xslt")) if type(f) in types.StringTypes: doc = etree.parse(f) else: doc = f return t(doc)
def transform(f): directory = os.path.dirname(os.path.abspath(__file__)) xslt_filename = os.path.join(directory, "FODS_TO_SAR.xslt") t = etree.XSLT(etree.parse(xslt_filename)) if type(f) in types.StringTypes: doc = etree.parse(f) else: doc = f return t(doc)
def transform(f): directory = os.path.dirname(os.path.abspath(__file__)) xslt_filename = os.path.join(directory, "XLS_TO_SAR.xslt") t = etree.XSLT(etree.parse(xslt_filename)) if type(f) in types.StringTypes: doc = etree.parse(f) else: doc = f return t(doc)