def __set__(self, instance, value): recipe_categories = instance.xml.xpath('./head/recipe_categories') if len(recipe_categories) != 0: instance.xml.head.remove(recipe_categories[0]) value = self._remove_duplicates(value) if len(value) > 0: el = E.recipe_categories() for item in value: el.append(E.category(code=item.code)) instance.xml.head.append(el)