diff --git a/docling/backend/xml/jats_backend.py b/docling/backend/xml/jats_backend.py index 3a2db8764d..99aae88fb2 100755 --- a/docling/backend/xml/jats_backend.py +++ b/docling/backend/xml/jats_backend.py @@ -908,16 +908,13 @@ def _add_figure_captions( ) caption_node = node.xpath("caption") - caption: str | None + caption: str = "" if len(caption_node) > 0: - caption = "" for caption_par in list(caption_node[0]): if caption_par.xpath(".//supplementary-material"): continue caption += JatsDocumentBackend._get_text(caption_par).strip() + " " caption = caption.strip() - else: - caption = None # TODO: format label vs caption once styling is supported fig_text: str = f"{label}{' ' if label and caption else ''}{caption}" @@ -1178,7 +1175,8 @@ def _add_tables( # Label if len(node.xpath("label")) > 0: - table["label"] = node.xpath("label")[0].text + # lxml sets .text to None for an empty element; fall back to "". + table["label"] = node.xpath("label")[0].text or "" try: self._add_table(doc, parent, table) diff --git a/tests/test_backend_jats.py b/tests/test_backend_jats.py index 0c6068b39c..dfa89e9920 100644 --- a/tests/test_backend_jats.py +++ b/tests/test_backend_jats.py @@ -1104,3 +1104,31 @@ def test_jats_empty_article_title_does_not_crash(): exported = doc.export_to_markdown() # Empty title is serialized as an H1 with no text. assert exported == "# " + + +def _caption_texts(doc: DoclingDocument) -> list[str]: + return [t.text for t in doc.texts if t.label == DocItemLabel.CAPTION] + + +def test_jats_labelled_figure_without_a_caption_omits_the_word_none(): + """A may carry a