Question/Enhancement: Keep XSD groups as separate classes
I was wondering if there is a way (a switch/setting) that allows xs:group. The following is a snippet from: identifier.xsd.
<xs:group name="documentNameGroup">
<xs:annotation>
<xs:documentation>Base IP-XACT document reference type. Contains vendor, library, name and version attributes.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:group ref="ipxact:versionedIdentifier"/>
<xs:element name="displayName" type="xs:string" minOccurs="0">
<xs:annotation>
<xs:documentation>Name for display purposes. Typically a few words providing a more detailed and/or user-friendly name than the vlnv.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element ref="ipxact:shortDescription" minOccurs="0"/>
<xs:element ref="ipxact:description" minOccurs="0"/>
</xs:sequence>
</xs:group>
Most elements (eg: component.xsd refer to it like:
<xs:group ref="ipxact:documentNameGroup"/>
I was expecting to see a documentNameGroup class generated and the componentType use it. But I see the elements from documentNameGroup are flattened in componentType class:
@dataclass(slots=True)
class ComponentType:
...
vendor: Optional[str] = field(
default=None,
metadata={
"type": "Element",
"namespace": "http://www.accellera.org/XMLSchema/IPXACT/1685-2022",
"required": True,
},
)
library: Optional[str] = field(
default=None,
metadata={
"type": "Element",
"namespace": "http://www.accellera.org/XMLSchema/IPXACT/1685-2022",
"required": True,
},
)
name: Optional[str] = field(
default=None,
metadata={
"type": "Element",
"namespace": "http://www.accellera.org/XMLSchema/IPXACT/1685-2022",
"required": True,
},
)
version: Optional[str] = field(
default=None,
metadata={
"type": "Element",
"namespace": "http://www.accellera.org/XMLSchema/IPXACT/1685-2022",
"required": True,
},
)
display_name: Optional[str] = field(
default=None,
metadata={
"name": "displayName",
"type": "Element",
"namespace": "http://www.accellera.org/XMLSchema/IPXACT/1685-2022",
},
)
short_description: Optional[ShortDescription] = field(
default=None,
metadata={
"name": "shortDescription",
"type": "Element",
"namespace": "http://www.accellera.org/XMLSchema/IPXACT/1685-2022",
},
)
description: Optional[Description] = field(
default=None,
metadata={
"type": "Element",
"namespace": "http://www.accellera.org/XMLSchema/IPXACT/1685-2022",
},
)
I appreciate if you can point me to an option if it exists, or consider this as an enhancement.
It looks like some simpleTypes are completely ignored and no binding is generated.
Example: cellStrengthValueType, edgeValueType, ... from. I cannot seen any of the simpleTypes in the generated classes.
- http://www.accellera.org/XMLSchema/IPXACT/1685-2022/constraints.xsd
And some groups versionedIdentifier, documentNameGroup, are just flattened instead of generating a class.
- http://www.accellera.org/XMLSchema/IPXACT/1685-2022/identifier.xsd
At my end I am able to generate:
class CellStrengthValueType(Enum):
"""
Indicates legal cell strength values.
"""
LOW = "low"
MEDIAN = "median"
HIGH = "high"
...
class EdgeValueType(Enum):
"""
Indicates legal values for edge specification attributes.
"""
RISE = "rise"
FALL = "fall"
With following command as basis:
xsdata index.xsd --package 2022 --structure-style namespaces --config .xsdata.xml --output data
And some groups versionedIdentifier, documentNameGroup, are just flattened instead of generating a class.
What could be the interest of generating such class ?
Anyway you can probably use xsdata plugin mechanism to customize what generate the tool.
What is the configuration file .xsdata content you are using? If groups are not flattened, I can access members with the same hierarchy as defined in the XSD and the standard.
Yes, my mistake, I see cellStrengthValueType, edgeValueType but shouldn't I see a documentNameGroup and versionedIdentifier class based on the schema? These are groups.
I don't think there is anything specific.
<?xml version="1.0" encoding="UTF-8"?>
<Config xmlns="http://pypi.org/project/xsdata" version="24.11">
<Output maxLineLength="79" subscriptableTypes="false" genericCollections="false" unionType="false">
<Package>generated</Package>
<Format repr="true" eq="true" order="false" unsafeHash="false" frozen="false" slots="false" kwOnly="false">dataclasses</Format>
<Structure>filenames</Structure>
<DocstringStyle>reStructuredText</DocstringStyle>
<RelativeImports>false</RelativeImports>
<CompoundFields defaultName="choice" useSubstitutionGroups="false" forceDefaultName="false" maxNameParts="3">false</CompoundFields>
<WrapperFields>false</WrapperFields>
<PostponedAnnotations>false</PostponedAnnotations>
<UnnestClasses>false</UnnestClasses>
<IgnorePatterns>false</IgnorePatterns>
<IncludeHeader>true</IncludeHeader>
</Output>
<Conventions>
<ClassName case="pascalCase" safePrefix="type"/>
<FieldName case="originalCase" safePrefix="value"/>
<ConstantName case="screamingSnakeCase" safePrefix="value"/>
<ModuleName case="snakeCase" safePrefix="mod"/>
<PackageName case="snakeCase" safePrefix="pkg"/>
</Conventions>
<Substitutions>
<Substitution type="package" search="http://www.w3.org/2001/XMLSchema" replace="xs"/>
<Substitution type="package" search="http://www.w3.org/XML/1998/namespace" replace="xml"/>
<Substitution type="package" search="http://www.w3.org/2001/XMLSchema-instance" replace="xsi"/>
<Substitution type="package" search="http://www.w3.org/1998/Math/MathML" replace="mathml3"/>
<Substitution type="package" search="http://www.w3.org/1999/xlink" replace="xlink"/>
<Substitution type="package" search="http://www.w3.org/1999/xhtml" replace="xhtml"/>
<Substitution type="package" search="http://schemas.xmlsoap.org/wsdl/soap/" replace="soap"/>
<Substitution type="package" search="http://schemas.xmlsoap.org/wsdl/soap12/" replace="soap12"/>
<Substitution type="package" search="http://schemas.xmlsoap.org/soap/envelope/" replace="soapenv"/>
<Substitution type="class" search="(.*)Class$" replace="\1Type"/>
</Substitutions>
<Extensions/>
</Config>
If groups are not flattened, I can access members with the same hierarchy as defined in the XSD and the standard.
I prefer my dataclass hierarchy to be close to xml file available at end-user for my application.
Could we somehow create classed the groups specified in http://www.accellera.org/XMLSchema/IPXACT/1685-2022/identifier.xsd. Some groups versionedIdentifier, documentNameGroup, are just flattened instead of generating a class and using composition. An option to not flatten group members?
xsdata follows the naive approach (that works, most of the time) elements and complex types are classes everything else is noise and is flattened by default. There is no option to generate simple types or groups or any other wrapper container because it's not easy to represent theses into classes.