我们从Python开源项目中,提取了以下33个代码示例,用于说明如何使用xml.etree.ElementTree.register_namespace()。
def xml_parse(xml_file): """ Parse an XML file, returns a tree of nodes and a dict of namespaces :param xml_file: the input XML file :returns: (doc, ns_map) """ root = None ns_map = {} # prefix -> ns_uri for event, elem in ET.iterparse(xml_file, ['start-ns', 'start', 'end']): if event == 'start-ns': # elem = (prefix, ns_uri) ns_map[elem[0]] = elem[1] elif event == 'start': if root is None: root = elem for prefix, uri in ns_map.items(): ET.register_namespace(prefix, uri) return (ET.ElementTree(root), ns_map)
def _prepare_document(self): """ Build the main document node and set xml namespaces. """ self._xml = ET.Element("Document") self._xml.set("xmlns", "urn:iso:std:iso:20022:tech:xsd:" + self.schema) self._xml.set("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") ET.register_namespace("", "urn:iso:std:iso:20022:tech:xsd:" + self.schema) ET.register_namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance") CstmrDrctDbtInitn_node = ET.Element("CstmrDrctDbtInitn") self._xml.append(CstmrDrctDbtInitn_node)
def __str__(self): # # need to register the ome namespace because BioFormats expects # that namespace to be the default or to be explicitly named "ome" # for ns_key in ["ome"]: ns = self.ns.get(ns_key) or NS_DEFAULT.format(ns_key=ns_key) # ElementTree.register_namespace(ns_key, ns) ElementTree.register_namespace('', ns) # ElementTree.register_namespace("om", NS_ORIGINAL_METADATA) result = StringIO() ElementTree.ElementTree(self.root_node).write(result, encoding=uenc, method="xml", xml_declaration = True # default_namespace = 'http://www.openmicroscopy.org/Schemas/ome/2013-06' ) return result.getvalue()
def register_namespace_prefixes(): from saml2 import md, saml, samlp try: from saml2 import xmlenc from saml2 import xmldsig except ImportError: import xmlenc import xmldsig prefixes = (('saml', saml.NAMESPACE), ('samlp', samlp.NAMESPACE), ('md', md.NAMESPACE), ('ds', xmldsig.NAMESPACE), ('xenc', xmlenc.NAMESPACE)) if hasattr(ElementTree, 'register_namespace'): for prefix, namespace in prefixes: ElementTree.register_namespace(prefix, namespace) else: for prefix, namespace in prefixes: ElementTree._namespace_map[namespace] = prefix
def attach_xml_body(tei_file, xml_entries): """Read given TEI XML file until the body tag. From there, insert the given entries. The result is a full TEI XML structure.""" events = ET.iterparse(tei_file, events=["start"]) root = next(events)[1] for _, elem in events: if elem.tag == 'body': break text = next(n for n in root if n.tag.endswith('text')) text.clear() # throw away all potential content body = ET.SubElement(text, 'body') for entry in xml_entries: body.append(entry) ET.register_namespace('', 'http://www.tei-c.org/ns/1.0') return ET.ElementTree(root)
def register_prefix(self, nspair): """ Register with ElementTree a set of namespaces :param nspair: A dictionary of prefixes and uris to use when constructing the text representation. :return: """ for prefix, uri in nspair.items(): try: ElementTree.register_namespace(prefix, uri) except AttributeError: # Backwards compatibility with ET < 1.3 ElementTree._namespace_map[uri] = prefix except ValueError: pass
def remove_office_metadata(input_file, output_file=None): """ Remove all metadata from Microsoft Office 2007+ file types such as docx, pptx, and xlsx. """ input_file = os.path.abspath(input_file) patches = {} ns = { 'cp': 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties', 'dc': 'http://purl.org/dc/elements/1.1/', 'dcterms': 'http://purl.org/dc/terms/', 'dcmitype': 'http://purl.org/dc/dcmitype/', 'xsi': 'http://www.w3.org/2001/XMLSchema-instance' } for prefix, uri in ns.items(): ElementTree.register_namespace(prefix, uri) with zipfile.ZipFile(input_file, 'r') as zin: docprops_core = zin.read('docProps/core.xml') root = ElementTree.fromstring(docprops_core) root.clear() docprops_core = ElementTree.tostring(root, 'utf-8') patches['docProps/core.xml'] = docprops_core archive.patch_zipfile(input_file, patches, output_file=output_file)
def loadFromFile(self): """load the ink from an inkml file (strokes, segments, labels)""" tree = ET.parse(self.fileName) # # ET.register_namespace(); root = tree.getroot() for info in root.findall('ns:annotation',namespaces=Inkml.NS): if 'type' in info.attrib: if info.attrib['type'] == 'truth': self.truth = info.text.strip() if info.attrib['type'] == 'UI': self.UI = info.text.strip() for strk in root.findall('ns:trace',namespaces=Inkml.NS): self.strokes[strk.attrib['id']] = strk.text.strip() self.strkOrder.append(strk.attrib['id']) segments = root.find('ns:traceGroup',namespaces=Inkml.NS) if segments is None or len(segments) == 0: print "No segmentation info" return for seg in (segments.iterfind('ns:traceGroup',namespaces=Inkml.NS)): id = seg.attrib[self.fixNS('xml','id')] label = seg.find('ns:annotation',namespaces=Inkml.NS).text strkList = set([]) for t in seg.findall('ns:traceView',namespaces=Inkml.NS): strkList.add(t.attrib['traceDataRef']) self.segments[id] = Segment(id,label, strkList)
def request(url, data, filepath): tree = ET.parse(filepath) root = tree.getroot() ns = get_namespace(root) ET.register_namespace('', ns) for k, v in data.items(): elem = find_col_elem(root, k) elem.text = v tree = ET.tostring(root, 'utf-8') user_agent = random.choice(USER_AGENTS) headers = { 'Content-Type': 'application/xml', 'User-Agent': user_agent, } response = requests.post(url, data=tree, headers=headers).content return ET.fromstring(response)
def __init__(self, *args, **kwargs): ET.register_namespace("svg", self._SVG_namespace) super(SvgFragmentImage, self).__init__(*args, **kwargs) # Save the unit size, for example the default box_size of 10 is '1mm'. self.unit_size = self.units(self.box_size)
def update_metadata(self, name, metatype, metavalue): ET.register_namespace('kvirt', 'kvirt') conn = self.conn vm = conn.lookupByName(name) xml = vm.XMLDesc(0) root = ET.fromstring(xml) if not vm: print("VM %s not found" % name) return {'result': 'failure', 'reason': "VM %s not found" % name} if vm.isActive() == 1: print("Machine up. Change will only appear upon next reboot") metadata = root.find('metadata') kroot, kmeta = None, None for element in root.getiterator('{kvirt}info'): kroot = element break for element in root.getiterator('{kvirt}%s' % metatype): kmeta = element break if metadata is None: metadata = ET.Element("metadata") kroot = ET.Element("kvirt:info") kroot.set("xmlns:kvirt", "kvirt") kmeta = ET.Element("kvirt:%s" % metatype) root.append(metadata) metadata.append(kroot) kroot.append(kmeta) elif kroot is None: kroot = ET.Element("kvirt:info") kroot.set("xmlns:kvirt", "kvirt") kmeta = ET.Element("kvirt:%s" % metatype) metadata.append(kroot) kroot.append(kmeta) elif kmeta is None: kmeta = ET.Element("kvirt:%s" % metatype) kroot.append(kmeta) kmeta.text = metavalue newxml = ET.tostring(root) conn.defineXML(newxml) return {'result': 'success'}
def loadFile(foldername,filename): """"Load an SVG file. Args: foldername (str): The string of the folder. filename (str): The string of the file name. Returns: The XML tree of the SVG file. """ # Register the name space ET.register_namespace('', "http://www.w3.org/2000/svg") ET.register_namespace('dc',"http://purl.org/dc/elements/1.1/") ET.register_namespace('cc',"http://creativecommons.org/ns#") ET.register_namespace('rdf',"http://www.w3.org/1999/02/22-rdf-syntax-ns#") ET.register_namespace('svg',"http://www.w3.org/2000/svg") ET.register_namespace('sodipodi',"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd") ET.register_namespace('inkscape',"http://www.inkscape.org/namespaces/inkscape") address = foldername + "/" + filename tree = ET.parse(address) return tree
def bug_200709_register_namespace(): """ >>> ET.tostring(ET.Element("{http://namespace.invalid/does/not/exist/}title")) b'<ns0:title xmlns:ns0="http://namespace.invalid/does/not/exist/" />' >>> ET.register_namespace("foo", "http://namespace.invalid/does/not/exist/") >>> ET.tostring(ET.Element("{http://namespace.invalid/does/not/exist/}title")) b'<foo:title xmlns:foo="http://namespace.invalid/does/not/exist/" />' And the Dublin Core namespace is in the default list: >>> ET.tostring(ET.Element("{http://purl.org/dc/elements/1.1/}title")) b'<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/" />' """
def check_issue10777(): """ Registering a namespace twice caused a "dictionary changed size during iteration" bug. >>> ET.register_namespace('test10777', 'http://myuri/') >>> ET.register_namespace('test10777', 'http://myuri/') """ # --------------------------------------------------------------------
def bug_200709_register_namespace(): """ >>> ET.tostring(ET.Element("{http://namespace.invalid/does/not/exist/}title")) '<ns0:title xmlns:ns0="http://namespace.invalid/does/not/exist/" />' >>> ET.register_namespace("foo", "http://namespace.invalid/does/not/exist/") >>> ET.tostring(ET.Element("{http://namespace.invalid/does/not/exist/}title")) '<foo:title xmlns:foo="http://namespace.invalid/does/not/exist/" />' And the Dublin Core namespace is in the default list: >>> ET.tostring(ET.Element("{http://purl.org/dc/elements/1.1/}title")) '<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/" />' """
def update_pom(self, version): ET.register_namespace('', "http://maven.apache.org/POM/4.0.0") tree = ET.parse('pom.xml') version_tag = tree.find(".//{http://maven.apache.org/POM/4.0.0}target.version") version_tag.text = version tree.write('pom.xml')
def register_namespace(prefix, uri): from xml.etree.ElementTree import _namespace_map if re.match("ns\d+$", prefix): raise ValueError("Prefix format reserved for internal use") for k, v in _namespace_map.items(): if k == uri or v == prefix: del _namespace_map[k] _namespace_map[uri] = prefix
def __init__(self,config = None): """constructor """ self._setConfig(config) self.gsDataDir = self.config.get("GeoServer","datadir") self.userPwd = self.config.get("GeoServer","userpwd") ### USERS & GROUPS ### #TODO: Get rid of the hardcoded namespaces # try Xml.register_namespace() and also the Xml.write(default_namespace=...)
def _get_android_manifest(self) -> ElementTree: """ Get the AndroidManifest as a parsed ElementTree :return: """ # use the android namespace ElementTree.register_namespace('android', 'http://schemas.android.com/apk/res/android') return ElementTree.parse(os.path.join(self.apk_temp_directory, 'AndroidManifest.xml'))
def loadtiletemplate(self, src): ElementTree.register_namespace('', self.SVGNS) xmltree = ElementTree.parse(src) xmlroot = xmltree.getroot() a4pxwidth, a4pxheight = self._getsize(xmlroot) # dot per mm dpmm = a4pxwidth / A4WIDTH_MM self.tiledpmm = dpmm #print(a4pxwidth, dpmm) self.tilegroup = xmlroot.find(".//*[@id='tilegroup']") assert self.tilegroup.tag == "{%s}g"%self.SVGNS, "<g id=tilegroup> not found" # remove id #self.tilegroup.attrib.pop("id") # get the width and the height of the template area tilearea = self.tilegroup.find(".//*[@id='tilearea']") #assert tilearea, "<rect id=tilearea> not found" assert tilearea.tag == "{%s}rect"%self.SVGNS, "<rect id=tilearea> not found" self.tilesize = self._getsize(tilearea) # get the rectangle area that are cropped scanarea = self.tilegroup.find(".//*[@id='scanarea']") assert scanarea.tag == "{%s}rect"%self.SVGNS, "<rect id=scanarea> not found" self.rectscanarea = self._getrectfromelem(scanarea, dpmm) # get the rectangle area where pictures are drawn drawarea = self.tilegroup.find(".//*[@id='drawarea']") assert drawarea.tag == "{%s}rect"%self.SVGNS, "<rect id=drawarea> not found" self.rectdrawarea = self._getrectfromelem(drawarea, dpmm) # get the rectangle area where the QR-code are placed qrarea = self.tilegroup.find(".//*[@id='qrarea']") assert qrarea.tag == "{%s}rect"%self.SVGNS, "<rect id=qrarea> not found" self.rectqrarea = self._getrectfromelem(qrarea, dpmm) # get the position where the text is placed textplace = self.tilegroup.find(".//*[@id='textplace']") assert textplace.tag == "{%s}text"%self.SVGNS, "<text id=textplace> not found" textplace.text = "" if "x" in textplace.attrib and "y" in textplace.attrib: strtextx, strtexty = self._getpos(textplace) else: # when designated by tranform attrib strtransform = textplace.get("transform") p = re.compile(r"""matrix\(\d+\.?\d*\s+\d+\.?\d*\s+\d+\.?\d*\s+\d+\.?\d*\s+(\d+\.?\d*)\s+(\d+\.?\d*)\)""") match = p.search(strtransform) assert match, "No position is assigned for the text placeholder" strtextx = match.group(1) strtexty = match.group(2) self.textpos = (float(strtextx), float(strtexty))
def insert_into_pom(self, test_package): if test_package is None: return None test_package = test_package + '*' ET.register_namespace('', 'http://www.w3.org/2000/svg') xml_data = None with open(self.pom_path, 'r') as pom_file: xml_data = pom_file.read() root = ET.XML(xml_data) # print root.attrib plugins_element = None # There needs to be a check here to see if pitest is already included for neighb in root.iter(): if neighb.tag.endswith('plugins'): plugins_element = neighb break if plugins_element is not None: plugin_element = ET.SubElement(plugins_element, 'plugin') groupid_elem = ET.SubElement(plugin_element, 'groupId') groupid_elem.text = 'org.evosuite.plugins' artifactid_elem = ET.SubElement(plugin_element, 'artifactId') artifactid_elem.text = 'evosuite-maven-plugin' version_elem = ET.SubElement(plugin_element, 'version') version_elem.text = '1.0.3' configuration = ET.SubElement(plugin_element, 'configuration') threads_elem = ET.SubElement(configuration, 'threads') threads_elem.text = '4' target_classes = ET.SubElement(configuration, 'targetClasses') class_param_elem = ET.SubElement(target_classes, 'param') class_param_elem.text = test_package target_tests = ET.SubElement(configuration, 'targetTests') test_param_elem = ET.SubElement(target_tests, 'param') test_param_elem.text = test_package reports_dir_elem = ET.SubElement(configuration, 'reportsDirectory') reports_dir_elem.text = 'wolf_reports' ts_report_elem = ET.SubElement(configuration, 'timestampedReports') ts_report_elem.text = 'false' export_line_coverage_elem = ET.SubElement(configuration, 'exportLineCoverage') export_line_coverage_elem.text = 'true' new_data = ET.tostring(root) new_data = new_data.replace('ns0:', '') new_data = new_data.replace(':ns0', '') with open(self.pom_path, 'w') as xml_file: xml_file.write(new_data)
def insert_into_pom(self, test_package): if test_package is None: return None test_package = test_package + '*' ET.register_namespace('', 'http://www.w3.org/2000/svg') xml_data = None with open(self.pom_path, 'r') as pom_file: xml_data = pom_file.read() root = ET.XML(xml_data) # print root.attrib plugins_element = None # There needs to be a check here to see if pitest is already included for neighb in root.iter(): if neighb.tag.endswith('plugins'): plugins_element = neighb break if plugins_element is not None: plugin_element = ET.SubElement(plugins_element, 'plugin') groupid_elem = ET.SubElement(plugin_element, 'groupId') groupid_elem.text = 'org.pitest' artifactid_elem = ET.SubElement(plugin_element, 'artifactId') artifactid_elem.text = 'pitest-maven' version_elem = ET.SubElement(plugin_element, 'version') version_elem.text = '1.1.2' configuration = ET.SubElement(plugin_element, 'configuration') threads_elem = ET.SubElement(configuration, 'threads') threads_elem.text = '4' target_classes = ET.SubElement(configuration, 'targetClasses') class_param_elem = ET.SubElement(target_classes, 'param') class_param_elem.text = test_package target_tests = ET.SubElement(configuration, 'targetTests') test_param_elem = ET.SubElement(target_tests, 'param') test_param_elem.text = test_package reports_dir_elem = ET.SubElement(configuration, 'reportsDirectory') reports_dir_elem.text = 'wolf_reports' ts_report_elem = ET.SubElement(configuration, 'timestampedReports') ts_report_elem.text = 'false' export_line_coverage_elem = ET.SubElement(configuration, 'exportLineCoverage') export_line_coverage_elem.text = 'true' new_data = ET.tostring(root) new_data = new_data.replace('ns0:', '') new_data = new_data.replace(':ns0', '') with open(self.pom_path, 'w') as xml_file: xml_file.write(new_data)
def add_color_svg(self): svg_files = util.get_svg_filepaths(self.conf['color_svg_dir']) svg_list = [] # Set default namespace (avoids "ns0:svg") ET.register_namespace("", "http://www.w3.org/2000/svg") for filepath in svg_files: glyph_id = self.get_glyph_id(filepath) svg_tree = ET.parse(filepath) svg_root = svg_tree.getroot() # Add Glyph ID as SVG root id, required by SVGinOT spec. svg_root.set('id', "glyph{}".format(glyph_id)) # Remove the viewBox/height/width attributes since they are # processed inconsistently by Gecko and Edge renderers. try: del svg_root.attrib['viewBox'] except KeyError: pass try: del svg_root.attrib['height'] except KeyError: pass try: del svg_root.attrib['width'] except KeyError: pass # Add the transform to size the SVG to the FONT_EM svg_transform = self.create_color_transform(filepath) logger.debug("Set SVG transform: {}".format(svg_transform)) svg_transform_attrib = {"transform": svg_transform} # Create a new group tag to apply the transform to new_svg_group = ET.Element('g', svg_transform_attrib) # Copy all SVG root children to the new group for child in svg_root: new_svg_group.append(child) # Backup the root attribs, clear the children, and apply attribs svg_root_attrib = svg_root.items() svg_root.clear() for name, value in svg_root_attrib: svg_root.set(name, value) # Append the new group. svg_root.append(new_svg_group) data = ET.tostring(svg_root, encoding='UTF-8') logger.debug("Glyph ID: %d Adding SVG: %s", glyph_id, filepath) svg_list.append([data, glyph_id, glyph_id]) svg_table = table_S_V_G_() # The SVG table must be sorted by glyph_id svg_table.docList = sorted(svg_list, key=lambda table: table[1]) svg_table.colorPalettes = None self.font['SVG '] = svg_table
def main(): parser = HelpfulParser("deduplicator", description=("Fnd and remove " "duplicated translations and empty TEI nodes")) parser.add_argument("-s", "--detect_changes", dest="detect_changes", help=("check whether duplicates or empty nodes can be detected " "and exit with exit code 42 if the first change would " "need to be made"), action="store_true", default=False) parser.add_argument('dictionary_path', help='input TEI file', nargs="+") args = parser.parse_args(sys.argv[1:]) # register TEI name space without prefix to dump the *same* XML file ET.register_namespace('', 'http://www.tei-c.org/ns/1.0') dictionary_path = args.dictionary_path[0] tree = XmlParserWrapper(dictionary_path) changed = False for entry in tei_iter(tree.root, 'entry'): changed1 = rm_doubled_senses(entry) changed2 = rm_doubled_quotes(entry) # the processing above might leave empty parent nodes, remove those changed3 = rm_empty_nodes(entry) if args.detect_changes and any((changed1, changed2, changed3)): print(("Warning: Found duplicated entries or empty XML nodes. Try " "`make rm_duplicates`.")) sys.exit(42) changed = any((changed, changed1, changed2, changed3)) if changed: output_fn = os.path.join('build', 'tei', dictionary_path.replace('.tei', '-dedup.tei')) exec('mkdir -p build/tei') tree.write(output_fn) # get a human-readable diff of the changes c5 = lambda x: shlex.quote(x.replace('.tei', '.c5')) exec('xsltproc $FREEDICT_TOOLS/xsl/tei2c5.xsl %s > %s' % (output_fn, c5(output_fn))) # convert original dictionary to c5 exec('make --no-print-directory build-dictd') # execute diff without checking the return type if not shutil.which('less'): exec('diff -u build/dictd/%s %s' % (c5(dictionary_path), c5(output_fn))) else: os.system('diff -u build/dictd/%s %s | less' % (c5(dictionary_path), c5(output_fn))) print("If you like the changes, copy build/tei/*.tei to .")
def generate_dict_xml(output_file, frames): # Writes an Apple dictionary file to output_file with the given frames ET.register_namespace('d', 'http://www.apple.com/DTDs/DictionaryService-1.0.rng') out = ET.ElementTree(ET.Element('d:dictionary')) out.getroot().set('xmlns', 'http://www.w3.org/1999/xhtml') out.getroot().set('xmlns:d', 'http://www.apple.com/DTDs/DictionaryService-1.0.rng') for frame in frames: # General metadata entry = ET.SubElement(out.getroot(), 'd:entry') entry.set('id', frame['name']) entry.set('d:title', frame['name']) entry = ET.SubElement(entry, 'div') entry.set('class', 'entry') ET.SubElement(entry, 'd:index').set('d:value', frame['name']) ET.SubElement(entry, 'd:index').set('d:value', frame['lemma']) for alias in frame['aliases']: ET.SubElement(entry, 'd:index').set('d:value', alias) # Name, description, roles ET.SubElement(entry, 'h1').text = frame['name'] e = ET.SubElement(entry, 'div') e.text = frame['desc'] e.set('class', 'desc') dl = ET.SubElement(entry, 'dl') for role in frame['roles']: ET.SubElement(dl, 'dt').text = role['name'] ET.SubElement(dl, 'dd').text = role['desc'] # Examples d = ET.SubElement(entry, 'div') d.set('class', 'subtle') d.set('d:priority', '1') for i, ex in enumerate(frame['examples']): e = ET.SubElement(d, 'p') ET.SubElement(e, 'i').text = 'Ex {} ({}): '.format(i+1, ex['name']) ET.SubElement(e, 'span').text = ex['text'] dl = ET.SubElement(d, 'dl') for arg in ex['args']: ET.SubElement(dl, 'dt').text = arg['name'] ET.SubElement(dl, 'dd').text = arg['value'] out.write(output_file)