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 elements in choice #380

Closed
Dobrygnom opened this issue May 10, 2023 · 1 comment
Closed

Order of elements in choice #380

Dobrygnom opened this issue May 10, 2023 · 1 comment

Comments

@Dobrygnom
Copy link

Hi, I have a problem similar to #285 and looking for solution/workaround.

I have an xml which contains sequence of Plus/Minus elements (simplified here) and their order matters to the backend.

<Operation>
    <Plus></Plus>
    <Plus></Plus>
    <Minus></Minus>
    <Plus></Plus>
</Operation> 

And following xsd which utilizes choice.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:generic="file://generic.xsd" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:complexType name="Operation">
        <xs:choice maxOccurs="unbounded">
                <xs:element name="Plus"   type="PlusType"/>
                <xs:element name="Minus"  type="MinusType"/>
        </xs:choice>
    </xs:complexType>
    <xs:complexType name="PlusType"/>
    <xs:complexType name="MinusType"/>

</xs:schema>

With code generated by XmlSchemaClassGenerator I can't figure out how to preserve order of Plus/Minus operations, because it always dumps all the Plus elements first and Minus after.
It worked fine with xsd.exe generated code, but with XmlSchemaClassGenerator handling choice differently - I can't figure out how to work around the problem without changing xsd to use xs:any which is much worse than what xsd.exe generates.

@mganss
Copy link
Owner

mganss commented May 16, 2023

Use substitution groups:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">
    <xs:element name="Operation">
        <xs:complexType>
            <xs:sequence>                
                <xs:element 
                    ref="OperationElement" 
                    minOccurs="0" 
                    maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="OperationElement" type="OperationType"  abstract="true"/>
    <xs:element name="Plus" type="PlusType"  substitutionGroup="OperationElement"/>
    <xs:element name="Minus" type="MinusType" substitutionGroup="OperationElement"/>
    <xs:complexType name="OperationType" />
    <xs:complexType name="PlusType">
        <xs:complexContent>
            <xs:extension base="OperationType" />
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="MinusType">
        <xs:complexContent>
            <xs:extension base="OperationType" />
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

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