Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write SDMX-ML (.xml) file with <mes:GenericData> as the root element #196

Open
henrihi opened this issue Oct 14, 2024 · 1 comment
Open
Labels
enh Enhancements & new features help welcome Issues that depend on contributions from new developers writer Convert objects to other formats xml SDMX-ML format

Comments

@henrihi
Copy link

henrihi commented Oct 14, 2024

Hi,

I am trying to create an SDMX-ML (.xml) file with <mes:GenericData> as the root of the .xml file. However the sdmx.to_xml() function seem to produce an .xml file with <mes:StructureSpecificData> as the root regardless:

<?xml version='1.0' encoding='utf-8'?>
<mes:StructureSpecificData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:com="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common" xmlns:md="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/metadata/generic" xmlns:data="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/structurespecific" xmlns:str="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/structure" xmlns:mes="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message" xmlns:gen="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic" xmlns:footer="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message/footer">
<mes:Header>
<mes:Test>false</mes:Test>
</mes:Header>
<mes:DataSet/>
</mes:StructureSpecificData>

Here is my code:

dataset = DataSet()
test_msg = DataMessage(data=[dataset])
with open(f"data/generic_data.xml", "wb") as f:
    f.write(sdmx.to_xml(test_msg))

Is there any way to produce a .xml file with <mes:GenericData> as the root using the sdmx.to_xml() function? Also, is it possible to change the root attributes? I would also like to remove the xmlns:data="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/structurespecific" attribute.

@khaeru khaeru added enh Enhancements & new features writer Convert objects to other formats xml SDMX-ML format help welcome Issues that depend on contributions from new developers labels Oct 22, 2024
@khaeru
Copy link
Owner

khaeru commented Oct 22, 2024

Hi there! Indeed, this is currently kind of fixed. It happens because the root rag is chosen by

sdmx/sdmx/writer/xml.py

Lines 124 to 131 in e88f8d3

# Identify root tag
if len(obj.data) and isinstance(
obj.data[0],
(model.StructureSpecificDataSet, model.StructureSpecificTimeSeriesDataSet),
):
tag = "mes:StructureSpecificData"
else:
tag = tag_for_class(type(obj))

and
# Message classes
("message.DataMessage", "mes:StructureSpecificData"),
("message.MetadataMessage", "mes:GenericMetadata"),
("message.MetadataMessage", "mes:StructureSpecificMetadata"),
("message.ErrorMessage", "mes:Error"),
("message.StructureMessage", "mes:Structure"),

The latter gives defaults used for both SDMX-ML 2.1 and 3.0.0. I guess it should only be the default for SDMX-ML 3.0.0 (where it is the only option given by the standard), and for SDMX-ML 2.1 the code could instead peek ahead and choose <mes:GenericData> if the contained data sets are not structure-specific.

The underlying package lxml writes out all of the XML namespace (xmlns:) attributes that appear on the root tag or any of its children. The set of namespaces and prefixes is determined further up in the same file:

_element_maker = ElementMaker(nsmap={k: v for k, v in NS.items() if v is not None})

So this could also be adapted to (a) differ for SDMX-ML 2.1 and 3.0 and (b) only include …/{meta}data/structurespecific or …/{meta}data/generic, as appropriate.

A PR would be welcome. As a workaround you could use lxml directly to manipulate the returned XML or file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enh Enhancements & new features help welcome Issues that depend on contributions from new developers writer Convert objects to other formats xml SDMX-ML format
Projects
None yet
Development

No branches or pull requests

2 participants