def parse_xmlelements(self, xmlelements, schema, name=None, context=None): """Consume matching xmlelements and call parse() on each of them :param xmlelements: Dequeue of XML element objects :type xmlelements: collections.deque of lxml.etree._Element :param schema: The parent XML schema :type schema: zeep.xsd.Schema :param name: The name of the parent element :type name: str :param context: Optional parsing context (for inline schemas) :type context: zeep.xsd.context.XmlParserContext :return: dict or None """ result = [] num_matches = 0 for _unused in max_occurs_iter(self.max_occurs): if not xmlelements: break # Workaround for SOAP servers which incorrectly use unqualified # or qualified elements in the responses (#170, #176). To make the # best of it we compare the full uri's if both elements have a # namespace. If only one has a namespace then only compare the # localname. # If both elements have a namespace and they don't match then skip element_tag = etree.QName(xmlelements[0].tag) if (element_tag.namespace and self.qname.namespace and element_tag.namespace != self.qname.namespace and schema.settings.strict): break # Only compare the localname if element_tag.localname == self.qname.localname: xmlelement = xmlelements.popleft() num_matches += 1 item = self.parse(xmlelement, schema, allow_none=True, context=context) result.append(item) elif (schema is not None and schema.settings.xsd_ignore_sequence_order and list( filter( lambda elem: etree.QName(elem.tag).localname == self. qname.localname, xmlelements))): # Search for the field in remaining elements, not only the leftmost xmlelement = list( filter( lambda elem: etree.QName(elem.tag).localname == self. qname.localname, xmlelements))[0] xmlelements.remove(xmlelement) num_matches += 1 item = self.parse(xmlelement, schema, allow_none=True, context=context) result.append(item) else: # If the element passed doesn't match and the current one is # not optional then throw an error if num_matches == 0 and not self.is_optional: raise UnexpectedElementError( "Unexpected element %r, expected %r" % (element_tag.text, self.qname.text)) break if not self.accepts_multiple: result = result[0] if result else None return result
def visit_element(self, node, parent): """ Definition:: <element abstract = Boolean : false block = (#all | List of (extension | restriction | substitution)) default = string final = (#all | List of (extension | restriction)) fixed = string form = (qualified | unqualified) id = ID maxOccurs = (nonNegativeInteger | unbounded) : 1 minOccurs = nonNegativeInteger : 1 name = NCName nillable = Boolean : false ref = QName substitutionGroup = QName type = QName {any attributes with non-schema Namespace}...> Content: (annotation?, ( (simpleType | complexType)?, (unique | key | keyref)*)) </element> :param node: The XML node :type node: lxml.etree._Element :param parent: The parent XML node :type parent: lxml.etree._Element """ is_global = parent.tag == tags.schema # minOccurs / maxOccurs are not allowed on global elements if not is_global: min_occurs, max_occurs = _process_occurs_attrs(node) else: max_occurs = 1 min_occurs = 1 # If the element has a ref attribute then all other attributes cannot # be present. Short circuit that here. # Ref is prohibited on global elements (parent = schema) if not is_global: result = self.process_reference(node, min_occurs=min_occurs, max_occurs=max_occurs) if result: return result element_form = node.get('form', self.document._element_form) if element_form == 'qualified' or is_global: qname = qname_attr(node, 'name', self.document._target_namespace) else: qname = etree.QName(node.get('name')) children = node.getchildren() xsd_type = None if children: value = None for child in children: if child.tag == tags.annotation: continue elif child.tag in (tags.simpleType, tags.complexType): assert not value xsd_type = self.process(child, node) if not xsd_type: node_type = qname_attr(node, 'type') if node_type: xsd_type = self._get_type(node_type.text) else: xsd_type = xsd_types.AnyType() # Naive workaround to mark fields which are part of a choice element # as optional if parent.tag == tags.choice: min_occurs = 0 nillable = node.get('nillable') == 'true' default = node.get('default') element = xsd_elements.Element(name=qname, type_=xsd_type, min_occurs=min_occurs, max_occurs=max_occurs, nillable=nillable, default=default, is_global=is_global) # Only register global elements if is_global: self.register_element(qname, element) return element
def test_complex_any_types(): # see https://github.com/mvantellingen/python-zeep/issues/252 schema = xsd.Schema( load_xml(""" <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tests.python-zeep.org/" targetNamespace="http://tests.python-zeep.org/" elementFormDefault="qualified"> <element name="container"> <complexType> <sequence> <element minOccurs="0" maxOccurs="1" name="auth" type="anyType" /> <element minOccurs="0" maxOccurs="1" name="params" type="anyType" /> </sequence> </complexType> </element> </schema> """)) schema.set_ns_prefix('tns', 'http://tests.python-zeep.org/') KeyValueData = xsd.Element( '{http://xml.apache.org/xml-soap}KeyValueData', xsd.ComplexType( xsd.Sequence([ xsd.Element( 'key', xsd.AnyType(), ), xsd.Element( 'value', xsd.AnyType(), ), ]), ), ) Map = xsd.ComplexType( xsd.Sequence([ xsd.Element('item', xsd.AnyType(), min_occurs=1, max_occurs="unbounded"), ]), qname=etree.QName('{http://xml.apache.org/xml-soap}Map')) header_Username = KeyValueData(xsd.AnyObject(xsd.String(), 'Username'), value=xsd.AnyObject(xsd.String(), 'abc')) header_ShopId = KeyValueData(xsd.AnyObject(xsd.String(), 'ShopId'), value=xsd.AnyObject(xsd.Int(), 123)) auth = Map(item=[header_Username, header_ShopId]) header_LimitNum = KeyValueData(xsd.AnyObject(xsd.String(), 'LimitNum'), value=xsd.AnyObject(xsd.Int(), 2)) params = Map(item=[header_LimitNum]) container = schema.get_element('ns0:container') obj = container(auth=auth, params=params) result = render_node(container, obj) expected = load_xml(""" <document> <ns0:container xmlns:ns0="http://tests.python-zeep.org/"> <ns0:auth xmlns:ns1="http://xml.apache.org/xml-soap" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns1:Map"> <item> <key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Username</key> <value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">abc</value> </item> <item> <key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">ShopId</key> <value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:int">123</value> </item> </ns0:auth> <ns0:params xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:Map"> <item> <key xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">LimitNum</key> <value xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:int">2</value> </item> </ns0:params> </ns0:container> </document> """) # noqa assert_nodes_equal(result, expected)
print(root.get("ddd")) # return text of this attribute for x in root.xpath('.//@*'): print(x) for x in root.xpath('.//*'): print('txt:', x.text) for x in root.xpath('.//*/text()'): print("text(): ", x) for p in root.findall('.//*[@first]'): p.set('first', "4") for x in root: if x.tag != etree.Comment: print(x.tag) print(etree.QName(x.tag).localname) print(etree.QName(x.tag).namespace) print(etree.QName(x.tag)) ss = elem.text elem2.text = elem.tail print(ss) root[-1] = root[0] print( etree.tostring(doc, encoding='UTF-8', xml_declaration=True, pretty_print=True)) print(elem.getnext()) if elem.get('sdsdsd'): print("ddddddddddd") else: print('aaaaaaaaa')
def __getattr__(self, name): return etree.QName(self.ns, name.strip("_"))
def get_child_by_name(self, name): """Get the child element by the tag name.""" for child in self._element.iterchildren(): if child.tag == name or etree.QName(child.tag).localname == name: return NaElement(child) return None
def filter_tag_match(filter_tag, elm_tag): fqname = etree.QName(filter_tag) eqname = qname(elm_tag) if not fqname.namespace: return fqname.localname == eqname.localname return fqname == eqname
def splitns(name): qn = etree.QName(name) return qn.namespace, qn.localname
def verb(**kwargs): """Create OAI-PMH envelope for response with verb.""" e_tree, e_oaipmh = envelope(**kwargs) e_element = SubElement(e_oaipmh, etree.QName(NS_OAIPMH, kwargs['verb'])) return e_tree, e_element
def child_with_qname(element: etree._Element, qname: etree.QName): for child in element.iterchildren(qname.text): if etree.QName(child).text == qname.text: return child
def node_to_xml_element(node, parent_namespace=None, strip_whitespace=True): if lxml_available: if isinstance(node, ET._Comment): items = [ Expression(Expression("XMLObject", String("Comment")), String(node.text)) ] if node.tail is not None: items.append(String(node.tail)) return items # see https://reference.wolfram.com/language/XML/tutorial/RepresentingXML.html default_namespace = node.get("xmlns") if default_namespace is None: default_namespace = parent_namespace if lxml_available: tag = ET.QName(node.tag) localname = tag.localname namespace = tag.namespace else: tag = node.tag if not tag.startswith("{"): namespace = None localname = tag else: m = re.match(r"\{(.*)\}(.*)", node.tag) namespace = m.group(1) localname = m.group(2) def children(): text = node.text if text: if strip_whitespace: text = text.strip() if text: yield String(text) for child in node: for element in node_to_xml_element(child, default_namespace, strip_whitespace): yield element tail = node.tail if tail: if strip_whitespace: tail = tail.strip() if tail: yield String(tail) def attributes(): for name, value in node.attrib.items(): if name == "xmlns": name = _namespace_key else: name = from_python(name) yield Expression("Rule", name, from_python(value)) if namespace is None or namespace == default_namespace: name = String(localname) else: name = Expression("List", String(namespace), String(localname)) return [ Expression( "XMLElement", name, Expression("List", *list(attributes())), Expression("List", *list(children())), ) ]
def load_from_file(self, file_path): ''' Process an XMI file into a set of collections (each Package becomes a collection) :param file_path: Location of XMI file :return: LXML parse root. Also alters internal state by populating self.collection_list with gathered UML Packages. ''' loaded_xmi = open(file_path, 'r') load_up = etree.parse(loaded_xmi) package_list = self.initial_parse(load_up) # transform found packages into collections for package in package_list: model_collection = None for attribute in package.attrib.keys(): split_key = etree.QName(attribute) # cycle through the keys (can get more efficient once we learn how # to compose keys with namespaces if split_key.localname == 'name': model_collection = Collection() model_collection.name = package.attrib[attribute] self.collection_list.append(model_collection) for packedElement in package: split_tag = etree.QName(packedElement.tag) if split_tag.localname == 'packagedElement': thing_name = '' thing_type = '' thing_id = '' new_thing = None added_properties = [] for attribute in packedElement.attrib.keys(): split_key = etree.QName(attribute) # cycle through the keys (can get more efficient once we learn how # to compose keys with namespaces if split_key.localname == 'name': thing_name = packedElement.attrib[attribute] if split_key.localname == 'id': thing_id = packedElement.attrib[attribute] if split_key.localname == 'type': thing_type = packedElement.attrib[attribute] # add class to package to be part of collection if packedElement.attrib[attribute] == 'uml:Class': new_thing = Class() added_properties = self.collect_attributes( packedElement) for added_prop in added_properties: added_properties[ added_prop].class_ = new_thing elif packedElement.attrib[ attribute] == 'uml:Association': new_thing = Association() added_properties = self.collect_attributes( packedElement) # MD has an included element, Papyrus just a tag collect_ends = self.collect_ends(packedElement) for new_end in collect_ends: new_thing.member_end.append(new_end) for new_end in added_properties: new_thing.owned_end.append(new_end) elif packedElement.attrib[ attribute] == 'uml:AssociationClass': new_thing = AssociationClass() added_properties = self.collect_attributes( packedElement) # this logic needs to be recursive to handle owned elements and their specializations if new_thing is not None and thing_name is not None: new_thing.name = thing_name model_collection.add_object_to_collection_with_id( new_thing, thing_id) for new_prop in added_properties: model_collection.add_object_to_collection_with_id( added_properties[new_prop], new_prop) # Once all objects are in the collection, need to resolve their references to make # them live # Note that some references will remain unresolved because their targets are in a different collection # (package) for package in self.collection_list: for collected in package.id_to_object_map: package.resolve_references(collected) return load_up
logging.basicConfig(level=logging.INFO) # Global variables SEMLANCET_NS = "http://www.semanticlancet.eu/resource/" SEMLANCET_URI_PRO_ROLE_AUTHOR = "http://purl.org/spar/pro/author" SUMMARY_FILENAME = "_CITATION_CONTEXTS_SUMMARY.csv" REPORT_FILENAME = "_REPORT.txt" NO_CROSS_REFS_LIST = "_NO_CROSS_REFS.txt" RDF_EXTENSION = "ttl" RDF_SERIALIZATION_FORMAT = "turtle" NON_DECIMAL = re.compile(r'[^\d.]+') # namespaces CE = "http://www.elsevier.com/xml/common/dtd" NS_MAP = {'ce': CE} cross_ref_tag_name = et.QName(CE, 'cross-ref') cross_refs_tag_name = '{http://www.elsevier.com/xml/common/dtd}cross-refs' # rdf namspaces frbrNS = Namespace('http://purl.org/vocab/frbr/core#') coNS = Namespace('http://purl.org/co/') foafNS = Namespace('http://xmlns.com/foaf/0.1/') c4oNS = Namespace('http://purl.org/spar/c4o/') proNS = Namespace('http://purl.org/spar/pro/') docoNS = Namespace('http://purl.org/spar/doco/') ns_mgr = NamespaceManager(Graph()) ns_mgr.bind('frbr', frbrNS, override=False) ns_mgr.bind('co', coNS, override=False) ns_mgr.bind('foaf', foafNS, override=False) ns_mgr.bind('c4o', c4oNS, override=False)
def nodename(self, name): """Extends name with the xmlns-prefix to a valid nodename.""" return etree.QName(self.root, name)
def _export_xml_get_dati_modulo(self, quadro): # 1.2.2.1 Modulo xModulo = etree.Element(etree.QName(NS_IV, "Modulo")) # Numero Modulo NumeroModulo = etree.SubElement(xModulo, etree.QName(NS_IV, "NumeroModulo")) NumeroModulo.text = str(self._context.get('nr_modulo', 1)) if quadro.period_type == 'month': # 1.2.2.1.1 Mese Mese = etree.SubElement(xModulo, etree.QName(NS_IV, "Mese")) Mese.text = str(quadro.month) else: # 1.2.2.1.2 Trimestre Trimestre = etree.SubElement(xModulo, etree.QName(NS_IV, "Trimestre")) Trimestre.text = str(quadro.quarter) # Da escludere per liquidazione del gruppo if not self.liquidazione_del_gruppo: # 1.2.2.1.3 Subfornitura if quadro.subcontracting: Subfornitura = etree.SubElement( xModulo, etree.QName(NS_IV, "Subfornitura")) Subfornitura.text = '1' if quadro.subcontracting \ else '0' # 1.2.2.1.4 EventiEccezionali if quadro.exceptional_events: EventiEccezionali = etree.SubElement( xModulo, etree.QName(NS_IV, "EventiEccezionali")) EventiEccezionali.text = quadro.exceptional_events # 1.2.2.1.5 TotaleOperazioniAttive TotaleOperazioniAttive = etree.SubElement( xModulo, etree.QName(NS_IV, "TotaleOperazioniAttive")) TotaleOperazioniAttive.text = "{:.2f}"\ .format(quadro.imponibile_operazioni_attive).replace('.', ',') # 1.2.2.1.6 TotaleOperazioniPassive TotaleOperazioniPassive = etree.SubElement( xModulo, etree.QName(NS_IV, "TotaleOperazioniPassive")) TotaleOperazioniPassive.text = "{:.2f}"\ .format(quadro.imponibile_operazioni_passive).replace('.', ',') # 1.2.2.1.7 IvaEsigibile IvaEsigibile = etree.SubElement(xModulo, etree.QName(NS_IV, "IvaEsigibile")) IvaEsigibile.text = "{:.2f}".format(quadro.iva_esigibile)\ .replace('.', ',') # 1.2.2.1.8 IvaDetratta IvaDetratta = etree.SubElement(xModulo, etree.QName(NS_IV, "IvaDetratta")) IvaDetratta.text = "{:.2f}".format(quadro.iva_detratta)\ .replace('.', ',') # 1.2.2.1.9 IvaDovuta if quadro.iva_dovuta_debito: IvaDovuta = etree.SubElement(xModulo, etree.QName(NS_IV, "IvaDovuta")) IvaDovuta.text = "{:.2f}".format(quadro.iva_dovuta_debito)\ .replace('.', ',') # 1.2.2.1.10 IvaCredito if quadro.iva_dovuta_credito: IvaCredito = etree.SubElement(xModulo, etree.QName(NS_IV, "IvaCredito")) IvaCredito.text = "{:.2f}".format(quadro.iva_dovuta_credito)\ .replace('.', ',') # 1.2.2.1.11 DebitoPrecedente DebitoPrecedente = etree.SubElement( xModulo, etree.QName(NS_IV, "DebitoPrecedente")) DebitoPrecedente.text = "{:.2f}".format( quadro.debito_periodo_precedente).replace('.', ',') # 1.2.2.1.12 CreditoPeriodoPrecedente CreditoPeriodoPrecedente = etree.SubElement( xModulo, etree.QName(NS_IV, "CreditoPeriodoPrecedente")) CreditoPeriodoPrecedente.text = "{:.2f}".format( quadro.credito_periodo_precedente).replace('.', ',') # 1.2.2.1.13 CreditoAnnoPrecedente CreditoAnnoPrecedente = etree.SubElement( xModulo, etree.QName(NS_IV, "CreditoAnnoPrecedente")) CreditoAnnoPrecedente.text = "{:.2f}".format( quadro.credito_anno_precedente).replace('.', ',') # 1.2.2.1.14 VersamentiAutoUE VersamentiAutoUE = etree.SubElement( xModulo, etree.QName(NS_IV, "VersamentiAutoUE")) VersamentiAutoUE.text = "{:.2f}".format( quadro.versamento_auto_UE).replace('.', ',') # 1.2.2.1.15 CreditiImposta CreditiImposta = etree.SubElement(xModulo, etree.QName(NS_IV, "CreditiImposta")) CreditiImposta.text = "{:.2f}".format(quadro.crediti_imposta).replace( '.', ',') # 1.2.2.1.16 InteressiDovuti InteressiDovuti = etree.SubElement( xModulo, etree.QName(NS_IV, "InteressiDovuti")) InteressiDovuti.text = "{:.2f}".format( quadro.interessi_dovuti).replace('.', ',') # 1.2.2.1.17 Acconto Acconto = etree.SubElement(xModulo, etree.QName(NS_IV, "Acconto")) Acconto.text = "{:.2f}".format(quadro.accounto_dovuto).replace( '.', ',') # 1.2.2.1.18 ImportoDaVersare ImportoDaVersare = etree.SubElement( xModulo, etree.QName(NS_IV, "ImportoDaVersare")) ImportoDaVersare.text = "{:.2f}".format(quadro.iva_da_versare).replace( '.', ',') # 1.2.2.1.19 ImportoACredito ImportoACredito = etree.SubElement( xModulo, etree.QName(NS_IV, "ImportoACredito")) ImportoACredito.text = "{:.2f}".format(quadro.iva_a_credito).replace( '.', ',') return xModulo
def identify(**kwargs): """Create OAI-PMH response for verb Identify.""" cfg = current_app.config # add by Mr ryuu. at 2018/06/06 start # Get The Set Of Identify oaiObj = OaiIdentify.get_all() # add by Mr ryuu. at 2018/06/06 end e_tree, e_identify = verb(**kwargs) e_repositoryName = SubElement(e_identify, etree.QName(NS_OAIPMH, 'repositoryName')) # add by Mr ryuu. at 2018/06/06 start cfg['OAISERVER_REPOSITORY_NAME'] = oaiObj.repositoryName # add by Mr ryuu. at 2018/06/06 end e_repositoryName.text = cfg['OAISERVER_REPOSITORY_NAME'] e_baseURL = SubElement(e_identify, etree.QName(NS_OAIPMH, 'baseURL')) e_baseURL.text = url_for('invenio_oaiserver.response', _external=True) e_protocolVersion = SubElement(e_identify, etree.QName(NS_OAIPMH, 'protocolVersion')) e_protocolVersion.text = cfg['OAISERVER_PROTOCOL_VERSION'] # add by Mr ryuu. at 2018/06/06 start cfg['OAISERVER_ADMIN_EMAILS'][0] = oaiObj.emails # add by Mr ryuu. at 2018/06/06 end for adminEmail in cfg['OAISERVER_ADMIN_EMAILS']: e = SubElement(e_identify, etree.QName(NS_OAIPMH, 'adminEmail')) e.text = adminEmail e_earliestDatestamp = SubElement( e_identify, etree.QName(NS_OAIPMH, 'earliestDatestamp')) # update by Mr ryuu. at 2018/06/06 start # e_earliestDatestamp.text = datetime_to_datestamp( # db.session.query(db.func.min(RecordMetadata.created)).scalar() or # datetime(MINYEAR, 1, 1) # ) e_earliestDatestamp.text = datetime_to_datestamp(oaiObj.earliestDatastamp) # update by Mr ryuu. at 2018/06/06 end e_deletedRecord = SubElement(e_identify, etree.QName(NS_OAIPMH, 'deletedRecord')) e_deletedRecord.text = 'no' e_granularity = SubElement(e_identify, etree.QName(NS_OAIPMH, 'granularity')) assert cfg['OAISERVER_GRANULARITY'] in DATETIME_FORMATS e_granularity.text = cfg['OAISERVER_GRANULARITY'] compressions = cfg['OAISERVER_COMPRESSIONS'] if compressions != ['identity']: for compression in compressions: e_compression = SubElement(e_identify, etree.QName(NS_OAIPMH, 'compression')) e_compression.text = compression for description in cfg.get('OAISERVER_DESCRIPTIONS', []): e_description = SubElement(e_identify, etree.QName(NS_OAIPMH, 'description')) e_description.append(etree.fromstring(description)) return e_tree
def _build_etree(self): self._etree = etree.Element(etree.QName(self.XMLNS_IDPKG, "Story"), nsmap={"idPkg": self.XMLNS_IDPKG}) self._etree.set("DOMVersion", self.DOM_VERSION) self._add_story()
def get_namespace(message): f = BytesIO(message) for _, element in etree.iterparse(f, events=('start', )): tag = etree.QName(element) if tag.localname == 'Document': return tag.namespace
def get_child_content(self, name): """Get the content of the child.""" for child in self._element.iterchildren(): if child.tag == name or etree.QName(child.tag).localname == name: return child.text return None
'''script to test creating PREMIS metadata with lxml etree''' from lxml import etree as ET barcode = 'jjjj' attr_qname = ET.QName("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation") PREMIS_NAMESPACE = "http://www.loc.gov/premis/v3" PREMIS = "{%s}" % PREMIS_NAMESPACE NSMAP = {'premis' : PREMIS_NAMESPACE, "xsi": "http://www.w3.org/2001/XMLSchema-instance"} root = ET.Element(PREMIS + 'premis', {attr_qname: "http://www.loc.gov/premis/v3 https://www.loc.gov/standards/premis/premis.xsd"}, version="3.0", nsmap=NSMAP) object = ET.SubElement(root, PREMIS + 'object', attrib={ET.QName(NSMAP['xsi'], 'type'): 'premis:file'}) objectIdentifier = ET.SubElement(object, PREMIS + 'objectIdentifier') objectIdentifierType = ET.SubElement(objectIdentifier, PREMIS + 'objectIdentifierType') objectIdentifierType.text = 'local' objectIdentifierValue = ET.SubElement(objectIdentifier, PREMIS + 'objectIdentifierValue') objectIdentifierValue.text = barcode.get() objectCharacteristics = ET.SubElement(object, PREMIS + 'objectCharacteristics') compositionLevel = ET.SubElement(objectCharacteristics, PREMIS + 'compositionLevel') compositionLevel.text = '0' format = ET.SubElement(objectCharacteristics, PREMIS + 'format') formatDesignation = ET.SubElement(format, PREMIS + 'formatDesignation') formatName = ET.SubElement(formatDesignation, PREMIS + 'formatName') formatName.text = 'Tape Archive Format' formatRegistry = ET.SubElement(format, PREMIS + 'formatRegistry')
def qname(tag): try: return etree.QName(tag) except ValueError: prefix, base = tag.split(":") return etree.QName(NSMAP[prefix], base)
def init_can(self): attr_qname = etree.QName("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation") self.root = etree.Element( 'AUTOSAR', {attr_qname: 'http://autosar.org/schema/r4.0 AUTOSAR_4-3-0.xsd'}, nsmap={ None: 'http://autosar.org/schema/r4.0', 'xsi': 'http://www.w3.org/2001/XMLSchema-instance', }) ar_packages = etree.Element('AR-PACKAGES') self.root.append(ar_packages) ap_gen = AddPkg(ar_packages, self.gen_name, 'AR-PACKAGES') ap_sys = AddPkg(ap_gen, "SYSTEM") etree.SubElement(etree.SubElement(ap_sys, "SYSTEM"), "SHORT-NAME").text = "System" ap_cluster = AddPkg(ap_gen, self.db_name) cluster_var = AddElem(ap_cluster, self.cluster_name, "CAN-CLUSTER", "CAN-CLUSTER-VARIANTS") cluster_cond = etree.SubElement(cluster_var, "CAN-CLUSTER-CONDITIONAL") etree.SubElement(cluster_cond, "BAUDRATE").text = "250000" channels = etree.SubElement(cluster_cond, "PHYSICAL-CHANNELS") channel = etree.SubElement(channels, "CAN-PHYSICAL-CHANNEL") etree.SubElement(channel, "SHORT-NAME").text = self.channel_name comm_connectors = etree.SubElement(channel, "COMM-CONNECTORS") triggerings = etree.SubElement(channel, "FRAME-TRIGGERINGS") sig_triggerings = etree.SubElement(channel, "I-SIGNAL-TRIGGERINGS") pdu_triggerings = etree.SubElement(channel, "PDU-TRIGGERINGS") etree.SubElement(cluster_cond, "PROTOCOL-NAME").text = "CAN" etree.SubElement(cluster_cond, "SPEED").text = "250000" ecu_instances = AddPkg(ap_gen, "ECU_INSTANCES") frm_elems = AddPkg(ap_gen, "FRAME") pdu_elems = AddPkg(ap_gen, "PDUS") sig_elems = AddPkg(ap_gen, "I_SIGNALS") types = AddPkg(ap_gen, "BASE_TYPES") add_dict( types, { "SW-BASE-TYPE": [ { "SHORT-NAME": "UINT32", "BASE-TYPE-SIZE": "32", "BASE-TYPE-ENCODING": "NONE" }, { "SHORT-NAME": "SINT32", "BASE-TYPE-SIZE": "32", "BASE-TYPE-ENCODING": "2C" }, { "SHORT-NAME": "UINT16", "BASE-TYPE-SIZE": "16", "BASE-TYPE-ENCODING": "NONE" }, { "SHORT-NAME": "SINT16", "BASE-TYPE-SIZE": "16", "BASE-TYPE-ENCODING": "2C" }, { "SHORT-NAME": "UINT8", "BASE-TYPE-SIZE": "8", "BASE-TYPE-ENCODING": "NONE" }, { "SHORT-NAME": "SINT8", "BASE-TYPE-SIZE": "8", "BASE-TYPE-ENCODING": "2C" }, { "SHORT-NAME": "IEEE754", "BASE-TYPE-SIZE": "32", "BASE-TYPE-ENCODING": "IEEE754" }, ] }) ap_compumethods = AddPkg(ap_gen, "COMPUMETHODS") add_dict( ap_compumethods, { "COMPU-METHOD": { "SHORT-NAME": { 'text': "FACTOR_0_1" }, "COMPU-INTERNAL-TO-PHYS": { "COMPU-SCALES": { "COMPU-SCALE": { "COMPU-RATIONAL-COEFFS": { "COMPU-NUMERATOR": { "V": [0, 0.1] }, "COMPU-DENOMINATOR": { "V": 1 } } } } } } }) add_dict( ap_compumethods, { "COMPU-METHOD": { "SHORT-NAME": { 'text': "FACTOR_1" }, "COMPU-INTERNAL-TO-PHYS": { "COMPU-SCALES": { "COMPU-SCALE": { "COMPU-RATIONAL-COEFFS": { "COMPU-NUMERATOR": { "V": [0, 1] }, "COMPU-DENOMINATOR": { "V": 1 } } } } } } }) add_dict( ap_compumethods, { "COMPU-METHOD": { "SHORT-NAME": { 'text': "FACTOR_0_01" }, "COMPU-INTERNAL-TO-PHYS": { "COMPU-SCALES": { "COMPU-SCALE": { "COMPU-RATIONAL-COEFFS": { "COMPU-NUMERATOR": { "V": [0, 0.01] }, "COMPU-DENOMINATOR": { "V": 1 } } } } } } }) syssig_elems = AddPkg(ap_gen, "SYSTEM_SIGNALS")
def ows(request): params = {key.upper(): request.GET[key] for key in request.GET.keys()} ows_project = clean_project_name(params.get('MAP')) project, timestamp = parse_project_name(ows_project) project_hash = hashlib.md5(project.encode('utf-8')).hexdigest() pi = get_project_info(project_hash, timestamp, project=ows_project) if not request.user.is_authenticated: basic_auth.is_authenticated(request) if not check_project_access(request, project, pi['authentication']): if not request.user.is_authenticated: response = HttpResponse('Authentication required', status=401) response['WWW-Authenticate'] = 'Basic realm=OWS API' return response raise PermissionDenied if params.get( 'SERVICE') == 'WFS' and params.get('REQUEST') != 'GetFeature': access_control = pi.get('access_control') if access_control and access_control['enabled']: root = etree.fromstring(request.body.decode()) for elem in root.findall('.//{*}Insert'): for child in elem.getchildren(): layer_name = etree.QName(child).localname if not check_layer_access(request.user, access_control, layer_name, 'insert'): raise PermissionDenied checks = [('.//{*}Update', 'update'), ('.//{*}Delete', 'delete')] for query_path, permission in checks: for elem in root.findall(query_path): layer_name = elem.get('typeName').split(':')[-1] if not check_layer_access(request.user, access_control, layer_name, permission): raise PermissionDenied url = "{0}?{1}".format(settings.GISQUICK_MAPSERVER_URL.rstrip("/"), request.environ['QUERY_STRING']) abs_project = abs_project_path(params.get('MAP')) url = set_query_parameters(url, {'MAP': abs_project}) if request.method == 'POST': owsrequest = urllib.request.Request(url, request.body) else: owsrequest = urllib.request.Request(url) owsrequest.add_header("User-Agent", "Gisquick") resp_content = b"" try: with contextlib.closing(urllib.request.urlopen(owsrequest)) as resp: while True: data = resp.read() if not data: break resp_content += data if params.get('REQUEST', '') == 'GetCapabilities': resp_content = resp_content.replace( settings.GISQUICK_MAPSERVER_URL.encode(), request.build_absolute_uri(request.path).encode()) content_type = resp.getheader('Content-Type') status = resp.getcode() return HttpResponse(resp_content, content_type=content_type, status=status) except urllib.error.HTTPError as e: # reason = e.read().decode("utf8") return HttpResponse(e.read(), content_type=e.headers.get_content_type(), status=e.code)
#!/usr/bin/env python import sys from lxml import etree as et HTML_NS = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" XSL_NS = "http://www.w3.org/1999/XSL/Transform" NS_MAP = {None: HTML_NS, "xsl": XSL_NS} rootName = et.QName(XSL_NS, 'stylesheet') root = et.Element(rootName, nsmap=NS_MAP) sheet = et.ElementTree(root) top = et.SubElement(root, et.QName(XSL_NS, "template"), match='/') html = et.SubElement(top, et.QName(HTML_NS, "html")) head = et.SubElement(html, "head") title = et.SubElement(head, "title") title.text = "Heading title" body = et.SubElement(html, "body") h1 = et.SubElement(body, "h1") h1.text = "Body heading" p = et.SubElement(body, "p") p.text = "Paragraph text" sheet_str = et.tostring(sheet) print(sheet_str)
def __init__(self, qname=None, is_global=False): super(AnySimpleType, self).__init__( qname or etree.QName(self._default_qname), is_global)
def _export_xml_get_fornitura(self): x1_Fornitura = etree.Element(etree.QName(NS_IV, "Fornitura"), nsmap=NS_MAP) return x1_Fornitura
def visit_attribute(self, node, parent): """Declares an attribute. Definition:: <attribute default = string fixed = string form = (qualified | unqualified) id = ID name = NCName ref = QName type = QName use = (optional | prohibited | required): optional {any attributes with non-schema Namespace...}> Content: (annotation?, (simpleType?)) </attribute> :param node: The XML node :type node: lxml.etree._Element :param parent: The parent XML node :type parent: lxml.etree._Element """ is_global = parent.tag == tags.schema # Check of wsdl:arayType array_type = node.get('{http://schemas.xmlsoap.org/wsdl/}arrayType') if array_type: match = re.match('([^\[]+)', array_type) if match: array_type = match.groups()[0] qname = as_qname(array_type, node.nsmap) array_type = UnresolvedType(qname, self.schema) # If the elment has a ref attribute then all other attributes cannot # be present. Short circuit that here. # Ref is prohibited on global elements (parent = schema) if not is_global: result = self.process_ref_attribute(node, array_type=array_type) if result: return result attribute_form = node.get('form', self.document._attribute_form) if attribute_form == 'qualified' or is_global: name = qname_attr(node, 'name', self.document._target_namespace) else: name = etree.QName(node.get('name')) annotation, items = self._pop_annotation(node.getchildren()) if items: xsd_type = self.visit_simple_type(items[0], node) else: node_type = qname_attr(node, 'type') if node_type: xsd_type = self._get_type(node_type) else: xsd_type = xsd_types.AnyType() # TODO: We ignore 'prohobited' for now required = node.get('use') == 'required' default = node.get('default') attr = xsd_elements.Attribute(name, type_=xsd_type, default=default, required=required) # Only register global elements if is_global: self.register_attribute(name, attr) return attr
def _export_xml_get_frontespizio(self): x1_2_1_Frontespizio = etree.Element(etree.QName(NS_IV, "Frontespizio")) # Codice Fiscale x1_2_1_1_CodiceFiscale = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "CodiceFiscale")) x1_2_1_1_CodiceFiscale.text = unicode(self.taxpayer_fiscalcode) \ if self.taxpayer_fiscalcode else '' # Anno Imposta x1_2_1_2_AnnoImposta = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "AnnoImposta")) x1_2_1_2_AnnoImposta.text = str(self.year) # Partita IVA x1_2_1_3_PartitaIVA = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "PartitaIVA")) x1_2_1_3_PartitaIVA.text = self.taxpayer_vat # PIVA Controllante if self.controller_vat: x1_2_1_4_PIVAControllante = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "PIVAControllante")) x1_2_1_4_PIVAControllante.text = self.controller_vat # Ultimo Mese if self.last_month: x1_2_1_5_UltimoMese = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "UltimoMese")) x1_2_1_5_UltimoMese.text = self.last_month # Liquidazione Gruppo x1_2_1_6_LiquidazioneGruppo = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "LiquidazioneGruppo")) x1_2_1_6_LiquidazioneGruppo.text = \ '1' if self.liquidazione_del_gruppo else '0' # CF Dichiarante if self.declarant_fiscalcode: x1_2_1_7_CFDichiarante = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "CFDichiarante")) x1_2_1_7_CFDichiarante.text = self.declarant_fiscalcode # CodiceCaricaDichiarante if self.codice_carica_id: x1_2_1_8_CodiceCaricaDichiarante = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "CodiceCaricaDichiarante")) x1_2_1_8_CodiceCaricaDichiarante.text = self.codice_carica_id.code # CodiceFiscaleSocieta if self.declarant_fiscalcode_company: x1_2_1_9_CodiceFiscaleSocieta = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "CodiceFiscaleSocieta")) x1_2_1_9_CodiceFiscaleSocieta.text =\ self.declarant_fiscalcode_company.code # FirmaDichiarazione x1_2_1_10_FirmaDichiarazione = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "FirmaDichiarazione")) x1_2_1_10_FirmaDichiarazione.text = '1' if self.declarant_sign else '0' # CFIntermediario if self.delegate_fiscalcode: x1_2_1_11_CFIntermediario = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "CFIntermediario")) x1_2_1_11_CFIntermediario.text = self.delegate_fiscalcode # ImpegnoPresentazione if self.delegate_commitment: x1_2_1_12_ImpegnoPresentazione = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "ImpegnoPresentazione")) x1_2_1_12_ImpegnoPresentazione.text = self.delegate_commitment # DataImpegno if self.date_commitment: x1_2_1_13_DataImpegno = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "DataImpegno")) x1_2_1_13_DataImpegno.text = datetime.strptime( self.date_commitment, "%Y-%m-%d").strftime('%d%m%Y') # FirmaIntermediario if self.delegate_fiscalcode: x1_2_1_14_FirmaIntermediario = etree.SubElement( x1_2_1_Frontespizio, etree.QName(NS_IV, "FirmaIntermediario")) x1_2_1_14_FirmaIntermediario.text =\ '1' if self.delegate_sign else '0' return x1_2_1_Frontespizio
def parse(cls, definitions, xmlelement, binding, nsmap): """ Definition:: <wsdl:operation name="nmtoken"> * <soap:operation soapAction="uri"? style="rpc|document"?>? <wsdl:input name="nmtoken"? > ? <soap:body use="literal"/> </wsdl:input> <wsdl:output name="nmtoken"? > ? <-- extensibility element (4) --> * </wsdl:output> <wsdl:fault name="nmtoken"> * <-- extensibility element (5) --> * </wsdl:fault> </wsdl:operation> Example:: <wsdl:operation name="GetLastTradePrice"> <soap:operation soapAction="http://example.com/GetLastTradePrice"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> </wsdl:output> <wsdl:fault name="dataFault"> <soap:fault name="dataFault" use="literal"/> </wsdl:fault> </operation> """ name = xmlelement.get('name') # The soap:operation element is required for soap/http bindings # and may be omitted for other bindings. soap_node = xmlelement.find('soap:operation', namespaces=binding.nsmap) action = None if soap_node is not None: action = soap_node.get('soapAction') style = soap_node.get('style', binding.default_style) else: style = binding.default_style obj = cls(name, binding, nsmap, action, style) if style == 'rpc': message_class = RpcMessage else: message_class = DocumentMessage for node in xmlelement: tag_name = etree.QName(node.tag).localname if tag_name not in ('input', 'output', 'fault'): continue msg = message_class.parse(definitions=definitions, xmlelement=node, operation=obj, nsmap=nsmap, type=tag_name) if tag_name == 'fault': obj.faults[msg.name] = msg else: setattr(obj, tag_name, msg) return obj
def _serialize_feature_structure(self, cas: Cas, root: etree.Element, fs: FeatureStructure): # The type name is a Java package, e.g. `org.myproj.Foo`. parts = fs.type.split(".") # The CAS type namespace is converted to an XML namespace URI by the following rule: # replace all dots with slashes, prepend http:///, and append .ecore. url = "http:///" + "/".join(parts[:-1]) + ".ecore" # The cas prefix is the last component of the CAS namespace, which is the second to last # element of the type (the last part is the type name without package name), e.g. `myproj` raw_prefix = parts[-2] typename = parts[-1] # If the url has not been seen yet, compute the namespace and add it if url not in self._urls_to_prefixes: # If the prefix already exists, but maps to a different url, then add it with # a number at the end, e.g. `type0` new_prefix = raw_prefix if raw_prefix in self._nsmap: suffix = self._duplicate_namespaces[raw_prefix] self._duplicate_namespaces[raw_prefix] += 1 new_prefix = raw_prefix + str(suffix) self._nsmap[new_prefix] = url self._urls_to_prefixes[url] = new_prefix prefix = self._urls_to_prefixes[url] name = etree.QName(self._nsmap[prefix], typename) elem = etree.SubElement(root, name) # Serialize common attributes elem.attrib["{http://www.omg.org/XMI}id"] = str(fs.xmiID) # Serialize feature attributes t = cas.typesystem.get_type(fs.type) for feature in t.all_features: feature_name = feature.name if feature_name in CasXmiSerializer._COMMON_FIELD_NAMES: continue # Skip over 'None' features value = getattr(fs, feature_name) if value is None: continue if fs.type == "uima.cas.StringArray": # String arrays need to be serialized to a series of child elements, as strings can # contain whitespaces. Consider e.g. the array ['likes cats, 'likes dogs']. If we would # serialize it as an attribute, it would look like # # <my:fs elements="likes cats likes dogs" /> # # which looses the information about the whitespace. Instead, we serialize it to # # <my:fs> # <elements>likes cats</elements> # <elements>likes dogs</elements> # </my:fs> for e in value: child = etree.SubElement(elem, feature_name) child.text = e elif feature_name == "sofa" or cas.typesystem.is_primitive( feature.rangeTypeName): elem.attrib[feature_name] = str(value) elif cas.typesystem.is_collection(feature.rangeTypeName): elements = " ".join(str(e.xmiID) for e in value) elem.attrib[feature_name] = elements else: # We need to encode non-primitive features as a reference elem.attrib[feature_name] = str(value.xmiID)