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

order of repeating xml tags #433

Closed
morskibg opened this issue Mar 25, 2021 · 4 comments · Fixed by #436
Closed

order of repeating xml tags #433

morskibg opened this issue Mar 25, 2021 · 4 comments · Fixed by #436
Labels
enhancement New feature or request

Comments

@morskibg
Copy link

Hello, great work !
I have a little issue (sorry if had been discussed). The schema states:

<xs:complexType name="MSCONS">
		<xs:sequence>
			<xs:element name="UNH" type="UNH" minOccurs="1" maxOccurs="1"/>
			<xs:element name="BGM" type="BGM" minOccurs="1" maxOccurs="1"/>
			<xs:element name="DTM" type="DTM" minOccurs="1" maxOccurs="1"/>
			<xs:element name="NAD" type="NAD" minOccurs="2" maxOccurs="2"/>
			<xs:element name="UNS" type="UNS" minOccurs="1" maxOccurs="1"/>
			<xs:element name="NAD" type="NAD" minOccurs="1" maxOccurs="1"/>
			<xs:element name="UNT" type="UNT" minOccurs="1" maxOccurs="1"/>
		</xs:sequence>
</xs:complexType>

final created class is:

@dataclass
class Mscons:
    """
    Metered services consumption report message.
    """
    class Meta:
        name = "MSCONS"

    unh: Optional[Unh] = field(
        default=None,
        metadata={
            "name": "UNH",
            "type": "Element",
            "required": True,
        }
    )
    bgm: Optional[Bgm] = field(
        default=None,
        metadata={
            "name": "BGM",
            "type": "Element",
            "required": True,
        }
    )
    dtm: Optional[Dtm] = field(
        default=None,
        metadata={
            "name": "DTM",
            "type": "Element",
            "required": True,
        }
    )
    nad: List[Nad] = field(
        default_factory=list,
        metadata={
            "name": "NAD",
            "type": "Element",
            "min_occurs": 1,
            "max_occurs": 2,
        }
    )
    uns: Optional[Uns] = field(
        default=None,
        metadata={
            "name": "UNS",
            "type": "Element",
            "required": True,
        }
    )
    unt: Optional[Unt] = field(
        default=None,
        metadata={
            "name": "UNT",
            "type": "Element",
            "required": True,
        }
    )

The XML file for parsing is:

<?xml version="1.0" encoding="UTF-8"?>
<MSCONS xsi:noNamespaceSchemaLocation ="MSCONS_890_v1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<UNH>
		<REFERENCENUMBER>1</REFERENCENUMBER>
		<IDENTIFIER>MSCONS</IDENTIFIER>
		<VERSIONNUMBER>D</VERSIONNUMBER>
		<RELEASENUMBER>1xx</RELEASENUMBER> 
		<CONTROLAGENCY>Udd</CONTROLAGENCY>
		<ASSOCCODE>xxxxx</ASSOCCODE>
	</UNH>
	<BGM>
		<NAME>890</NAME>
		<CODELISTAGENCY>esdf</CODELISTAGENCY>
		<DOCUMENTNUMBER>faaf173d-3e44-4e231-8c15-8e8634566789f</DOCUMENTNUMBER>
		<DOCUMENTFUNC>9</DOCUMENTFUNC>
		<RESPONSETYPE>NA</RESPONSETYPE>
	</BGM>
	<DTM>
		<DATUMQUALIFIER>137</DATUMQUALIFIER>
		<DATUM>201908121200+03</DATUM>
		<FORMAT>303</FORMAT>
	</DTM>
	<NAD>
		<ACTION>MS</ACTION>
		<PARTNER>sdewertxx</PARTNER>
		<CODELISTAGENCY>305</CODELISTAGENCY>
		<COUNTRY>GG</COUNTRY>
	</NAD>
	<NAD>
		<ACTION>MR</ACTION>
		<PARTNER>ddddddddd</PARTNER>
		<CODELISTAGENCY>305</CODELISTAGENCY>
		<COUNTRY>mm</COUNTRY>
	</NAD>
	<UNS>
		<SECTION_ID>D</SECTION_ID>
	</UNS>
	<NAD>
		<ACTION>GN</ACTION>
		<PARTNER>dfrterffff</PARTNER>
		<CODELISTAGENCY>305</CODELISTAGENCY>
		<COUNTRY>mm</COUNTRY>
		<LOC>
			<PLACE_QUALIFIER>172</PLACE_QUALIFIER>
			<PLACE_ID>wertyfghj</PLACE_ID>
			<CODE_LIST_RESPONSIBLE_AGENCY>ff</CODE_LIST_RESPONSIBLE_AGENCY>			
		</LOC>
	</NAD>
	<UNT>
		<NUMSEG>17</NUMSEG>
		<REFNUM>1</REFNUM>
	</UNT>
</MSCONS>

After parsing the segment:

<UNS>
     <SECTION_ID>D</SECTION_ID>
</UNS>"

is after 3 NAD, instead to be between 2 and 3 NAD segment
Is there any option to rearrange such a case?
Thank You !

@tefra
Copy link
Owner

tefra commented Mar 25, 2021

I call this case an effective choice of a elements or repeating elements.

If the elements were part of a xs:choice and you used the --compound-fields option of the cli

<xsd:complexType name="MSCONS">
		<xsd:choice maxOccurs="unbounded">
			<xsd:element name="UNH" type="xsd:string" minOccurs="1" maxOccurs="1"/>
			<xsd:element name="BGM" type="xsd:string" minOccurs="1" maxOccurs="1"/>
			<xsd:element name="DTM" type="xsd:string" minOccurs="1" maxOccurs="1"/>
			<xsd:element name="NAD" type="xsd:string" minOccurs="2" maxOccurs="2"/>
			<xsd:element name="UNS" type="xsd:string" minOccurs="1" maxOccurs="1"/>
			<xsd:element name="NAD" type="xsd:string" minOccurs="1" maxOccurs="1"/>
			<xsd:element name="UNT" type="xsd:string" minOccurs="1" maxOccurs="1"/>
		</xsd:choice>
</xsd:complexType>

You would get a model like this

@dataclass
class Mscons:
    class Meta:
        name = "MSCONS"

    choice: List[object] = field(
        default_factory=list,
        metadata={
            "type": "Elements",
            "choices": (
                {
                    "name": "UNH",
                    "type": str,
                    "namespace": "",
                },
                {
                    "name": "BGM",
                    "type": str,
                    "namespace": "",
                },
                {
                    "name": "DTM",
                    "type": str,
                    "namespace": "",
                },
                {
                    "name": "NAD",
                    "type": str,
                    "namespace": "",
                },
                {
                    "name": "UNS",
                    "type": str,
                    "namespace": "",
                },
                {
                    "name": "UNT",
                    "type": str,
                    "namespace": "",
                },
            ),
        }
    )

That model can ensure the input/output ordering of elements. But for now no it's not possible with xs:sequence elements.

It's on my todo list for some time now though, I am going to prioritize this one.

@tefra tefra added the enhancement New feature or request label Mar 25, 2021
@morskibg
Copy link
Author

Thank You ! Will waiting.

@tefra tefra reopened this Mar 25, 2021
@tefra tefra closed this as completed in 6196a1d Mar 26, 2021
@tefra tefra reopened this Mar 26, 2021
tefra added a commit that referenced this issue Mar 26, 2021
@tefra
Copy link
Owner

tefra commented Mar 26, 2021

The change is master @morskibg give it a try, it should catch most of the cases now

For the above example the generator will now create with the flag --compound-fields true the following model

@dataclass
class Mscons:
    class Meta:
        name = "MSCONS"

    unh: Optional[str] = field(
        default=None,
        metadata={
            "name": "UNH",
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    bgm: Optional[str] = field(
        default=None,
        metadata={
            "name": "BGM",
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    dtm: Optional[str] = field(
        default=None,
        metadata={
            "name": "DTM",
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    nad_or_uns_or_unt: List[object] = field(
        default_factory=list,
        metadata={
            "type": "Elements",
            "choices": (
                {
                    "name": "NAD",
                    "type": str,
                    "namespace": "",
                },
                {
                    "name": "UNS",
                    "type": str,
                    "namespace": "",
                },
                {
                    "name": "UNT",
                    "type": str,
                    "namespace": "",
                },
            ),
            "min_occurs": 3,
            "max_occurs": 5,
        }
    )

@morskibg
Copy link
Author

Work as a charm !!! Thank You very much !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants