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

empty attributes are removed in generated XML #230

Closed
gjvc opened this issue Aug 12, 2020 · 7 comments
Closed

empty attributes are removed in generated XML #230

gjvc opened this issue Aug 12, 2020 · 7 comments
Labels
bug Something isn't working

Comments

@gjvc
Copy link

gjvc commented Aug 12, 2020

Hello,
First of all, thank you for a wonderful project. It's a great leap forward over walking XML trees via ElementTree findall() and such.

I am using it to generate a configuration for a system. This is a legacy application and as such has many quirks. One such quirk is that it needs to see an empty attribute in an element like this

<element something="hello" else="there" amount="" symbol="ABC" />

The problem I'm having is that amount="" is getting removed and does not appear in the generated XML, even when I set it to "". I would expect it to be removed if it were set to None in Python, but i'm setting it to the empty string and that is what I would like to see in the XML. I'm using xsdata version 20.7 on python 3.8.5.

@tefra
Copy link
Owner

tefra commented Aug 12, 2020

You are right the serializer is ignoring empty strings as well, that doesn't sound right.

Let me run a few tests to see if we can change this default behavior or if we need to add some sort of configuration there.

Same behavior for v20.8 as well

@tefra tefra added the bug Something isn't working label Aug 12, 2020
@tefra
Copy link
Owner

tefra commented Aug 12, 2020

Out of curiosity @gjvc are you working with a schema definition or do you write the models yourself? Also did you encounter an issue with v20.8 and didn't upgrade?

@gjvc
Copy link
Author

gjvc commented Aug 12, 2020

Out of curiosity @gjvc are you working with a schema definition or do you write the models yourself? Also did you encounter an issue with v20.8 and didn't upgrade?

Thank you very much for the quick response! I am using an XSD generated from a RelaxNG grammar via the "trang" tool.

I tried v20.8 to see if it fixed this problem (which it didn't)[1], but held off making this problem report until I'd gone through the documentation one more time :-)

[1] I will try the latest version

@tefra
Copy link
Owner

tefra commented Aug 12, 2020

Can you share how that attribute is defined in the xsd?

tefra added a commit that referenced this issue Aug 12, 2020
Notes:
Exception to the rule is an empty tokens list!
@gjvc
Copy link
Author

gjvc commented Aug 12, 2020

Currently it is defined in the XSD as

   <xs:attribute name="orderQty" type="xs:integer"/>

I am able (and might need) to change this to "xs:string", because in some cases the value is of the format "1000|2000|3000" (as I said, legacy system :-))

tefra added a commit that referenced this issue Aug 12, 2020
Notes:
Exception to the rule is an empty tokens list!
@gjvc
Copy link
Author

gjvc commented Aug 12, 2020

I have just tried your fix via this in my etc/pip/requirements.txt file

-xsdata==20.7
+git+https://github.com/tefra/xsdata.git@issue-230

...and it looks like it works great. Thank you very much for the amazingly quick fix. :-)

@tefra
Copy link
Owner

tefra commented Aug 12, 2020

awesome man, I also confirmed with the w3c-test-suite that the updated behavior still produces valid xml instances.

I am going to merge it to master, and it will be included in the next release v20.9, make sure you switch to the master branch till then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants