tefra / xsdata

Naive XML & JSON Bindings for python
https://xsdata.readthedocs.io
MIT License
324 stars 59 forks source link

Do not lose data during serialization after parsing #1011

Closed sedrew closed 5 months ago

sedrew commented 5 months ago

Hello,I'm using fail_on_unknown_properties=False. Since arguments in my models may not be registered in my xml, but I need to save this data during serialization.

How to save xml elements that are not specified in the xsdata model during parsing so that they are not lost during serialization?

Update:

I'm trying to achieve this with a Wildcard. My base class

@dataclass
class BaseSchema:

    any_elements: List[object] = field(
        default_factory=list,
        metadata={
            "type": "Wildcard",
            "namespace": "##any"
        }
    )

But namespace don't wrote in local element attribute

I get is

    <p:extLst>
      <p:ext uri="{BB962C8B-B14F-4D97-AF65-F5344CB8AC3E}">
        <p14:creationId val="3561048925"/>
      </p:ext>
    </p:extLst>

I need

  <p:extLst>
    <p:ext uri="{BB962C8B-B14F-4D97-AF65-F5344CB8AC3E}">
      <p14:creationId xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="3561048925"/>
    </p:ext>
   </p:extLst>
tefra commented 5 months ago

Hi @sedrew can you post the whole excerpt please, from only that output I don't see what's the different both xml outputs are equivalent.

sedrew commented 5 months ago

I'm trying to overwrite slide from powerpoint. I did some additional research and found out that the namespace problem is not related to a powerpoint reading error. I found out that the Wildcard order does not match the original file. Because of this, powerpoint cannot read this xml.

image image

after_parser.txt after_power_point.txt

Not a problem for reading powerpoint:

image
sedrew commented 5 months ago

Hi @sedrew can you post the whole excerpt please, from only that output I don't see what's the different both xml outputs are equivalent.

How do I save index positions in XmlVar for elements during parsing?

For example, one Sp element in xml may be located at the beginning, the other at the end of the root

image
@dataclass
class SpTree:
    class Meta:
        name = "spTree"
        namespace = (
            "http://schemas.openxmlformats.org/presentationml/2006/main"
        )

    nv_grp_sp_pr: Optional[NvGrpSpPr] = field(
        default=None,
        metadata={
            "name": "nvGrpSpPr",
            "type": "Element",
            "required": True,
        },
    )
    grp_sp_pr: Optional[GrpSpPr] = field(
        default=None,
        metadata={
            "name": "grpSpPr",
            "type": "Element",
            "required": True,
        },
    )
    sp: List[Sp] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "min_occurs": 1,
        },
    )

    any_elements: List[object] = field(
        default_factory=list,
        metadata={
            "type": "Wildcard",
            "namespace": "##any"
        }
    )
tefra commented 5 months ago

For repeating choices, on sequences that the order might change you can use compound fields

https://xsdata.readthedocs.io/en/latest/models/fields/#elements https://xsdata.readthedocs.io/en/latest/codegen/config/#compoundfields

sedrew commented 5 months ago

Thanks, It helped me.

    shapes: List[Union[NvGrpSpPr, GrpSpPr, Sp]] = field(
        metadata={
            "type": "Elements",
            "choices": (
                {"wildcard": True, "type": object},
                {"name": "sp", "type": Sp},
                {"name": "grpSpPr", "type": GrpSpPr},
                {"name": "nvGrpSpPr", "type": NvGrpSpPr}
            )
        }
    )