def test_load_xml_resource(self): self.assertTrue(is_etree_element(load_xml_resource(self.vh_xml_file, element_only=True))) root, text, url = load_xml_resource(self.vh_xml_file, element_only=False) self.assertTrue(is_etree_element(root)) self.assertEqual(root.tag, '{http://example.com/vehicles}vehicles') self.assertTrue(text.startswith('<?xml version')) self.check_url(url, self.vh_xml_file)
def test_load_xml_resource(self): self.assertTrue( is_etree_element( load_xml_resource(self.vh_xml_file, element_only=True))) root, text, url = load_xml_resource(self.vh_xml_file, element_only=False) self.assertTrue(is_etree_element(root)) self.assertEqual(root.tag, '{http://example.com/vehicles}vehicles') self.assertTrue(text.startswith('<?xml version')) self.check_url(url, self.vh_xml_file)
def load_xml(xml_document): try: xml_root = xml_document.getroot() except (AttributeError, TypeError): if is_etree_element(xml_document): xml_root = xml_document else: xml_root = load_xml_resource(xml_document) else: if not is_etree_element(xml_root): raise XMLSchemaValidationError( "wrong type %r for 'xml_document' argument." % type(xml_document) ) return xml_root
def check_encode(self, xsd_component, data, expected, **kwargs): if isinstance(expected, type) and issubclass(expected, Exception): self.assertRaises(expected, xsd_component.encode, data, **kwargs) elif is_etree_element(expected): elem = xsd_component.encode(data, **kwargs) self.check_etree_elements(expected, elem) else: obj = xsd_component.encode(data, **kwargs) if isinstance(obj, tuple) and len(obj) == 2 and isinstance(obj[1], list): self.assertEqual(expected, obj[0]) self.assertTrue(isinstance(obj[0], type(expected))) elif is_etree_element(obj): self.assertEqual(expected, etree_tostring(obj).strip()) else: self.assertEqual(expected, obj) self.assertTrue(isinstance(obj, type(expected)))
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 test_max_occurs_sequence(self): # Issue #119 schema = self.get_schema(""" <xs:element name="foo"> <xs:complexType> <xs:sequence> <xs:element name="A" type="xs:integer" maxOccurs="2" /> </xs:sequence> </xs:complexType> </xs:element>""") # Check validity self.assertIsNone(schema.validate("<foo><A>1</A></foo>")) self.assertIsNone(schema.validate("<foo><A>1</A><A>2</A></foo>")) with self.assertRaises(XMLSchemaChildrenValidationError): schema.validate("<foo><A>1</A><A>2</A><A>3</A></foo>") self.assertTrue(is_etree_element(schema.to_etree({'A': 1}, path='foo'))) self.assertTrue( is_etree_element(schema.to_etree({'A': [1]}, path='foo'))) self.assertTrue( is_etree_element(schema.to_etree({'A': [1, 2]}, path='foo'))) with self.assertRaises(XMLSchemaChildrenValidationError): schema.to_etree({'A': [1, 2, 3]}, path='foo') schema = self.get_schema(""" <xs:element name="foo"> <xs:complexType> <xs:sequence> <xs:element name="A" type="xs:integer" maxOccurs="2" /> <xs:element name="B" type="xs:integer" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element>""") self.assertTrue( is_etree_element(schema.to_etree({'A': [1, 2]}, path='foo'))) with self.assertRaises(XMLSchemaChildrenValidationError): schema.to_etree({'A': [1, 2, 3]}, path='foo')
def test_xml_document_etree_interface(self): xml_document = XmlDocument(self.vh_xml_file) self.assertIs(xml_document.getroot(), xml_document._root) self.assertTrue(is_etree_element(xml_document.getroot())) self.assertTrue(is_etree_document(xml_document.get_etree_document())) xml_document = XmlDocument(self.vh_xml_file, lazy=1) with self.assertRaises(XMLResourceError) as ctx: xml_document.get_etree_document() self.assertIn('cannot create an ElementTree from a lazy resource', str(ctx.exception)) vh_tree = ElementTree.parse(self.vh_xml_file) xml_document = XmlDocument(vh_tree, base_url=self.vh_dir) self.assertIs(xml_document.source, vh_tree) self.assertIs(xml_document.get_etree_document(), vh_tree)