def test_local_name_functions(self): self.assertEqual(local_name(XSD_SCHEMA), 'schema') self.assertEqual(local_name('schema'), 'schema') self.assertEqual(local_name(''), '') self.assertEqual(local_name(None), None) self.assertRaises(ValueError, local_name, '{ns name') self.assertRaises(TypeError, local_name, 1.0) self.assertRaises(TypeError, local_name, 0)
def test_decode_encode(self): """Test encode after a decode, checking the re-encoded tree.""" filename = self.casepath('examples/collection/collection.xml') xt = ElementTree.parse(filename) xd = self.col_schema.to_dict(filename) elem = self.col_schema.encode(xd, path='./col:collection', namespaces=self.col_namespaces) self.assertEqual( len([e for e in elem.iter()]), 20, msg="The encoded tree must have 20 elements as the origin." ) self.assertTrue(all( local_name(e1.tag) == local_name(e2.tag) for e1, e2 in zip(elem.iter(), xt.getroot().iter()) ))
def _parse_content_model(self, elem, content_model): self.model = local_name(content_model.tag) if self.model == 'all': if self.max_occurs != 1: self.parse_error("maxOccurs must be (0 | 1) for 'all' model groups") if self.min_occurs not in (0, 1): self.parse_error("minOccurs must be (0 | 1) for 'all' model groups") for child in self._iterparse_components(content_model): if child.tag == XSD_ELEMENT: # Builds inner elements and reference groups later, for avoids circularity. self.append((child, self.schema)) elif child.tag == XSD_ANY: self.append(XsdAnyElement(child, self.schema, self)) elif child.tag in (XSD_SEQUENCE, XSD_CHOICE, XSD_ALL): self.append(XsdGroup(child, self.schema, self)) elif child.tag == XSD_GROUP: try: ref = self.schema.resolve_qname(child.attrib['ref']) except KeyError: self.parse_error("missing attribute 'ref' in local group", child) continue if ref != self.name: self.append(XsdGroup(child, self.schema, self)) elif self.redefine is None: self.parse_error("Circular definition detected for group %r:" % self.ref, elem) else: if child.get('minOccurs', '1') != '1' or child.get('maxOccurs', '1') != '1': self.parse_error( "Redefined group reference cannot have minOccurs/maxOccurs other than 1:", elem ) self.append(self.redefine) else: continue # Error already caught by validation against the meta-schema
def _parse_content_model(self, elem, content_model): self.model = local_name(content_model.tag) if self.model == 'all': if self.max_occurs != 1: self.parse_error("maxOccurs must be 1 for 'all' model groups") if self.min_occurs not in (0, 1): self.parse_error("minOccurs must be (0 | 1) for 'all' model groups") for child in self._iterparse_components(content_model): if child.tag == XSD_ELEMENT: # Builds inner elements and reference groups later, for avoids circularity. self.append((child, self.schema)) elif content_model.tag == XSD_ALL: self.parse_error("'all' model can contains only elements.", elem) elif child.tag == XSD_ANY: self.append(XsdAnyElement(child, self.schema, self)) elif child.tag in (XSD_SEQUENCE, XSD_CHOICE): self.append(XsdGroup(child, self.schema, self)) elif child.tag == XSD_GROUP: xsd_group = XsdGroup(child, self.schema, self) if xsd_group.name != self.name: self.append(xsd_group) elif not hasattr(self, '_elem'): self.parse_error("Circular definitions detected for group %r:" % self.ref, elem) else: continue # Error already caught by validation against the meta-schema
def _parse_content_model(self, elem, content_model): self.model = local_name(content_model.tag) if self.model == 'all': if self.max_occurs != 1: self.parse_error( "maxOccurs must be (0 | 1) for 'all' model groups") if self.min_occurs not in (0, 1): self.parse_error( "minOccurs must be (0 | 1) for 'all' model groups") for child in self._iterparse_components(content_model): if child.tag == XSD_ELEMENT: # Builds inner elements and reference groups later, for avoids circularity. self.append((child, self.schema)) elif child.tag == XSD_ANY: self.append(XsdAnyElement(child, self.schema, self)) elif child.tag in (XSD_SEQUENCE, XSD_CHOICE, XSD_ALL): self.append(XsdGroup(child, self.schema, self)) elif child.tag == XSD_GROUP: xsd_group = XsdGroup(child, self.schema, self) if xsd_group.name != self.name: self.append(xsd_group) elif not hasattr(self, '_elem'): self.parse_error( "Circular definitions detected for group %r:" % self.ref, elem) else: continue # Error already caught by validation against the meta-schema
def test_local_name(self): self.assertEqual(local_name('element'), 'element') self.assertEqual(local_name(XSD_ELEMENT), 'element') self.assertEqual(local_name('xs:element'), 'element') self.assertEqual(local_name(XSD_SCHEMA), 'schema') self.assertEqual(local_name('schema'), 'schema') self.assertEqual(local_name(''), '') self.assertEqual(local_name(None), None) self.assertRaises(ValueError, local_name, '{ns name') self.assertRaises(TypeError, local_name, 1.0) self.assertRaises(TypeError, local_name, 0)
def _parse_content_model(self, elem, content_model): self.model = local_name(content_model.tag) if self.model == 'all': if self.max_occurs != 1: self.parse_error("maxOccurs must be 1 for 'all' model groups") if self.min_occurs not in (0, 1): self.parse_error("minOccurs must be (0 | 1) for 'all' model groups") for child in filter(lambda x: x.tag != XSD_ANNOTATION, content_model): if child.tag == XSD_ELEMENT: # Builds inner elements and reference groups later, for avoids circularity. self.append((child, self.schema)) elif content_model.tag == XSD_ALL: self.parse_error("'all' model can contains only elements.", elem) elif child.tag == XSD_ANY: self.append(XsdAnyElement(child, self.schema, self)) elif child.tag in (XSD_SEQUENCE, XSD_CHOICE): self.append(XsdGroup(child, self.schema, self)) elif child.tag == XSD_GROUP: try: ref = self.schema.resolve_qname(child.attrib['ref']) except (KeyError, ValueError, RuntimeError) as err: if 'ref' not in child.attrib: self.parse_error("missing attribute 'ref' in local group", child) else: self.parse_error(err, child) continue if ref != self.name: xsd_group = XsdGroup(child, self.schema, self) if xsd_group.model == 'all': self.parse_error("'all' model can appears only at 1st level of a model group") else: self.append(xsd_group) elif self.redefine is None: self.parse_error("Circular definition detected for group %r:" % self.name, elem) else: if child.get('minOccurs', '1') != '1' or child.get('maxOccurs', '1') != '1': self.parse_error( "Redefined group reference cannot have minOccurs/maxOccurs other than 1:", elem ) self.append(self.redefine) else: continue # Error already caught by validation against the meta-schema