def as_dict(self, fqn_keys=False): if fqn_keys: return { k: v for k, v in self.target_dict.items() if self.namespace == get_namespace(k) } else: return { k if k[0] != '{' else k[k.rindex('}') + 1:]: v for k, v in self.target_dict.items() if self.namespace == get_namespace(k) }
def is_matching(self, name, default_namespace=None): if name is None: return False elif not name or name[0] == '{': return self.is_namespace_allowed(get_namespace(name)) elif default_namespace is None: return self.is_namespace_allowed('') else: return self.is_namespace_allowed(default_namespace)
def iter_encode(self, attrs, validation='lax', **kwargs): result_list = [] required_attributes = {a for a in self.iter_required()} try: attrs = attrs.items() except AttributeError: pass for name, value in attrs: try: xsd_attribute = self[name] except KeyError: namespace = get_namespace(name) or self.target_namespace if namespace == XSI_NAMESPACE: try: xsd_attribute = self.maps.lookup_attribute(name) except LookupError: if validation != 'skip': reason = "%r is not an attribute of the XSI namespace." % name yield self.validation_error( validation, reason, attrs, **kwargs) continue else: try: xsd_attribute = self[None] # None key ==> anyAttribute value = (name, value) except KeyError: if validation != 'skip': reason = "%r attribute not allowed for element." % name yield self.validation_error( validation, reason, attrs, **kwargs) continue else: required_attributes.discard(name) for result in xsd_attribute.iter_encode(value, validation, **kwargs): if isinstance(result, XMLSchemaValidationError): yield result else: result_list.append((name, result)) break if required_attributes and validation != 'skip': reason = "missing required attributes %r" % required_attributes yield self.validation_error(validation, reason, attrs, **kwargs) yield result_list
def map_qname(self, qname): try: if qname[0] != '{' or not self._namespaces: return qname except IndexError: return qname qname_uri = get_namespace(qname) for prefix, uri in self.items(): if uri != qname_uri: continue if prefix: self._namespaces[prefix] = uri return qname.replace(u'{%s}' % uri, u'%s:' % prefix) else: if uri: self._namespaces[prefix] = uri return qname.replace(u'{%s}' % uri, '') else: return qname
def iter_encode(self, obj, validation='lax', converter=None, **kwargs): if self.process_contents == 'skip': return name, value = obj namespace = get_namespace(name) if self.is_namespace_allowed(namespace): self._load_namespace(namespace) try: xsd_element = self.maps.lookup_element(name) except LookupError: if self.process_contents == 'strict' and validation != 'skip': reason = "element %r not found." % name yield self.validation_error(validation, reason, **kwargs) else: for result in xsd_element.iter_encode(value, validation, converter, **kwargs): yield result elif validation != 'skip': reason = "element %r not allowed here." % name yield self.validation_error(validation, reason, value, **kwargs)
def retrieve_schema_source(self, source): """ Returns a schema source that can be used to create an XMLSchema instance. :param source: A string or an ElementTree's Element. :return: An schema source string, an ElementTree's Element or a full pathname. """ if is_etree_element(source): if source.tag in (XSD_SCHEMA, 'schema'): return source elif get_namespace(source.tag): raise XMLSchemaValueError( "source %r namespace has to be empty." % source) elif source.tag not in { 'element', 'attribute', 'simpleType', 'complexType', 'group', 'attributeGroup', 'notation' }: raise XMLSchemaValueError( "% is not an XSD global definition/declaration." % source) root = etree_element('schema', attrib={ 'xmlns:ns': "ns", 'xmlns': "http://www.w3.org/2001/XMLSchema", 'targetNamespace': "ns", 'elementFormDefault': "qualified", 'version': self.schema_class.XSD_VERSION, }) root.append(source) return root else: source = source.strip() if not source.startswith('<'): return self.casepath(source) else: return self.SCHEMA_TEMPLATE.format( self.schema_class.XSD_VERSION, source)
def iter_encode(self, attribute, validation='lax', **kwargs): if self.process_contents == 'skip': return name, value = attribute namespace = get_namespace(name) if self.is_namespace_allowed(namespace): self._load_namespace(namespace) try: xsd_attribute = self.maps.lookup_attribute(name) except LookupError: if self.process_contents == 'strict' and validation != 'skip': reason = "attribute %r not found." % name yield self.validation_error(validation, reason, attribute, **kwargs) else: for result in xsd_attribute.iter_encode( value, validation, **kwargs): yield result elif validation != 'skip': reason = "attribute %r not allowed." % name yield self.validation_error(validation, reason, attribute, **kwargs)
def test_get_namespace_function(self): self.assertEqual(get_namespace(XSD_SIMPLE_TYPE), XSD_NAMESPACE) self.assertEqual(get_namespace(''), '') self.assertEqual(get_namespace(None), '')
def namespace(self): """The namespace of the XML document.""" return get_namespace(self._root.tag) if self._root is not None else None