def default_loader(href, parse, encoding=None): file = open(href, 'rb') if parse == "xml": data = etree.parse(file).getroot() else: data = file.read() if not encoding: encoding = 'utf-8' data = data.decode(encoding) file.close() return data
def _lxml_default_loader(href, parse, encoding=None, parser=None): if parse == "xml": data = etree.parse(href, parser).getroot() else: if "://" in href: f = urlopen(href) else: f = open(href, 'rb') data = f.read() f.close() if not encoding: encoding = 'utf-8' data = data.decode(encoding) return data
def __init__(self, etree=None, file=None, include=True, expand=True, include_params={}, expand_params={}, compile_params={}, store_schematron=False, store_xslt=False, store_report=False, phase=None, error_finder=ASSERTS_ONLY): super(Schematron, self).__init__() self._store_report = store_report self._schematron = None self._validator_xslt = None self._validation_report = None if error_finder is not self.ASSERTS_ONLY: self._validation_errors = error_finder # parse schema document, may be a schematron schema or an XML Schema or # a RelaxNG schema with embedded schematron rules root = None try: if etree is not None: if _etree.iselement(etree): root = etree else: root = etree.getroot() elif file is not None: root = _etree.parse(file).getroot() except Exception: raise _etree.SchematronParseError("No tree or file given: %s" % sys.exc_info()[1]) if root is None: raise ValueError("Empty tree") if root.tag == _schematron_root: schematron = root else: schematron = self._extract(root) if schematron is None: raise _etree.SchematronParseError( "Document is not a schematron schema or schematron-extractable" ) # perform the iso-schematron skeleton implementation steps to get a # validating xslt if include: schematron = self._include(schematron, **include_params) if expand: schematron = self._expand(schematron, **expand_params) if not schematron_schema_valid(schematron): raise _etree.SchematronParseError( "invalid schematron schema: %s" % schematron_schema_valid.error_log) if store_schematron: self._schematron = schematron # add new compile keyword args here if exposing them compile_kwargs = {'phase': phase} compile_params = _stylesheet_param_dict(compile_params, compile_kwargs) validator_xslt = self._compile(schematron, **compile_params) if store_xslt: self._validator_xslt = validator_xslt self._validator = _etree.XSLT(validator_xslt)
# some namespaces #FIXME: Maybe lxml should provide a dedicated place for common namespace #FIXME: definitions? XML_SCHEMA_NS = "http://www.w3.org/2001/XMLSchema" RELAXNG_NS = "http://relaxng.org/ns/structure/1.0" SCHEMATRON_NS = "http://purl.oclc.org/dsdl/schematron" SVRL_NS = "http://purl.oclc.org/dsdl/svrl" # some helpers _schematron_root = '{%s}schema' % SCHEMATRON_NS _xml_schema_root = '{%s}schema' % XML_SCHEMA_NS _resources_dir = os.path.join(os.path.dirname(__file__), 'resources') # the iso-schematron skeleton implementation steps aka xsl transformations extract_xsd = _etree.XSLT( _etree.parse(os.path.join(_resources_dir, 'xsl', 'XSD2Schtrn.xsl'))) extract_rng = _etree.XSLT( _etree.parse(os.path.join(_resources_dir, 'xsl', 'RNG2Schtrn.xsl'))) iso_dsdl_include = _etree.XSLT( _etree.parse( os.path.join(_resources_dir, 'xsl', 'iso-schematron-xslt1', 'iso_dsdl_include.xsl'))) iso_abstract_expand = _etree.XSLT( _etree.parse( os.path.join(_resources_dir, 'xsl', 'iso-schematron-xslt1', 'iso_abstract_expand.xsl'))) iso_svrl_for_xslt1 = _etree.XSLT( _etree.parse( os.path.join(_resources_dir, 'xsl', 'iso-schematron-xslt1', 'iso_svrl_for_xslt1.xsl')))
current_size = 0 for chunk in r.iter_content(chunk_size=1024): if chunk: current_size += len(chunk) f.write(chunk) download_dialog.set_progress(current_size) download_dialog.close() InfoDialog("Install Game...", "Installing Game \"" + game_to_install + "\"\nPlease wait...") with open("/etc/emulationstation/es_systems.cfg", "r") as f: xml_data = f.read() root = etree.parse(xml_data) system = None systems = root.findall(".//system") for s in systems: if s.find(".//name").text == store: system = s break rom_path_object = system.find("./path") rom_path = etree.tostring(rom_path_object).replace(" ", "").replace("\n", "") + "/" extensions_object = system.find("./extension") allowed_extensions = etree.tostring(extensions_object).replace("\n", "").split(" ")
+__author__='admin' +from lxml import etree +tree = etree.parse('Sergeeva1.xml') +list = tree.getroot() + +for elem in list.iter('price'): + newcount = elem.get('price') + newline = newcount.split('-') + + for product in newline: + subelement = etree.SubElement(elem, 'discount') + subelement.text = product + print(product + 'discount' + '%') + +text = etree.tostring(list, pretty_print=True, encoding='UTF-8') +function = open('Sergeeva1.xml', 'w') +function.write(text) +function.close()
def parser_xml(xml_str): return etree.parse(xml_str)
--- tests/gtester2xunit.py.orig 2018-11-07 22:39:11 UTC +++ tests/gtester2xunit.py @@ -1,18 +1,17 @@ #! /usr/bin/python from argparse import ArgumentParser -import libxslt -import libxml2 import sys import os +from lxml import etree XSL_TRANSFORM='/usr/share/gtester2xunit/gtester.xsl' def transform_file(input_filename, output_filename, xsl_file): - gtester = libxml2.parseFile(xsl_file) - style = libxslt.parseStylesheetDoc(gtester) - doc = libxml2.parseFile(input_filename) - result = style.applyStylesheet(doc, None) - result.saveFormatFile(filename=output_filename, format=True) + gtester = etree.parse(xsl_file) + style = etree.XSLT(gtester) + doc = etree.parse(input_filename) + result = style(doc) + result.write(filename=output_filename, format=True) def get_output_filename(input_filename):
def __init__(self, filepath): self.tree = etree.parse(filepath)