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

remove useless classes in the generated models + use booleans instead of True False enum. #691

Closed
amal-meer opened this issue Jul 18, 2022 · 11 comments · Fixed by #699
Closed

Comments

@amal-meer
Copy link

amal-meer commented Jul 18, 2022

I have 2 things that I need to improve to simplify the generated classes from my xsd file. A sample to illustrate the issue:

<?xml version="1.0" encoding="utf-8"?>
	<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
		<xs:element name="Parent">
		<xs:complexType>
			<xs:sequence>
					<xs:element ref="Child1" />
					<xs:element ref="Child2" />
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="Child1">
		<xs:complexType>
			<xs:attribute name="name" type="xs:string" use="required" />
			<xs:attribute name="id" type="xs:string" use="required" />
			<xs:attribute name="isTrue" type="xs:boolean" use="required" />
		</xs:complexType>
	</xs:element>
	<xs:element name="Child2">
		<xs:complexType>
			<xs:sequence>
				<xs:element minOccurs="0" maxOccurs="1" ref="Name" />
			</xs:sequence>
			<xs:attribute name="id" type="xs:string" use="required" />
			<xs:attribute name="isTrue" use="required">
				<xs:simpleType>
					<xs:restriction base="xs:NMTOKEN">
						<xs:enumeration value="Yes" />
						<xs:enumeration value="No" />
					</xs:restriction>
				</xs:simpleType>
			</xs:attribute>
		</xs:complexType>
	</xs:element>
	<xs:element name="Name" type="xs:string" />
</xs:schema>

Child1 is the ideal schema representation that will result in the needed output, but Child2 is the one that I have.

Problems:

  1. The xsd file represents some strings attributes as a sequence of elements that refers to another element.
    Ex: Name in Child2 element. This will results in an unnecessary Name class and I will need to remove it manually.

  2. Some attributes are represented as a "Yes" and "No" enum, is there a way to convert such an attribute to boolean? and then parse it from the XML file as True if the value is Yes, and False if the value is No?

@tefra
Copy link
Owner

tefra commented Jul 18, 2022

The name type is an root level element, by definition those are global types and can appear as standalone

<?xml version="1.0" encoding="UTF-8"?>
<name>foo<name>

Maybe I could add a config option to remove types with no dependencies that are not used anywhere could do the trick here.

For the second on the enumeration extend the xs:NMTOKEN, there is no way to determine that these are actually boolean.

@amal-meer
Copy link
Author

Actually for the first one, I generated the output 2 weeks ago with xsdata-22.5 and xsdata-attrs-21.11 using the default config with the following command
xsdata file.xsd --package result.models --output attrs
and it removes the Name class from the generated output with the following warning
warning:: Reset absent type: Name

I tried again yesterday but I couldn't reproduce the same result, I have no idea about the reason.

would appreciate if you could add it as a config option.

@tefra
Copy link
Owner

tefra commented Jul 24, 2022

The warning:: Reset absent type: Name, refers to when a type reference can't be found, maybe your xsd changed?

@amal-meer
Copy link
Author

amal-meer commented Jul 24, 2022

Never mind, maybe I've commented out some elements.

@amal-meer
Copy link
Author

Is there a plan to add this as a config option?

@tefra
Copy link
Owner

tefra commented Jul 31, 2022

Yeap, it's planned

@tefra
Copy link
Owner

tefra commented Aug 21, 2022

Hi @amal-meer

I added a new config option to specify the filter strategy

Give it a try

xsdata schema.xsd --filter-strategy referredGlobals

@matteoaletti
Copy link

Hello, I don't see this option anymore in v24.3, did I miss something on this topic?
Thank you!

@tefra
Copy link
Owner

tefra commented Mar 19, 2024

Since v24.3 the generator avoids flattening all root elements and global types, which also removed the filtering strategies.

@matteoaletti
Copy link

Thanks for the quick answer, does this new behavior correspond to one of the filter strategies that were available or the current behavior is completely new?

@tefra
Copy link
Owner

tefra commented Mar 19, 2024

There is no filter strategies anymore, all root elements and complex types are going to be generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants