-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
Order of next_value in XmlSerializer #565
Comments
oh It's a repeatable choice, which means the elements can appear in any order, the only way to maintain the ordering between binding operations is to enable compound fields during generation <xs:complexType name="SimpleContourType">
<xs:annotation>
<xs:documentation>Defintion of a simple contour (one point and multiple lines and arcs)</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="StartPoint" type="PointType" />
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Line" type="LineType" />
<xs:element name="Arc" type="ArcType" />
</xs:choice>
</xs:sequence>
</xs:complexType> @dataclass
class SimpleContourType:
"""
Defintion of a simple contour (one point and multiple lines and arcs)
"""
start_point: Optional[PointType] = field(
default=None,
metadata={
"name": "StartPoint",
"type": "Element",
"namespace": "http://www.design2machine.com",
"required": True,
}
)
line_or_arc: List[object] = field(
default_factory=list,
metadata={
"type": "Elements",
"choices": (
{
"name": "Line",
"type": LineType,
"namespace": "http://www.design2machine.com",
},
{
"name": "Arc",
"type": ArcType,
"namespace": "http://www.design2machine.com",
},
),
}
) |
Thank you so much for the hint. Works like a charm! Just to recap: contour = btlx.project.parts.part[0].processings.choice[0].contour
for loa in contour.line_or_arc:
print(loa) gives all choices in the correct order:
From an XML design: Is there a (better) alternative? |
Unfortunately there is no other way without keeping some sort of ordering in a not xml field, but that would be extremely tricky without additional code inside each model. The idea for the compound fields is baed on the jaxb modeling, but I am open to suggestions |
I suspect that
next_value
ofXmlSerializer
does not maintain the order of elements in the XML file.Full example here
The XSD is from here.
The excerpt of the XML is
A
Line
is explicitly defined by anEndPoint
and implicitly by the previous point. That is a least a questionable design choice ...I use xsdata to access the XML which is awesome!
Given the model generated by the XSD the order of the
Arc
andLine
is separated in two lists:but it loses of course the order.
According to the hint in #261 I tried
next_value
:But the result has a different order than the original XML>
Arc
is the 10th entry in the XML, but the 3rd output of xsdata.Would be awesome if
XmlSerializer
maintains the order. Or is there any other possibility to get the elements in the order they appear in the XML?Tested on Python 3.9.6 on macOS 10.15.7, xsdata 21.7, and on Python 3.9.6 in a Docker container.
The text was updated successfully, but these errors were encountered: