def test_parse_target_namespace(self): name = '{%s}motorbikes' % self.schema.target_namespace elem = ElementTree.Element(XSD_ELEMENT, name=name, targetNamespace='tns0') group = self.schema.elements['vehicles'].type.content_type xsd_element = self.FakeElement(elem=elem, name=name, schema=self.schema, parent=None) with self.assertRaises(XMLSchemaParseError) as ctx: xsd_element._parse_target_namespace() self.assertIn("must have the same namespace", ctx.exception.message) xsd_element = self.FakeElement(elem=elem, name=name, schema=self.schema, parent=group) self.assertIsNone(xsd_element._parse_target_namespace()) self.assertEqual(xsd_element.name, '{tns0}motorbikes') elem = ElementTree.Element(XSD_ELEMENT, targetNamespace='tns0') xsd_element = self.FakeElement(elem=elem, name=None, schema=self.schema, parent=group) with self.assertRaises(XMLSchemaParseError) as ctx: xsd_element._parse_target_namespace() self.assertIn("attribute 'name' must be present", ctx.exception.message) elem = ElementTree.Element(XSD_ELEMENT, name=name, form='qualified', targetNamespace='tns0') xsd_element = self.FakeElement(elem=elem, name=name, schema=self.schema, parent=group) with self.assertRaises(XMLSchemaParseError) as ctx: xsd_element._parse_target_namespace() self.assertIn("attribute 'form' must be absent", ctx.exception.message) elem = ElementTree.Element( XSD_ELEMENT, name='motobikes', targetNamespace=self.schema.target_namespace ) xsd_element = self.FakeElement(elem, self.schema, parent=group, name=name) self.assertIsNone(xsd_element._parse_target_namespace()) self.assertEqual(xsd_element.name, name) xsd_attribute = self.schema.types['vehicleType'].attributes['model'] xsd_attribute.elem.attrib['targetNamespace'] = 'tns0' with self.assertRaises(XMLSchemaParseError) as ctx: xsd_attribute._parse_target_namespace() self.assertIn("a declaration contained in a global complexType must " "have the same namespace", ctx.exception.message) del xsd_attribute.elem.attrib['targetNamespace'] with self.assertRaises(XMLSchemaParseError) as ctx: XMLSchema11("""<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root" targetNamespace=""/> </xs:schema>""") self.assertIn("use of attribute 'targetNamespace' is prohibited", ctx.exception.message) schema = XMLSchema11("""<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root"> <xs:complexType> <xs:sequence> <xs:element name="node" targetNamespace=""/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>""") self.assertEqual(schema.elements['root'].type.content_type[0].name, 'node')
def test_parse_xpath_default_namespace(self): xsd_text = """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root"/> </xs:schema>""" schema = XMLSchema11(xsd_text) elem = ElementTree.Element('A') self.assertEqual(schema._parse_xpath_default_namespace(elem), '') elem = ElementTree.Element('A', xpathDefaultNamespace='##local') self.assertEqual(schema._parse_xpath_default_namespace(elem), '') elem = ElementTree.Element('A', xpathDefaultNamespace='##defaultNamespace') self.assertEqual(schema._parse_xpath_default_namespace(elem), '') elem = ElementTree.Element('A', xpathDefaultNamespace='##targetNamespace') self.assertEqual(schema._parse_xpath_default_namespace(elem), '') xsd_text = """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="tns0" targetNamespace="tns0"> <xs:element name="root"/> </xs:schema>""" schema = XMLSchema11(xsd_text, validation='lax') elem = ElementTree.Element('A') self.assertEqual(schema._parse_xpath_default_namespace(elem), '') elem = ElementTree.Element('A', xpathDefaultNamespace='##local') self.assertEqual(schema._parse_xpath_default_namespace(elem), '') elem = ElementTree.Element('A', xpathDefaultNamespace='##defaultNamespace') self.assertEqual(schema._parse_xpath_default_namespace(elem), 'tns0') elem = ElementTree.Element('A', xpathDefaultNamespace='##targetNamespace') self.assertEqual(schema._parse_xpath_default_namespace(elem), 'tns0') elem = ElementTree.Element('A', xpathDefaultNamespace='tns1') self.assertEqual(schema._parse_xpath_default_namespace(elem), 'tns1') elem = ElementTree.Element('A', xpathDefaultNamespace='tns0 tns1') self.assertEqual(schema._parse_xpath_default_namespace(elem), '') self.assertIn('tns0 tns1', schema.errors[-1].message)