pydantic-xml
                                
                                 pydantic-xml copied to clipboard
                                
                                    pydantic-xml copied to clipboard
                            
                            
                            
                        Ordered search mode with List[Union[]]
I have the model, which has field with type:
children: list[
    Union[
        FieldBool,
        FieldInt8,
        FieldInt16,
        FieldInt32,
        FieldInt64,
        FieldUint8,
        FieldUint16,
        FieldUint32,
        FieldUint64,
        FieldEnum,
        FieldBitfield,
        FieldCompound,
        FieldStaticArray,
        FieldVector,
        FieldDeque,
        FieldSet,
        FieldBitVector,
        FieldPointer,
        FieldDfFlagArray,
        FieldDfStaticFlagArray,
        FieldDfArray,
        FieldDfLinkedList,
        FieldDfLinkedListType,
        FieldDfOtherVectorsType,
        FieldString,
        FieldPtrString,
        FieldStaticString,
    ]
] = element(default=None)
I need to parse tag and save the order of children in list. unordered search mode found 100 items, but ordered found only 1.
I mean, i don't know exact order of children, but i want to keep it. How can i do it?
@shevernitskiy hi,
Could you provide the document you are trying to parse?
The problem is that in ordered search mode Union typed field is bound to the first matched element skipping all the elements before.
In your example FieldBool is not found, then FieldInt8 is bound to the <int8_t name='cached_glowtile_type'/> skipping all the elements before and the search continues after it. Therefore only 1 element is found in ordered search mode.
In unordered search mode the elements order is not kept and right now there is not way to keep it.
The problem is that in
orderedsearch modeUniontyped field is bound to the first matched element skipping all the elements before.In your example
FieldBoolis not found, thenFieldInt8is bound to the<int8_t name='cached_glowtile_type'/>skipping all the elements before and the search continues after it. Therefore only 1 element is found inorderedsearch mode. Inunorderedsearch mode the elements order is not kept and right now there is not way to keep it.
I see. That sad. As you can see there is cpp struct in xml, so the order matters.
Could the unordered_sequenced mode be the possible future improvement?
Yes, I will think about it. It is not easy to implement the search keeping elements order due to internal implementation details, but I will try to find a workaround.
Thanks!