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

xml:space="preserve" attribute is dropped from elements when parsing .arxml #704

Closed
magnus-lindstrom opened this issue Sep 5, 2022 · 3 comments

Comments

@magnus-lindstrom
Copy link

(similar to my previous question/bug report. This time I'm using the latest master version of xsData installed roughly 30mins prior to writing this text)

I am working with large xml files called .arxml, the schema for which can be seen here. When I parse an .arxml file and then serialize to a new file and compare the input and output, all instances of the xml:space="preserve" attribute are dropped. For example, the element

<SD GID="ElementNumber" xml:space="preserve">4529224</SD>

is reduced to

<SD GID="ElementNumber">4529224</SD>

Is this because there is no whitespace in the element content, and therefore the tag is meaningless? I have looked through all instances where the attribute is dropped, and in all cases the content is void of any whitespace.
If this is the case, should the attribute perhaps be kept anyway, should the element happen to be modified to contain whitespace which should be ignored?

Below are the steps that I have taken to arrive at my current problem.

xsdata "C:\<path_to_file>\AUTOSAR_4-2-2.xsd" --package autosar_classes

Followed by running the below python script

from pathlib import Path
from xsdata.formats.dataclass.parsers import XmlParser
from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig
import autosar_classes as ac

parser = XmlParser()
root = parser.from_path(Path("sample.arxml"), ac.Autosar)

serializer_config = SerializerConfig(pretty_print=True)
serializer = XmlSerializer(serializer_config)

path = Path("modified.arxml")
with path.open("w") as fp:
    serializer.write(fp, root, ns_map={"": "http://autosar.org/schema/r4.0"})
@tefra
Copy link
Owner

tefra commented Sep 5, 2022

Is it possible to send me the sampe file? I can't reproduce that

In [1]: from xsdata.formats.dataclass.parsers import XmlParser
   ...: from generated import Sd
   ...: from xsdata.formats.dataclass.serializers import XmlSerializer
   ...: 
   ...: raw = """<SD GID="ElementNumber" xml:space="preserve">4529224</SD>"""
   ...: 
   ...: obj = XmlParser().from_string(raw, Sd)
   ...: 
   ...: print(XmlSerializer().render(obj))
<?xml version="1.0" encoding="UTF-8"?>
<SD GID="ElementNumber" xml:space="preserve">4529224</SD>

In [2]: 

@tefra
Copy link
Owner

tefra commented Sep 6, 2022

Just checked this is the same issue with #701, before the patch the class doesn't get the xml:space attr,

Before

@dataclass
class Sd:
...
    class Meta:
        name = "SD"

    value_extension: str = field(
        default="",
        metadata={
            "required": True,
            "white_space": "preserve",
        }
    )
    value: Optional[str] = field(
        default=None,
        metadata={
            "name": "VALUE",
            "type": "Element",
            "namespace": "http://autosar.org/schema/r4.0",
            "white_space": "preserve",
        }
    )

After the patch in master

@dataclass
class Sd:
... 
    class Meta:
        name = "SD"

    value: str = field(
        default="",
        metadata={
            "required": True,
            "white_space": "preserve",
        }
    )
    s: Optional[str] = field(
        default=None,
        metadata={
            "name": "S",
            "type": "Attribute",
        }
    )
    t: Optional[str] = field(
        default=None,
        metadata={
            "name": "T",
            "type": "Attribute",
            "pattern": r"([0-9]{4}-[0-9]{2}-[0-9]{2})(T[0-9]{2}:[0-9]{2}:[0-9]{2}(Z|([+\-][0-9]{2}:[0-9]{2})))?",
        }
    )
    gid: Optional[str] = field(
        default=None,
        metadata={
            "name": "GID",
            "type": "Attribute",
        }
    )
    space: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
            "namespace": "http://www.w3.org/XML/1998/namespace",
        }
    )

Make sure you install the latest master @magnus-lindstrom

@tefra tefra closed this as completed Sep 6, 2022
@magnus-lindstrom
Copy link
Author

Yes, I realize now that the file I had been comparing against was the previously generated file using the old installation. I am not able to reproduce the problem stated in this issue again. Sorry, and thanks for the clarification!

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

2 participants