pydantic-xml icon indicating copy to clipboard operation
pydantic-xml copied to clipboard

[Question] Combining multiple tags into a single attribute

Open synapticarbors opened this issue 7 months ago • 2 comments

Digging through the documentation and code, I was unable to figure out if the following was possible, and if so, what the best approach would be. Supposing I had an xml snippet that looked like:

<system>
    <A>
        <row>
            <item>1.0</item>
            <item>0.0</item>
            <item>0.0</item>
        </row>
        <row>
            <item>0.0</item>
            <item>2.2</item>
            <item>3.3</item>
        </row>
        <row>
            <item>0.0</item>
            <item>4.4</item>
            <item>5.5</item>
        </row>
    </A>
</system>

I'm trying to map A to something like:

class Example(BaseXmlModel, tag="system"):
    A: list[list[float]]: element(default_factory=list)

where ex = Example.from_xml(doc.xml) would have

ex.A == [[1.0, 0.0, 0.0], [0.0, 2.2, 3.3], [0.0, 4.4, 5.5]]

The xml_field_validator used in the custom xml serialization example only really seems capable of mapping one tag to a single attribute. Any suggestions on how to do this, as well as the reverse serialization step would be greatly appreciated.

synapticarbors avatar Mar 07 '25 19:03 synapticarbors