From c933ad0c51180b89fb95be52e1baf78782df6567 Mon Sep 17 00:00:00 2001 From: Bee Webb Date: Tue, 20 Aug 2024 13:21:35 +0000 Subject: [PATCH] xml input: Ensure namespaces don't break sheet titles Namespaces contain colons which aren't permitted in Excel sheet names. --- flattentool/schema.py | 2 +- flattentool/tests/fixtures/iati_namespaces.xml | 16 ++++++++++++++++ flattentool/tests/test_init.py | 12 +++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 flattentool/tests/fixtures/iati_namespaces.xml diff --git a/flattentool/schema.py b/flattentool/schema.py index 9110e8b..4f01722 100644 --- a/flattentool/schema.py +++ b/flattentool/schema.py @@ -42,7 +42,7 @@ def make_sub_sheet_name( x[:truncation_length] for x in parent_path.split(path_separator) if x != "0" ) + property_name - ) + ).replace(":", "-") class TitleLookup(UserDict): diff --git a/flattentool/tests/fixtures/iati_namespaces.xml b/flattentool/tests/fixtures/iati_namespaces.xml new file mode 100644 index 0000000..3a2defe --- /dev/null +++ b/flattentool/tests/fixtures/iati_namespaces.xml @@ -0,0 +1,16 @@ + + + + + + two + two + + + + + three + four + + + diff --git a/flattentool/tests/test_init.py b/flattentool/tests/test_init.py index 0f382dc..35829f4 100644 --- a/flattentool/tests/test_init.py +++ b/flattentool/tests/test_init.py @@ -7,7 +7,7 @@ import pytest -from flattentool import decimal_datetime_default, unflatten +from flattentool import decimal_datetime_default, flatten, unflatten def original_cell_and_row_locations(data): @@ -1452,3 +1452,13 @@ def test_commands_id_name(tmpdir, input_format): ], "some": "data", } + + +def test_flatten_xml(tmpdir): + flatten( + "flattentool/tests/fixtures/iati_namespaces.xml", + output_name=tmpdir.join("flattened_xml").strpath, + id_name="iati-identifier", + root_list_path="iati-activity", + xml=True, + )