def test_namespace_manager(self): xml = '''<?xml version="1.0" encoding="UTF-8"?> <a:foo xmlns:a="http://example/test" xmlns:b="http://example2/test2"> <a:cat /> <b:dog /> <a:mouse><a:bat /></a:mouse> </a:foo> ''' root = cElementTree.fromstring(xml) manager = XmlNamespaceManager() manager.add_namespace('http://example/test') tags = [] for element in manager.iterate_children(root): tags.append(element.tag) expected_tags = [ '{http://example/test}cat', '{http://example/test}mouse', ] self.assertEqual(tags, expected_tags) manager.add_namespace('http://example2/test2') tags = [] for element in manager.iterate_children(root): tags.append(element.tag) expected_tags = [ '{http://example/test}cat', '{http://example2/test2}dog', '{http://example/test}mouse', ] self.assertEqual(tags, expected_tags) manager = XmlNamespaceManager() manager.add_namespace('http://example2/test2') tags = [] for element in manager.iterate_children(root): tags.append(element.tag) expected_tags = [ '{http://example2/test2}dog', ] self.assertEqual(tags, expected_tags)
def test_namespace_manager(self): xml = '''<?xml version="1.0" encoding="UTF-8"?> <a:foo xmlns:a="http://example/test" xmlns:b="http://example2/test2"> <a:cat /> <b:dog /> <a:mouse><a:bat /></a:mouse> </a:foo> ''' root = cElementTree.fromstring(xml) manager = XmlNamespaceManager() manager.add_namespace('http://example/test') tags = [] for element in manager.iterate_children(root): tags.append(element.tag) expected_tags = [ '{http://example/test}cat', '{http://example/test}mouse', ] self.assertEqual(tags, expected_tags) manager.add_namespace('http://example2/test2') tags = [] for element in manager.iterate_children(root): tags.append(element.tag) expected_tags = [ '{http://example/test}cat', '{http://example2/test2}dog', '{http://example/test}mouse', ] self.assertEqual(tags, expected_tags) manager = XmlNamespaceManager() manager.add_namespace('http://example2/test2') tags = [] for element in manager.iterate_children(root): tags.append(element.tag) expected_tags = [ '{http://example2/test2}dog', ] self.assertEqual(tags, expected_tags)
def _load_relationships(self): part_container = self.get_part_container() if not part_container.part_exists(self.relationship_uri): return manager = XmlNamespaceManager() manager.add_namespace(PackageRelationship.namespace) stream = part_container.get_part(self.relationship_uri).stream root = cElementTree.fromstring(stream.read()) for node in manager.iterate_children(root): _, tag = xml_tag_split(node.tag) if tag != PackageRelationship.XML_TAG_NAME: continue relationship_id = node.get(PackageRelationship.XML_ATTR_ID) relationship_type = node.get(PackageRelationship.XML_ATTR_TYPE) target_mode = node.get(PackageRelationship.XML_ATTR_TARGETMODE) target_uri = node.get(PackageRelationship.XML_ATTR_TARGET) self.create_relationship( target_uri=target_uri, target_mode=target_mode, relationship_type=relationship_type, relationship_id=relationship_id, )
def _load_relationships(self): part_container = self.get_part_container() if not part_container.part_exists(self.relationship_uri): return manager = XmlNamespaceManager() manager.add_namespace(PackageRelationship.namespace) stream = part_container.get_part(self.relationship_uri).stream root = cElementTree.fromstring(stream.read()) for node in manager.iterate_children(root): _, tag = xml_tag_split(node.tag) if tag != PackageRelationship.XML_TAG_NAME: continue relationship_id = node.get(PackageRelationship.XML_ATTR_ID) relationship_type = node.get(PackageRelationship.XML_ATTR_TYPE) target_mode = node.get(PackageRelationship.XML_ATTR_TARGETMODE) target_uri = node.get(PackageRelationship.XML_ATTR_TARGET) self.create_relationship( target_uri=target_uri, target_mode=target_mode, relationship_type=relationship_type, relationship_id=relationship_id, )