Skip to content
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

Serializing XML attributes from extension types? #1092

Open
mcrist5564 opened this issue Nov 14, 2024 · 0 comments
Open

Serializing XML attributes from extension types? #1092

mcrist5564 opened this issue Nov 14, 2024 · 0 comments

Comments

@mcrist5564
Copy link

I am attempting to use the schema from here: PLMXML

plus plmxml_parameters.zip to create an XML document from python. In the zip file is an example output that I'm expecting. The schema has extensions on extensions, with different extension layers having attributes for the extension. How do I set the attributes on a python class when those attributes are in the extension?

I've tried multiple ways to use xsdata, but have only really managed to get this command to work with the code below:
!xsdata plmxml_parameters.xsd --package com.siemens --structure-style single-package

Here's the code I'm using:

import com.siemens
import pprint
import datetime
from xsdata.formats.dataclass.context import XmlContext
from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig
from xsdata.formats.dataclass.serializers.writers import XmlEventWriter
from xsdata.formats.dataclass.serializers.writers import LxmlEventWriter

# Create initial container for XML objects
plmxml_container = com.siemens.Plmxml(language="en-us", schema_version="6", author="Mike", time=datetime.datetime.now().strftime("%H:%M:%S"), date=datetime.datetime.now().strftime("%Y-%m-%d"))

# Create Header and Product objects

# Define parameters by unit
velocity = com.siemens.Parameter(id="id" + str(1))
velocity.name = 'velocity'

# Define a parameter revision for some reason that uses the parameter unit
param_rev = com.siemens.ParameterDefinitionRevision(id="id" + str(2))
param_rev.master_ref=velocity.id
param_rev.revision="A"

# Define the parameter that uses the parameter revision
realparam = com.siemens.RealParameter(id="id" + str(3))
realparam.name="Airspeed"
realparam.definition_ref=param_rev.id
realparam.minimum=0
realparam.maximum=120
realparam.maximum_operator="lessThan"
realparam.goal=90
realparam.minimum_operator="greaterThan"

config = SerializerConfig(indent="  ", pretty_print=True)
context = XmlContext()
serializer = XmlSerializer(context=context, config=config, writer=XmlEventWriter)

serialize_this = com.siemens.Plmxml(
        parameter = [velocity],
        parameter_definition_revision = [param_rev],
        real_parameter = [realparam]
)

print(serializer.render(serialize_this, ns_map={"": "http://www.plmxml.org/Schemas/PLMXMLSchema"}))```

And the result:
`<?xml version="1.0" encoding="UTF-8"?>
<PLMXML xmlns="http://www.plmxml.org/Schemas/PLMXMLSchema" language="en" languages="en">
  <RealParameter id="id3" distributionFactor="1.0" definitionRef="id2" goal="90" minimum="0" maximum="120" minimumOperator="greaterThan" maximumOperator="lessThan"/>
  <Parameter id="id1" distributionFactor="1.0"/>
  <ParameterDefinitionRevision id="id2" revision="A" masterRef="id1" allowedValuesExhaustive="false"/>
</PLMXML>`

Note that the "name" value is missing from each of RealParameter and ParameterDefinition.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant