From 9a9df79aaa5d83cca17257e2db35e6eeb688db12 Mon Sep 17 00:00:00 2001 From: kthoden Date: Fri, 19 Jul 2019 13:49:22 +0200 Subject: [PATCH] Resolve choice element --- tei2imxml.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tei2imxml.py b/tei2imxml.py index ac14aed..bae5610 100755 --- a/tei2imxml.py +++ b/tei2imxml.py @@ -422,6 +422,24 @@ def hi_lookup(hi_id): return hi_code # def hi_lookup ends here + +def resolve_choice_to_expan(element_containing_choice, original_element): + """Check whether an element contains choice and if so return expanded version.""" + + element_children = element_containing_choice.getchildren() + if element_children and element_children[0].tag == "{" + ns_tei + "}choice": + cleaned_element = element_children[0].find("t:expan", namespaces=NS_MAP) + cleaned_element.tag = original_element + element_containing_choice.insert(-1, cleaned_element) + element_containing_choice.tag = "tagtobestripped" + etree.strip_elements(element_containing_choice, "{%s}choice" % ns_tei) + else: + cleaned_element = element_containing_choice + + return cleaned_element +# def resolve_choice_to_expan ends here + + def transform_body(xml_tree, cited_data, translation_file, publang, hyperimage=False): """Transform the body of XML document into IntermediateXML file""" @@ -470,7 +488,8 @@ def handle_refs_hyperimage(ref): chapter.tag = "div1" chapter.set("language", publang) - chapter_title = chapter.find("t:head", namespaces=NS_MAP) + chapter_title_element = chapter.find("t:head", namespaces=NS_MAP) + chapter_title = resolve_choice_to_expan(chapter_title_element, "head") author_ids = chapter.get("resp") if author_ids is not None: @@ -483,7 +502,7 @@ def handle_refs_hyperimage(ref): chapter_title.insert(0, eoa_author) else: logging.info("No chapter author.") - chapter.insert(0, chapter_title) + chapter.insert(0, chapter_title_element) eoa_sections = xml_tree.xpath("//t:div[@type='section']", namespaces=NS_MAP) for section in eoa_sections: