xsdata icon indicating copy to clipboard operation
xsdata copied to clipboard

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

Open amal-meer opened this issue 3 years ago • 6 comments

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?

amal-meer avatar Jul 18 '22 07:07 amal-meer

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.

tefra avatar Jul 18 '22 07:07 tefra

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.

amal-meer avatar Jul 18 '22 10:07 amal-meer

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

tefra avatar Jul 24 '22 05:07 tefra

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

amal-meer avatar Jul 24 '22 06:07 amal-meer

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

amal-meer avatar Jul 24 '22 06:07 amal-meer

Yeap, it's planned

tefra avatar Jul 31 '22 06:07 tefra

Hi @amal-meer

I added a new config option to specify the filter strategy

Give it a try

xsdata schema.xsd --filter-strategy referredGlobals

tefra avatar Aug 21 '22 08:08 tefra

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

matteoaletti avatar Mar 19 '24 11:03 matteoaletti

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

tefra avatar Mar 19 '24 14:03 tefra

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?

matteoaletti avatar Mar 19 '24 14:03 matteoaletti

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

tefra avatar Mar 19 '24 14:03 tefra