def upgrade_rating(root, data): rating = None for meta in XPath('./opf:metadata/opf:meta[@name="calibre:rating"]')(root): remove_element(meta, data.refines) rating = meta.get('content') if rating is not None: create_rating(root, data.prefixes, rating)