From 5f5c6dc7882c6894d74fd7ee660f8a7d0b61565a Mon Sep 17 00:00:00 2001 From: Bee Webb Date: Tue, 20 Aug 2024 13:25:38 +0000 Subject: [PATCH] xml input: Ensure namespaces don't break sheet titles Namespaces contain colons which aren't permitted in Excel sheet names. --- CHANGELOG.md | 1 + flattentool/schema.py | 2 +- flattentool/tests/fixtures/iati_namespaces.xml | 16 ++++++++++++++++ flattentool/tests/test_init.py | 12 +++++++++++- 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 flattentool/tests/fixtures/iati_namespaces.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index fce92a3..4010229 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed - Ignore null characters in the input CSV file when getting non-header rows +- When unflatteneing XML, avoid errors due to namespaces causing colons in sheet names, by replacing them with dashes https://github.com/OpenDataServices/flatten-tool/pull/456 ### Changed 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, + )