diff --git a/ballerina/tests/resources/xsd_tests/schemas/schema_1.xsd b/ballerina/tests/resources/xsd_tests/schemas/schema_1.xsd
new file mode 100644
index 00000000..786e70b5
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/schemas/schema_1.xsd
@@ -0,0 +1,3 @@
+
+
+
diff --git a/ballerina/tests/resources/xsd_tests/schemas/schema_2.xsd b/ballerina/tests/resources/xsd_tests/schemas/schema_2.xsd
new file mode 100644
index 00000000..bc9fb555
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/schemas/schema_2.xsd
@@ -0,0 +1,16 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/ballerina/tests/resources/xsd_tests/schemas/schema_3.xsd b/ballerina/tests/resources/xsd_tests/schemas/schema_3.xsd
new file mode 100644
index 00000000..94f57073
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/schemas/schema_3.xsd
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/ballerina/tests/resources/xsd_tests/schemas/schema_4.xsd b/ballerina/tests/resources/xsd_tests/schemas/schema_4.xsd
new file mode 100644
index 00000000..c18078c9
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/schemas/schema_4.xsd
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/ballerina/tests/resources/xsd_tests/schemas/schema_5.xsd b/ballerina/tests/resources/xsd_tests/schemas/schema_5.xsd
new file mode 100644
index 00000000..3d4dd6a8
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/schemas/schema_5.xsd
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/ballerina/tests/resources/xsd_tests/xml_values/schema_1_invalid_xml.xml b/ballerina/tests/resources/xsd_tests/xml_values/schema_1_invalid_xml.xml
new file mode 100644
index 00000000..c684f571
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/xml_values/schema_1_invalid_xml.xml
@@ -0,0 +1 @@
+Sample
diff --git a/ballerina/tests/resources/xsd_tests/xml_values/schema_1_valid_xml.xml b/ballerina/tests/resources/xsd_tests/xml_values/schema_1_valid_xml.xml
new file mode 100644
index 00000000..9f606074
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/xml_values/schema_1_valid_xml.xml
@@ -0,0 +1 @@
+Sample
diff --git a/ballerina/tests/resources/xsd_tests/xml_values/schema_2_invalid_xml.xml b/ballerina/tests/resources/xsd_tests/xml_values/schema_2_invalid_xml.xml
new file mode 100644
index 00000000..3ac2835b
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/xml_values/schema_2_invalid_xml.xml
@@ -0,0 +1,5 @@
+
+
+ Sample Title
+
+
diff --git a/ballerina/tests/resources/xsd_tests/xml_values/schema_2_valid_xml.xml b/ballerina/tests/resources/xsd_tests/xml_values/schema_2_valid_xml.xml
new file mode 100644
index 00000000..5f57d006
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/xml_values/schema_2_valid_xml.xml
@@ -0,0 +1,6 @@
+
+
+ Sample Title
+ Sample Author
+
+
diff --git a/ballerina/tests/resources/xsd_tests/xml_values/schema_3_invalid_xml.xml b/ballerina/tests/resources/xsd_tests/xml_values/schema_3_invalid_xml.xml
new file mode 100644
index 00000000..646ec8bd
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/xml_values/schema_3_invalid_xml.xml
@@ -0,0 +1,4 @@
+
+ John Doe
+ 30
+
diff --git a/ballerina/tests/resources/xsd_tests/xml_values/schema_3_valid_xml.xml b/ballerina/tests/resources/xsd_tests/xml_values/schema_3_valid_xml.xml
new file mode 100644
index 00000000..f34888a1
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/xml_values/schema_3_valid_xml.xml
@@ -0,0 +1,4 @@
+
+ John Doe
+ 30
+
diff --git a/ballerina/tests/resources/xsd_tests/xml_values/schema_4_invalid_xml.xml b/ballerina/tests/resources/xsd_tests/xml_values/schema_4_invalid_xml.xml
new file mode 100644
index 00000000..110e9428
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/xml_values/schema_4_invalid_xml.xml
@@ -0,0 +1,4 @@
+
+ Toyota
+ Yamaha
+
diff --git a/ballerina/tests/resources/xsd_tests/xml_values/schema_4_valid_xml.xml b/ballerina/tests/resources/xsd_tests/xml_values/schema_4_valid_xml.xml
new file mode 100644
index 00000000..81605952
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/xml_values/schema_4_valid_xml.xml
@@ -0,0 +1,3 @@
+
+ Toyota
+
diff --git a/ballerina/tests/resources/xsd_tests/xml_values/schema_5_invalid_xml.xml b/ballerina/tests/resources/xsd_tests/xml_values/schema_5_invalid_xml.xml
new file mode 100644
index 00000000..e50c87f2
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/xml_values/schema_5_invalid_xml.xml
@@ -0,0 +1,3 @@
+
+ 12345
+
diff --git a/ballerina/tests/resources/xsd_tests/xml_values/schema_5_valid_xml.xml b/ballerina/tests/resources/xsd_tests/xml_values/schema_5_valid_xml.xml
new file mode 100644
index 00000000..98e265fc
--- /dev/null
+++ b/ballerina/tests/resources/xsd_tests/xml_values/schema_5_valid_xml.xml
@@ -0,0 +1,4 @@
+
+ 12345
+ Laptop
+
diff --git a/ballerina/tests/test_from_json.bal b/ballerina/tests/test_from_json.bal
new file mode 100644
index 00000000..2adb500a
--- /dev/null
+++ b/ballerina/tests/test_from_json.bal
@@ -0,0 +1,38 @@
+import ballerina/test;
+
+@test:Config {groups: ["xsd", "to_xml"], dataProvider: fromJsonDataProvider}
+function testFromJson(json value, xml expected) returns error?{
+ xml|Error xmlResult = fromJson(value);
+ test:assertEquals(xmlResult, expected);
+}
+
+function fromJsonDataProvider() returns [json, xml][] {
+ return [
+ [{a: {b: 2, c: 3}}, xml `23`],
+ [{a: {a: 1}}, xml `1`],
+ [{a: {d: 4, e: {f: 5, g: "text"}}}, xml `45text`],
+ [{root: {nested: {value1: "example", value2: 10}}}, xml `example10`],
+ [{book: {title: "XML Guide", author: "John Doe", year: 2024}}, xml `XML GuideJohn Doe2024`],
+ [{library: {section: {book1: "Book A", book2: "Book B"}}}, xml ``],
+ [{person: {name: "Alice", details: {age: 30, city: "Wonderland"}}}, xml `Alice30Wonderland `],
+ [{catalog: {item: [{id: 1, name: "Item 1"}, {id: 2, name: "Item 2"}]}}, xml `- 1Item 1
- 2Item 2
`],
+ [{company: {employee: {id: 1001, name: "Bob", department: "Engineering"}}}, xml `1001BobEngineering`],
+ [{'order: {orderId: 5001, items: {item1: "Widget", item2: "Gadget"}}}, xml `5001WidgetGadget`],
+ [{menu: {dish: [{name: "Pasta", price: 12.5}, {name: "Salad", price: 8.0}]}}, xml `
`],
+ [{report: {entries: [{date: "2024-10-01", detail: "Entry 1"}, {date: "2024-10-02", detail: "Entry 2"}]}}, xml `2024-10-01Entry 12024-10-02Entry 2`],
+ [{shoppingList: {items: [{item: "Apples", quantity: 5}, {item: "Bananas", quantity: 3}]}}, xml `- Apples
5- Bananas
3`],
+ [{conference: {session: [{topic: "AI Trends", speaker: "Dr. Smith"}, {topic: "Web 3.0", speaker: "Jane Doe"}]}}, xml `AI TrendsDr. SmithWeb 3.0Jane Doe`],
+ [{project: {tasks: [{title: "Setup Environment", status: "Completed"}, {title: "Develop Module", status: "In Progress"}]}}, xml `Setup EnvironmentCompletedDevelop ModuleIn Progress`],
+ [{school: {students: [{name: "Emily", grade: "A"}, {name: "Michael", grade: "B"}]}}, xml `EmilyAMichaelB`],
+ [{portfolio: {stocks: [{symbol: "AAPL", shares: 50}, {symbol: "TSLA", shares: 30}]}}, xml `AAPL50TSLA30`],
+ [{university: {course: [{name: "Mathematics", credits: 4}, {name: "History", credits: 3}]}}, xml `Mathematics4History3`],
+ [{research: {papers: [{title: "Quantum Computing", author: "Alice Cooper"}, {title: "Blockchain Advances", author: "John Smith"}]}}, xml `Quantum ComputingAlice CooperBlockchain AdvancesJohn Smith`],
+ [{movieCollection: {movies: [{title: "Inception", director: "Nolan"}, {title: "Interstellar", director: "Nolan"}]}}, xml `InceptionNolanInterstellarNolan`],
+ [{library: {books: [{title: "XML Guide", author: "John Doe", year: 2024}, {title: "JSON Primer", author: "Jane Smith", year: 2023}]}}, xml `XML GuideJohn Doe2024JSON PrimerJane Smith2023`],
+ [{shoppingList: {items: [{item: "Apples", quantity: 5}, {item: "Bananas", quantity: 3}]}}, xml `- Apples
5- Bananas
3`],
+ [{project: {tasks: [{title: "Setup Environment", status: "Completed"}, {title: "Develop Module", status: "In Progress"}]}}, xml `Setup EnvironmentCompletedDevelop ModuleIn Progress`],
+ [{school: {students: [{name: "Emily", grade: "A"}, {name: "Michael", grade: "B"}]}}, xml `EmilyAMichaelB`],
+ [{conference: {sessions: [{topic: "AI Trends", speaker: "Dr. Smith"}, {topic: "Web 3.0", speaker: "Jane Doe"}]}}, xml `AI TrendsDr. SmithWeb 3.0Jane Doe`],
+ [{catalog: {items: [{id: 1, name: "Item 1"}, {id: 2, name: "Item 2"}]}}, xml `1Item 12Item 2`]
+ ];
+}
diff --git a/ballerina/tests/toXml_test.bal b/ballerina/tests/toXml_test.bal
index b9630914..47694791 100644
--- a/ballerina/tests/toXml_test.bal
+++ b/ballerina/tests/toXml_test.bal
@@ -95,7 +95,7 @@ function testToXmlWithNameAnnotation() returns error? {
xml xmlVal1 = xml `1234`;
Rec2 rec1 = check parseAsType(xmlVal1);
xml result = check toXml(rec1);
- test:assertTrue(result == xmlVal1);
+ test:assertEquals(result, xmlVal1);
}
@test:Config {
diff --git a/ballerina/tests/xsd_choice_array_test.bal b/ballerina/tests/xsd_choice_array_test.bal
new file mode 100644
index 00000000..d5e98c41
--- /dev/null
+++ b/ballerina/tests/xsd_choice_array_test.bal
@@ -0,0 +1,335 @@
+// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+type XsdChoiceArray record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 2
+ }
+ Choice_XsdChoiceArray choice_XsdChoiceArray;
+|};
+
+type Choice_XsdChoiceArray record {|
+ int[] age?;
+ float[] salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXsdChoiceArray() returns error? {
+ string xmlStr;
+ XsdChoiceArray|Error v;
+
+ xmlStr = string `1311.1`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XsdChoiceArray: {age: [13], salary: [11.1]}});
+
+ xmlStr = string `1312`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XsdChoiceArray: {age: [13, 12]}});
+
+ xmlStr = string `11.112.1`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XsdChoiceArray: {salary: [11.1, 12.1]}});
+
+ xmlStr = string `1311.11414.11515.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'choice_XsdChoiceArray' occurs more than the max allowed times");
+}
+
+type XsdChoiceArray2 record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 2
+ }
+ Choice_XsdChoiceArray2 choice_XsdChoiceArray2;
+
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 2
+ }
+ Choice_XsdChoiceArray2_2 choice_XsdChoiceArray2_2?;
+|};
+
+type Choice_XsdChoiceArray2 record {|
+ int[] age?;
+ float[] salary?;
+|};
+
+type Choice_XsdChoiceArray2_2 record {|
+ int[] age2?;
+ float[] salary2?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXsdChoiceArray2() returns error? {
+ string xmlStr;
+ XsdChoiceArray2|Error v;
+
+ xmlStr = string `1311.11311.1`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XsdChoiceArray2: {age: [13], salary: [11.1]}, choice_XsdChoiceArray2_2: {age2: [13], salary2: [11.1]}});
+
+ xmlStr = string `13131313`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XsdChoiceArray2: {age: [13, 13]}, choice_XsdChoiceArray2_2: {age2: [13, 13]}});
+
+ xmlStr = string `13131311.1`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XsdChoiceArray2: {age: [13, 13]}, choice_XsdChoiceArray2_2: {age2: [13], salary2: [11.1]}});
+
+ xmlStr = string `13131311.11415`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'choice_XsdChoiceArray2_2' occurs more than the max allowed times");
+
+ xmlStr = string `13131313`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'choice_XsdChoiceArray2' occurs more than the max allowed times");
+
+ xmlStr = string `1313`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XsdChoiceArray2: {age: [13, 13]}});
+
+ xmlStr = string `1313`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "required field 'choice_XsdChoiceArray2' not present in XML");
+}
+
+type XSDChoiceArrayRecord13 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Choice_XSDChoiceArrayRecord13_1 choice_XSDChoiceArrayRecord13_1;
+
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Choice_XSDChoiceArrayRecord13_2 choice_XSDChoiceArrayRecord13_2;
+};
+
+type Choice_XSDChoiceArrayRecord13_1 record {
+ Choice_Array_A_3[] field1?;
+ Choice_Array_B_3[] field2?;
+ Choice_Array_C_3[] field3?;
+};
+
+type Choice_XSDChoiceArrayRecord13_2 record {
+ Choice_Array_D_3[] field4?;
+ Choice_Array_E_3[] field5?;
+ Choice__Array_F_3[] field6?;
+};
+
+type Choice_Array_A_3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Choice_Array_3 value1;
+};
+
+type Choice_Array_B_3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Choice2_Array_3 value2;
+};
+
+type Choice_Array_C_3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Choice3_Array_3 value3;
+};
+
+type Choice_Array_D_3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Choice_Array_3 value1;
+};
+
+type Choice_Array_E_3 record {
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Choice2_Array_3 value2;
+};
+
+type Choice__Array_F_3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Choice3_Array_3 value3;
+};
+
+type Choice_Array_3 record {
+ string[] a?;
+ string[] b?;
+ string[] c?;
+};
+
+type Choice2_Array_3 record {
+ string[] d?;
+ string[] e?;
+ string[] f?;
+};
+
+type Choice3_Array_3 record {
+ string[] g?;
+ string[] h?;
+ string[] i?;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXSDChoiceArrayRecord4() returns error? {
+ string xmlStr = string `123123123123123123`;
+ XSDChoiceArrayRecord13|Error v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceArrayRecord13_1: {field1: [{value1: {a: ["1"], b:["2"], c: ["3"]}}],field2: [{value2: {d: ["1"], e: ["2"], f: ["3"]}}], field3: [{value3: {g: ["1"], h: ["2"], i: ["3"]}}]}, choice_XSDChoiceArrayRecord13_2: {field4: [{value1: {a: ["1"], b: ["2"], c: ["3"]}}], field5: [{value2: {d: ["1"], e: ["2"], f:["3"]}}], field6: [{value3: {g: ["1"], h: ["2"], i: ["3"]}}]}});
+
+ xmlStr = string `22123233123123123`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceArrayRecord13_1: {field1: [{value1: {b:["2", "2"]}}, {value1: {a: ["1"], b:["2"], c: ["3"]}}], field3: [{value3: {h: ["2"], i: ["3", "3"]}}]}, choice_XSDChoiceArrayRecord13_2: {field5: [{value2: {d: ["1"], e: ["2"], f:["3"]}}, {value2: {d: ["1"], e: ["2"], f:["3"]}}], field6: [{value3: {g: ["1"], h: ["2"], i: ["3"]}}]}});
+
+ xmlStr = string `22123233123`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceArrayRecord13_2' occurs less than the min required times");
+
+ xmlStr = string `221232331123123`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is error);
+ test:assertEquals((v2).message(), "'value2' occurs less than the min required times");
+}
+
+type XsdChoiceArray5 record {|
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Choice_XsdChoiceArray5 choice_XsdChoiceArray5;
+|};
+
+type Choice_XsdChoiceArray5 record {|
+ int[] age?;
+ float[] salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXsdChoiceArray5() returns error? {
+ string xmlStr;
+ XsdChoiceArray5|Error v;
+
+ xmlStr = string `1311.114`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XsdChoiceArray5: {age: [13, 14], salary: [11.1]}});
+
+ xmlStr = string `1311.1`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XsdChoiceArray5: {age: [13], salary: [11.1]}});
+
+ xmlStr = string `1311.11414.11515.11515.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XsdChoiceArray5' occurs more than the max allowed times");
+
+ xmlStr = string `13`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XsdChoiceArray5' occurs less than the min required times");
+}
+
+type XSDChoiceArrayRecord6 record {
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 4
+ }
+ Choice_XSDChoiceArrayRecord6_1 choice_XSDChoiceArrayRecord6_1;
+
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 4
+ }
+ Choice_XSDChoiceArrayRecord6_2 choice_XSDChoiceArrayRecord6_2;
+};
+
+type Choice_XSDChoiceArrayRecord6_1 record {
+ Choice_Array_A_6[] field1?;
+ Choice_Array_B_6[] field2?;
+};
+
+type Choice_XSDChoiceArrayRecord6_2 record {
+ Choice_Array_D_6[] field4?;
+ Choice_Array_E_6[] field5?;
+};
+
+type Choice_Array_A_6 record {
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Choice_Array_6 value1;
+};
+
+type Choice_Array_B_6 record {
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Choice2_Array_6 value2;
+};
+
+type Choice_Array_D_6 record {
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 4
+ }
+ Choice_Array_6 value1;
+};
+
+type Choice_Array_E_6 record {
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 4
+ }
+ Choice2_Array_6 value2;
+};
+
+type Choice_Array_6 record {
+ string[] a?;
+};
+
+type Choice2_Array_6 record {
+ string[] d?;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXSDChoiceArrayRecord6() returns error? {
+ string xmlStr = string `1111111111111111`;
+ XSDChoiceArrayRecord6|Error v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceArrayRecord6_1: {field1: [{value1: {a: ["1", "1"]}}, {value1: {a:["1", "1"]}}], field2: [{value2: {d: ["1", "1"]}}, {value2: {d: ["1", "1"]}}]}, choice_XSDChoiceArrayRecord6_2: {field4: [{value1: {a: ["1", "1"]}}, {value1: {a:["1", "1"]}}], field5: [{value2: {d: ["1", "1"]}}, {value2: {d: ["1","1"]}}]}});
+}
diff --git a/ballerina/tests/xsd_choice_array_test_with_parse_type.bal b/ballerina/tests/xsd_choice_array_test_with_parse_type.bal
new file mode 100644
index 00000000..9ee44904
--- /dev/null
+++ b/ballerina/tests/xsd_choice_array_test_with_parse_type.bal
@@ -0,0 +1,264 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+@Name {
+ value: "Root"
+}
+type XsdChoiceArrayWithXmlValue record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 2
+ }
+ Choice_XsdChoiceArrayWithXmlValue choice_XsdChoiceArrayWithXmlValue;
+|};
+
+type Choice_XsdChoiceArrayWithXmlValue record {|
+ int[] age?;
+ float[] salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXsdChoiceArrayWithXmlValue() returns error? {
+ xml xmlValue;
+ XsdChoiceArrayWithXmlValue|Error v;
+
+ xmlValue = xml `1311.1`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XsdChoiceArrayWithXmlValue: {age: [13], salary: [11.1]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `1312`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XsdChoiceArrayWithXmlValue: {age: [13, 12]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `11.112.1`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XsdChoiceArrayWithXmlValue: {salary: [11.1, 12.1]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `1311.11414.11515.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'choice_XsdChoiceArrayWithXmlValue' occurs more than the max allowed times");
+ xml|Error toXmlResult = toXml({choice_XsdChoiceArrayWithXmlValue: {age: [13, 14, 15], salary: [11.1, 14.1, 15.1]}});
+ test:assertTrue(toXmlResult is error);
+ test:assertEquals((toXmlResult).message(), "'choice_XsdChoiceArrayWithXmlValue' occurs more than the max allowed times");
+}
+
+@Name {
+ value: "Root"
+}
+type XsdChoiceArrayWithXmlValue2 record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 2
+ }
+ Choice_XsdChoiceArrayWithXmlValue2 choice_XsdChoiceArrayWithXmlValue2;
+
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 2
+ }
+ Choice_XsdChoiceArrayWithXmlValue2_2 choice_XsdChoiceArrayWithXmlValue2_2?;
+|};
+
+type Choice_XsdChoiceArrayWithXmlValue2 record {|
+ int[] age?;
+ float[] salary?;
+|};
+
+type Choice_XsdChoiceArrayWithXmlValue2_2 record {|
+ int[] age2?;
+ float[] salary2?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXsdChoiceArrayWithXmlValue2() returns error? {
+ xml xmlValue;
+ XsdChoiceArrayWithXmlValue2|Error v;
+
+ xmlValue = xml `1311.11311.1`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XsdChoiceArrayWithXmlValue2: {age: [13], salary: [11.1]}, choice_XsdChoiceArrayWithXmlValue2_2: {age2: [13], salary2: [11.1]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `13131313`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XsdChoiceArrayWithXmlValue2: {age: [13, 13]}, choice_XsdChoiceArrayWithXmlValue2_2: {age2: [13, 13]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `13131311.1`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XsdChoiceArrayWithXmlValue2: {age: [13, 13]}, choice_XsdChoiceArrayWithXmlValue2_2: {age2: [13], salary2: [11.1]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `13131311.11415`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'choice_XsdChoiceArrayWithXmlValue2_2' occurs more than the max allowed times");
+ xml|Error toXmlResult = toXml({choice_XsdChoiceArrayWithXmlValue2: {age: [13], salary: []}, choice_XsdChoiceArrayWithXmlValue2_2: {age2: [13, 14, 15], salary2: [11.1]}});
+ test:assertTrue(toXmlResult is error);
+ test:assertEquals((toXmlResult).message(), "'choice_XsdChoiceArrayWithXmlValue2_2' occurs more than the max allowed times");
+
+ xmlValue = xml `13131313`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'choice_XsdChoiceArrayWithXmlValue2' occurs more than the max allowed times");
+ toXmlResult = toXml({choice_XsdChoiceArrayWithXmlValue2: {age: [13, 14, 15], salary: [11.1, 14.1, 15.1]}});
+ test:assertTrue(toXmlResult is error);
+ test:assertEquals((toXmlResult).message(), "'choice_XsdChoiceArrayWithXmlValue2' occurs more than the max allowed times");
+
+ xmlValue = xml `1313`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XsdChoiceArrayWithXmlValue2: {age: [13, 13]}});
+
+ xmlValue = xml `1313`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "required field 'choice_XsdChoiceArrayWithXmlValue2' not present in XML");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13_1 choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13_1;
+
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13_2 choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13_2;
+};
+
+type Choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13_1 record {
+ Choice_Array_A_3[] field1?;
+ Choice_Array_B_3[] field2?;
+ Choice_Array_C_3[] field3?;
+};
+
+type Choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13_2 record {
+ Choice_Array_D_3[] field4?;
+ Choice_Array_E_3[] field5?;
+ Choice__Array_F_3[] field6?;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord4() returns error? {
+ xml xmlValue = xml `123123123123123123`;
+ XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13|Error v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13_1: {field1: [{value1: {a: ["1"], b:["2"], c: ["3"]}}],field2: [{value2: {d: ["1"], e: ["2"], f: ["3"]}}], field3: [{value3: {g: ["1"], h: ["2"], i: ["3"]}}]}, choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13_2: {field4: [{value1: {a: ["1"], b: ["2"], c: ["3"]}}], field5: [{value2: {d: ["1"], e: ["2"], f:["3"]}}], field6: [{value3: {g: ["1"], h: ["2"], i: ["3"]}}]}});
+ test:assertEquals(toXml(check v2), xmlValue);
+
+ xmlValue = xml `22123233123123123`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13_1: {field1: [{value1: {b:["2", "2"]}}, {value1: {a: ["1"], b:["2"], c: ["3"]}}], field3: [{value3: {h: ["2"], i: ["3", "3"]}}]}, choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13_2: {field5: [{value2: {d: ["1"], e: ["2"], f:["3"]}}, {value2: {d: ["1"], e: ["2"], f:["3"]}}], field6: [{value3: {g: ["1"], h: ["2"], i: ["3"]}}]}});
+ test:assertEquals(toXml(check v2), xmlValue);
+
+ xmlValue = xml `22123233123`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord13_2' occurs less than the min required times");
+
+ xmlValue = xml `221232331123123`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is error);
+ test:assertEquals((v2).message(), "'value2' occurs less than the min required times");
+}
+
+@Name {
+ value: "Root"
+}
+type XsdChoiceArrayWithXmlValue5 record {|
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Choice_XsdChoiceArrayWithXmlValue5 choice_XsdChoiceArrayWithXmlValue5;
+|};
+
+type Choice_XsdChoiceArrayWithXmlValue5 record {|
+ int[] age?;
+ float[] salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXsdChoiceArrayWithXmlValue5() returns error? {
+ xml xmlValue;
+ XsdChoiceArrayWithXmlValue5|Error v;
+
+ xmlValue = xml `1311.114`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XsdChoiceArrayWithXmlValue5: {age: [13, 14], salary: [11.1]}});
+ test:assertEquals(toXml(check v), xml `131411.1`);
+
+ xmlValue = xml `1311.1`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XsdChoiceArrayWithXmlValue5: {age: [13], salary: [11.1]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `1311.11414.11515.11515.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XsdChoiceArrayWithXmlValue5' occurs more than the max allowed times");
+
+ xmlValue = xml `13`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XsdChoiceArrayWithXmlValue5' occurs less than the min required times");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord6 record {
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 4
+ }
+ Choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord6_1 choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord6_1;
+
+ @Choice {
+ minOccurs: 2,
+ maxOccurs: 4
+ }
+ Choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord6_2 choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord6_2;
+};
+
+type Choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord6_1 record {
+ Choice_Array_A_6[] field1?;
+ Choice_Array_B_6[] field2?;
+};
+
+type Choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord6_2 record {
+ Choice_Array_D_6[] field4?;
+ Choice_Array_E_6[] field5?;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord6() returns error? {
+ xml xmlValue = xml `1111111111111111`;
+ XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord6|Error v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord6_1: {field1: [{value1: {a: ["1", "1"]}}, {value1: {a:["1", "1"]}}], field2: [{value2: {d: ["1", "1"]}}, {value2: {d: ["1", "1"]}}]}, choice_XSDChoiceArrayWithXmlValueXsdChoiceArrayWithXmlValueRecord6_2: {field4: [{value1: {a: ["1", "1"]}}, {value1: {a:["1", "1"]}}], field5: [{value2: {d: ["1", "1"]}}, {value2: {d: ["1","1"]}}]}});
+ test:assertEquals(toXml(check v2), xml `1111111111111111`);
+}
diff --git a/ballerina/tests/xsd_choice_test.bal b/ballerina/tests/xsd_choice_test.bal
new file mode 100644
index 00000000..24509d64
--- /dev/null
+++ b/ballerina/tests/xsd_choice_test.bal
@@ -0,0 +1,646 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+type XSDChoiceRecord record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord choice_XSDChoiceRecord?;
+|};
+
+type Choice_XSDChoiceRecord record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXsdChoice() returns error? {
+ string xmlStr;
+ XSDChoiceRecord|Error v;
+
+ xmlStr = string `10`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XSDChoiceRecord: {age: 10}});
+
+ xmlStr = string `10.5`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XSDChoiceRecord: {salary: 10.5}});
+
+ xmlStr = string `1011.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceRecord' occurs more than the max allowed times");
+
+ xmlStr = string `11.111.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceRecord' occurs more than the max allowed times");
+
+ xmlStr = string ``;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceRecord' occurs less than the min required times");
+}
+
+type XSDChoiceRecordP2 record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecordP2 choice_XSDChoiceRecordP2;
+|};
+
+type Choice_XSDChoiceRecordP2 record {|
+ @Element {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ int[] age?;
+ float salary?;
+ @Element {
+ minOccurs: 1,
+ maxOccurs: 2
+ }
+ string[] name?;
+|};
+
+type XSDChoiceP1Record record {|
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceP1Record choice_XSDChoiceP1Record?;
+|};
+
+type Choice_XSDChoiceP1Record record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXsdChoiceP1() returns error? {
+ string xmlStr;
+ XSDChoiceP1Record|Error v;
+
+ xmlStr = string `10`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XSDChoiceP1Record: {age: 10}});
+
+ xmlStr = string `10.5`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XSDChoiceP1Record: {salary: 10.5}});
+
+ xmlStr = string `1011.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceP1Record' occurs more than the max allowed times");
+
+ xmlStr = string `11.111.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceP1Record' occurs more than the max allowed times");
+
+ xmlStr = string ``;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {});
+}
+
+@test:Config {groups: ["xsd", "xsd_Choice"]}
+function testXsdChoiceP2() returns error? {
+ string xmlStr = string `10`;
+ XSDChoiceRecordP2|Error v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XSDChoiceRecordP2: {age: [10]}});
+
+ xmlStr = string `ABC`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XSDChoiceRecordP2: {name: ["ABC"]}});
+
+ xmlStr = string `11.1`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XSDChoiceRecordP2: {salary: 11.1}});
+
+ xmlStr = string `10101011.1ABC`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceRecordP2' occurs more than the max allowed times");
+
+ xmlStr = string `10ABC11.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceRecordP2' occurs more than the max allowed times");
+}
+
+type XSDChoiceRecord2 record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord2 choice_XSDChoiceRecord2;
+
+ int num;
+|};
+
+type Choice_XSDChoiceRecord2 record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoice2() returns error? {
+ string xmlStr = string `310`;
+ XSDChoiceRecord2|Error v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XSDChoiceRecord2: {age: 10}, num: 3});
+ test:assertEquals((check v).choice_XSDChoiceRecord2.age, 10);
+ test:assertEquals((check v).num, 3);
+
+ xmlStr = string `11.13`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {choice_XSDChoiceRecord2: {salary: 11.1}, num: 3});
+ test:assertEquals((check v).choice_XSDChoiceRecord2.salary, 11.1);
+ test:assertEquals((check v).num, 3);
+
+ xmlStr = string `11.1103`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceRecord2' occurs more than the max allowed times");
+
+ xmlStr = string `10311.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceRecord2' occurs more than the max allowed times");
+}
+
+type XSDChoiceRecord3 record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord3 choice_XSDChoiceRecord3;
+
+ record{int n;} num;
+|};
+
+type Choice_XSDChoiceRecord3 record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoice3() returns error? {
+ string xmlStr = string `310`;
+ XSDChoiceRecord3|Error v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord3: {age: 10}, num: {n: 3}});
+ test:assertEquals((check v2).choice_XSDChoiceRecord3.age, 10);
+ test:assertEquals((check v2).num, {n: 3});
+
+ xmlStr = string `11.13`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord3: {salary: 11.1}, num: {n: 3}});
+
+ xmlStr = string `10311.1`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord3' occurs more than the max allowed times");
+
+ xmlStr = string `31011.1`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord3' occurs more than the max allowed times");
+}
+
+type XSDChoiceRecord4 record {|
+ record{record {int n;} n;} num;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord4 choice_XSDChoiceRecord4;
+|};
+
+type Choice_XSDChoiceRecord4 record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoice4() returns error? {
+ string xmlStr = string `310`;
+ XSDChoiceRecord4|Error v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord4: {age: 10}, num: {n: {n: 3}}});
+ test:assertEquals((check v2).choice_XSDChoiceRecord4.age, 10);
+ test:assertEquals((check v2).num, {n: {n: 3}});
+
+ xmlStr = string `11.13`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord4: {salary: 11.1}, num: {n: {n: 3}}});
+
+ xmlStr = string `10311.1`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord4' occurs more than the max allowed times");
+
+ xmlStr = string `31011.1`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord4' occurs more than the max allowed times");
+}
+
+type XSDChoiceRecord5 record {|
+ record{record {int n;} n;} num;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord5 choice_XSDChoiceRecord5;
+ record{record {int n;} n;} num2;
+|};
+
+type Choice_XSDChoiceRecord5 record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoice5() returns error? {
+ string xmlStr = string `3310`;
+ XSDChoiceRecord5|Error v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord5: {age: 10}, num: {n: {n: 3}}, num2: {n: {n: 3}}});
+
+ xmlStr = string `311.13`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord5: {salary: 11.1}, num: {n: {n: 3}}, num2: {n: {n: 3}}});
+
+ xmlStr = string `11.133`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord5: {salary: 11.1}, num: {n: {n: 3}}, num2: {n: {n: 3}}});
+
+ xmlStr = string `103311.1`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord5' occurs more than the max allowed times");
+
+ xmlStr = string `33`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "required field 'choice_XSDChoiceRecord5' not present in XML");
+}
+
+type XSDChoiceRecord6 record {|
+ record{record {int n;} n;} num;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord6_1 choice_XSDChoiceRecord6_1;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord6_2 choice_XSDChoiceRecord6_2;
+ record{record {int n;} n;} num2;
+|};
+
+type Choice_XSDChoiceRecord6_1 record {|
+ int age?;
+ float salary?;
+|};
+
+type Choice_XSDChoiceRecord6_2 record {|
+ string name?;
+ string status?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoice6() returns error? {
+ string xmlStr = string `3success310`;
+ XSDChoiceRecord6|Error v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord6_1: {age: 10}, num: {n: {n: 3}}, num2: {n: {n: 3}}, choice_XSDChoiceRecord6_2: {status: "success"}});
+ test:assertEquals((check v2).choice_XSDChoiceRecord6_1.age, 10);
+ test:assertEquals((check v2).choice_XSDChoiceRecord6_2.status, "success");
+ test:assertEquals((check v2).num, {n: {n: 3}});
+ test:assertEquals((check v2).num2, {n: {n: 3}});
+
+ xmlStr = string `SD33`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "required field 'choice_XSDChoiceRecord6_1' not present in XML");
+
+ xmlStr = string `33SD`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "required field 'choice_XSDChoiceRecord6_1' not present in XML");
+
+ xmlStr = string `SDsuccess33`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord6_2' occurs more than the max allowed times");
+}
+
+type XSDChoiceRecord7 record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord7_1 choice_XSDChoiceRecord7_1;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord7_2 choice_XSDChoiceRecord7_2;
+|};
+
+type Choice_XSDChoiceRecord7_1 record {|
+ int age?;
+ float salary?;
+|};
+
+type Choice_XSDChoiceRecord7_2 record {|
+ string name?;
+ string status?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoice7() returns error? {
+ string xmlStr = string `success11.1`;
+ XSDChoiceRecord7|Error v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord7_1: {salary: 11.1}, choice_XSDChoiceRecord7_2: {status: "success"}});
+}
+
+type XSDChoiceRecord8 record {|
+ XSDChoiceRecord8P test;
+ int a;
+|};
+
+type XSDChoiceRecord8P record {|
+ record{record {int n;} n;} num;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord8_1 choice_XSDChoiceRecord8_1;
+
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord8_2 choice_XSDChoiceRecord8_2;
+ record{record {int n;} n;} num2;
+|};
+
+type Choice_XSDChoiceRecord8_1 record {|
+ int age?;
+ float salary?;
+|};
+
+type Choice_XSDChoiceRecord8_2 record {|
+ RecChoice8 name?;
+ RecChoice8 status?;
+|};
+
+type RecChoice8 record {|
+ string value1;
+ string value2;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoice8() returns error? {
+ string xmlStr;
+ XSDChoiceRecord8|Error v2;
+
+ xmlStr = string `3SDAB3102`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {a: 2, test: {choice_XSDChoiceRecord8_1: {age: 10}, num: {n: {n: 3}}, num2: {n: {n: 3}}, choice_XSDChoiceRecord8_2: {name: {value1: "SD", value2: "AB"}}}});
+ test:assertEquals((check v2).test.choice_XSDChoiceRecord8_1.age, 10);
+ test:assertEquals((check v2).test.choice_XSDChoiceRecord8_2.name, {value1: "SD", value2: "AB"});
+ test:assertEquals((check v2).test.num, {n: {n: 3}});
+ test:assertEquals((check v2).test.num2, {n: {n: 3}});
+
+ xmlStr = string `SuccessFail3311.12`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {a: 2, test: {choice_XSDChoiceRecord8_1: {salary: 11.1}, num: {n: {n: 3}}, num2: {n: {n: 3}}, choice_XSDChoiceRecord8_2: {status: {value1: "Success", value2: "Fail"}}}});
+ test:assertEquals((check v2).test.choice_XSDChoiceRecord8_1.salary, 11.1);
+ test:assertEquals((check v2).test.num, {n: {n: 3}});
+ test:assertEquals((check v2).test.num2, {n: {n: 3}});
+
+ xmlStr = string `33SuccessFail11.12`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {a: 2, test: {choice_XSDChoiceRecord8_1: {salary: 11.1}, num: {n: {n: 3}}, num2: {n: {n: 3}}, choice_XSDChoiceRecord8_2: {status: {value1: "Success", value2: "Fail"}}}});
+
+ xmlStr = string `SDAB10SuccessFail11.12`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord8_2' occurs more than the max allowed times");
+
+ xmlStr = string `10SuccessFail11.12`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord8_1' occurs more than the max allowed times");
+
+ xmlStr = string `102`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord8_2' occurs less than the min required times");
+
+ xmlStr = string `SuccessFail2`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord8_1' occurs less than the min required times");
+}
+
+type XSDChoiceRecord9 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord9_1 choice_XSDChoiceRecord9_1;
+};
+
+type Choice_XSDChoiceRecord9_1 record {
+ Choice_A field1?;
+ Choice_B field2?;
+ Choice_C field3?;
+};
+
+type Choice_A record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice value1;
+};
+
+type Choice_B record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice value2;
+};
+
+type Choice_C record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice value3;
+};
+
+type Choice record {
+ string a?;
+ string b?;
+ string c?;
+};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoice9() returns error? {
+ string xmlStr;
+ XSDChoiceRecord9|Error v2;
+
+ xmlStr = string `1`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord9_1: {field2: {value2: {a: "1"}}}});
+
+ xmlStr = string `1`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord9_1: {field3: {value3: {c: "1"}}}});
+
+ xmlStr = string `11`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord9_1' occurs more than the max allowed times");
+
+ xmlStr = string `11`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'value1' occurs more than the max allowed times");
+}
+
+type XSDChoiceRecord10 record {
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord10_1 choice_XSDChoiceRecord10_1?;
+
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceRecord10_2 choice_XSDChoiceRecord10_2;
+};
+
+type Choice_XSDChoiceRecord10_1 record {
+ Choice_A_10 field1?;
+ Choice_B_10 field2?;
+ Choice_C_10 field3?;
+};
+
+type Choice_XSDChoiceRecord10_2 record {
+ Choice_D_10 field4?;
+ Choice_E_10 field5?;
+ Choice_F_10 field6?;
+};
+
+type Choice_A_10 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_10 value1;
+};
+
+type Choice_B_10 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice2_10 value2;
+};
+
+type Choice_C_10 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice3_10 value3;
+};
+
+type Choice_D_10 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_10 value1;
+};
+
+type Choice_E_10 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice2_10 value2;
+};
+
+type Choice_F_10 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice3_10 value3;
+};
+
+type Choice_10 record {
+ string a?;
+ string b?;
+ string c?;
+};
+
+type Choice2_10 record {
+ string d?;
+ string e?;
+ string f?;
+};
+
+type Choice3_10 record {
+ string g?;
+ string h?;
+ string i?;
+};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoice10() returns error? {
+ string xmlStr = string `12`;
+ XSDChoiceRecord10|Error v2 = parseString(xmlStr);
+ test:assertEquals(v2, {choice_XSDChoiceRecord10_1: {field1: {value1: {a: "1"}}}, choice_XSDChoiceRecord10_2: {field5: {value2: {"d": "2"}}}});
+
+ xmlStr = string `112`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord10_1' occurs more than the max allowed times");
+
+ xmlStr = string `122`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceRecord10_2' occurs more than the max allowed times");
+
+ xmlStr = string `112`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'value2' occurs more than the max allowed times");
+}
diff --git a/ballerina/tests/xsd_choice_test_with_element_annotation.bal b/ballerina/tests/xsd_choice_test_with_element_annotation.bal
new file mode 100644
index 00000000..f70664aa
--- /dev/null
+++ b/ballerina/tests/xsd_choice_test_with_element_annotation.bal
@@ -0,0 +1,359 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+type XsdChoiceWithElementAnnotation record {
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_EA1 seq_EA1?;
+};
+
+type Choice_EA1 record {
+
+ @Element {
+ maxOccurs: 2,
+ minOccurs: 0
+ }
+ string[] EA1?;
+
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ string EA2?;
+
+ @Element {
+ maxOccurs: 4,
+ minOccurs: 2
+ }
+ string[] EA3?;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice", "xsd_element", "xsd_element_and_sequence"]}
+function testXsdChoiceWithElementAnnotation() returns error? {
+ string xmlStr;
+ XsdChoiceWithElementAnnotation|Error v;
+
+ xmlStr = string `ABC`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC"]}});
+
+ xmlStr = string ``;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {});
+
+ xmlStr = string `ABABABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_EA1: {EA3: ["AB", "AB", "AB", "AB"]}});
+
+ xmlStr = string `ABC`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA1":{"EA2": "ABC"}});
+
+ xmlStr = string `ABABABABAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs more than the max allowed times");
+
+ xmlStr = string `ABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA1":{EA3: ["AB", "AB"]}});
+
+ xmlStr = string `ABABABABABAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs more than the max allowed times");
+
+ xmlStr = string `AB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlStr = string `ABCABC`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC", "ABC"]}});
+
+ xmlStr = string `ABC`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC"]}});
+
+ xmlStr = string `ABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlStr = string `AB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlStr = string `ABCABCABC`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA1' occurs more than the max allowed times");
+
+ xmlStr = string `ABCABCABCABCABC`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'seq_EA1' occurs more than the max allowed times");
+}
+
+type XsdChoiceWithElementAnnotation2 record {
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_EA2 seq_EA2;
+};
+
+type Choice_EA2 record {
+ record {
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ @SequenceOrder {
+ value: 1
+ }
+ string EA1?;
+
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ @SequenceOrder {
+ value: 2
+ }
+ string EA2?;
+
+ @Element {
+ maxOccurs: 4,
+ minOccurs: 2
+ }
+ @SequenceOrder {
+ value: 3
+ }
+ string[] EA3?;
+ } EA;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice", "xsd_element", "xsd_element_and_sequence"]}
+function testXsdChoiceWithElementAnnotation2() returns error? {
+ string xmlStr;
+ XsdChoiceWithElementAnnotation2|Error v;
+
+ xmlStr = string `ABCABC`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlStr = string `ABCABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlStr = string `ABCABCABCD`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_EA2: {EA: {EA1: "ABC", EA2: "ABC", EA3: ["AB", "CD"]}}});
+
+ xmlStr = string `ABCABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA2": "ABC", EA3: ["AB", "AB"]}}});
+
+ xmlStr = string `ABCABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA2": "ABC", EA3: ["AB", "AB"]}}});
+
+ xmlStr = string `ABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA2": {EA: {EA3: ["AB", "AB"]}}});
+
+ xmlStr = string `ABABABABABAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs more than the max allowed times");
+
+ xmlStr = string `ABCABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA1": "ABC", EA3: ["AB", "AB"]}}});
+
+ xmlStr = string `ABCABCCD`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlStr = string `ABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlStr = string `ABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlStr = string `AB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlStr = string `ABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+}
+
+type XsdChoiceWithElementAnnotation3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XsdChoiceWithElementAnnotation3_1 seq_XsdChoiceWithElementAnnotation3_1;
+
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XsdChoiceWithElementAnnotation3_2 seq_XsdChoiceWithElementAnnotation3_2?;
+};
+
+type Choice_XsdChoiceWithElementAnnotation3_1 record {
+ @Element {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Choice_A_3[] field1?;
+
+ @Element {
+ minOccurs: 0,
+ maxOccurs: 3
+ }
+ Choice_B_3[] field2?;
+
+ @Element {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Choice_C_3 field3?;
+};
+
+type Choice_XsdChoiceWithElementAnnotation3_2 record {
+ @Element {
+ minOccurs: 0,
+ maxOccurs: 3
+ }
+ Choice_D_3[] field4?;
+
+ @Element {
+ minOccurs: 0,
+ maxOccurs: 3
+ }
+ Choice_E_3[] field5?;
+
+ @Element {
+ minOccurs: 0,
+ maxOccurs: 3
+ }
+ Choice_F_3[] field6?;
+};
+
+type Choice_A_3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_3 value1;
+};
+
+type Choice_B_3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice2_3 value2;
+};
+
+type Choice_C_3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice3_3 value3;
+};
+
+type Choice_D_3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_3 value1;
+};
+
+type Choice_E_3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice2_3 value2;
+};
+
+type Choice_F_3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice3_3 value3;
+};
+
+type Choice_3 record {
+ @Element {
+ minOccurs: 0,
+ maxOccurs: 3
+ }
+ string[] a?;
+
+ @Element {
+ minOccurs: 0,
+ maxOccurs: 3
+ }
+ string[] b?;
+ string c?;
+};
+
+type Choice2_3 record {
+ string d?;
+ string e?;
+ string f?;
+};
+
+type Choice3_3 record {
+ string g?;
+ string h?;
+ string i?;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice", "xsd_element", "xsd_element_and_sequence"]}
+function testXsdChoiceWithElementAnnotation3() returns error? {
+ string xmlStr;
+ XsdChoiceWithElementAnnotation3|Error v2;
+
+ xmlStr = string `ABCABCABCABCABCABCABCABCABC`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {seq_XsdChoiceWithElementAnnotation3_1: {field1: [{value1: {a: ["ABC"]}}, {value1: {a: ["ABC", "ABC"]}}, {value1: {a: ["ABC", "ABC", "ABC"]}}]}, seq_XsdChoiceWithElementAnnotation3_2: {field5: [{value2: {d: "ABC"}}, {value2: {d: "ABC"}}, {value2: {d: "ABC"}}]}});
+}
diff --git a/ballerina/tests/xsd_choice_test_with_element_annotation_with_parse_type.bal b/ballerina/tests/xsd_choice_test_with_element_annotation_with_parse_type.bal
new file mode 100644
index 00000000..c37f4d21
--- /dev/null
+++ b/ballerina/tests/xsd_choice_test_with_element_annotation_with_parse_type.bal
@@ -0,0 +1,251 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+@Name {
+ value: "Root"
+}
+type XsdChoiceWithElementAnnotationWithXmlValue record {
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_EA1 seq_EA1?;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice", "xsd_element", "xsd_element_and_sequence"]}
+function testXsdChoiceWithElementAnnotationWithXmlValue() returns error? {
+ xml xmlValue;
+ XsdChoiceWithElementAnnotationWithXmlValue|Error v;
+
+ xmlValue = xml `ABC`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC"]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml ``;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {});
+
+ xmlValue = xml `ABABABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_EA1: {EA3: ["AB", "AB", "AB", "AB"]}});
+
+ xmlValue = xml `ABC`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA1":{"EA2": "ABC"}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `ABABABABAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs more than the max allowed times");
+
+ xmlValue = xml `ABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA1":{EA3: ["AB", "AB"]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `ABABABABABAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs more than the max allowed times");
+
+ xmlValue = xml `AB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlValue = xml `ABCABC`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC", "ABC"]}});
+
+ xmlValue = xml `ABC`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC"]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `ABCAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlValue = xml `AB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlValue = xml `ABCABCABC`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA1' occurs more than the max allowed times");
+
+ xmlValue = xml `ABCABCABCABCABC`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'seq_EA1' occurs more than the max allowed times");
+}
+
+@Name {
+ value: "Root"
+}
+type XsdChoiceWithElementAnnotationWithXmlValue2 record {
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_EA2 seq_EA2;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice", "xsd_element", "xsd_element_and_sequence"]}
+function testXsdChoiceWithElementAnnotationWithXmlValue2() returns error? {
+ xml xmlValue;
+ XsdChoiceWithElementAnnotationWithXmlValue2|Error v;
+
+ xmlValue = xml `ABCABC`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlValue = xml `ABCABCAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlValue = xml `ABCABCABCD`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_EA2: {EA: {EA1: "ABC", EA2: "ABC", EA3: ["AB", "CD"]}}});
+ test:assertEquals(toXml(check v), xmlValue);
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `ABCABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA2": "ABC", EA3: ["AB", "AB"]}}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `ABCABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA2": "ABC", EA3: ["AB", "AB"]}}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `ABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA2": {EA: {EA3: ["AB", "AB"]}}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `ABABABABABAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs more than the max allowed times");
+
+ xmlValue = xml `ABCABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA1": "ABC", EA3: ["AB", "AB"]}}});
+
+ xmlValue = xml `ABCABCCD`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlValue = xml `ABCAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlValue = xml `ABCAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlValue = xml `AB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlValue = xml `ABCAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+}
+
+@Name {
+ value: "Root"
+}
+type XsdChoiceWithElementAnnotationWithXmlValue3 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XsdChoiceWithElementAnnotationWithXmlValue3_1 seq_XsdChoiceWithElementAnnotationWithXmlValue3_1;
+
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XsdChoiceWithElementAnnotationWithXmlValue3_2 seq_XsdChoiceWithElementAnnotationWithXmlValue3_2?;
+};
+
+type Choice_XsdChoiceWithElementAnnotationWithXmlValue3_1 record {
+ @Element {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Choice_A_3[] field1?;
+
+ @Element {
+ minOccurs: 0,
+ maxOccurs: 3
+ }
+ Choice_B_3[] field2?;
+
+ @Element {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Choice_C_3 field3?;
+};
+
+type Choice_XsdChoiceWithElementAnnotationWithXmlValue3_2 record {
+ @Element {
+ minOccurs: 0,
+ maxOccurs: 3
+ }
+ Choice_D_3[] field4?;
+
+ @Element {
+ minOccurs: 0,
+ maxOccurs: 3
+ }
+ Choice_E_3[] field5?;
+
+ @Element {
+ minOccurs: 0,
+ maxOccurs: 3
+ }
+ Choice_F_3[] field6?;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice", "xsd_element", "xsd_element_and_sequence"]}
+function testXsdChoiceWithElementAnnotationWithXmlValue3() returns error? {
+ xml xmlValue;
+ XsdChoiceWithElementAnnotationWithXmlValue3|Error v2;
+
+ xmlValue = xml `ABCABCABCABCABCABCABCABCABC`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {seq_XsdChoiceWithElementAnnotationWithXmlValue3_1: {field1: [{value1: {a: ["ABC"]}}, {value1: {a: ["ABC", "ABC"]}}, {value1: {a: ["ABC", "ABC", "ABC"]}}]}, seq_XsdChoiceWithElementAnnotationWithXmlValue3_2: {field5: [{value2: {d: "ABC"}}, {value2: {d: "ABC"}}, {value2: {d: "ABC"}}]}});
+ test:assertEquals(toXml(check v2), xmlValue);
+}
diff --git a/ballerina/tests/xsd_choice_test_with_parse_type.bal b/ballerina/tests/xsd_choice_test_with_parse_type.bal
new file mode 100644
index 00000000..25677648
--- /dev/null
+++ b/ballerina/tests/xsd_choice_test_with_parse_type.bal
@@ -0,0 +1,636 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueRecord record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord choice_XSDChoiceWithXmlValueRecord?;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecord record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXsdChoiceWithXmlValue() returns error? {
+ xml xmlValue;
+ XSDChoiceWithXmlValueRecord|Error v;
+
+ xmlValue = xml `10`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XSDChoiceWithXmlValueRecord: {age: 10}});
+ test:assertEquals(toXml(check v), xmlValue);
+ Error? e = validate(xmlValue, XSDChoiceWithXmlValueRecord);
+ test:assertEquals(e, ());
+
+ xmlValue = xml `10.5`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XSDChoiceWithXmlValueRecord: {salary: 10.5}});
+ test:assertEquals(toXml(check v), xmlValue);
+ e = validate(xmlValue, XSDChoiceWithXmlValueRecord);
+ test:assertEquals(e, ());
+
+ xmlValue = xml `1011.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceWithXmlValueRecord' occurs more than the max allowed times");
+ e = validate(xmlValue, XSDChoiceWithXmlValueRecord);
+ test:assertEquals((e).message(), "Invalid XML found: ''choice_XSDChoiceWithXmlValueRecord' occurs more than the max allowed times'");
+ xml|Error toXmlResult = toXml({choice_XSDChoiceWithXmlValueRecord: {age: 10, salary: 11.1}});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'choice_XSDChoiceWithXmlValueRecord' occurs more than the max allowed times");
+
+ xmlValue = xml `11.111.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceWithXmlValueRecord' occurs more than the max allowed times");
+ e = validate(xmlValue, XSDChoiceWithXmlValueRecord);
+ test:assertEquals((e).message(), "Invalid XML found: ''choice_XSDChoiceWithXmlValueRecord' occurs more than the max allowed times'");
+
+ xmlValue = xml ``;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceWithXmlValueRecord' occurs less than the min required times");
+ e = validate(xmlValue, XSDChoiceWithXmlValueRecord);
+ test:assertEquals((e).message(), "Invalid XML found: ''choice_XSDChoiceWithXmlValueRecord' occurs less than the min required times'");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueRecordP2 record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecordP2 choice_XSDChoiceWithXmlValueRecordP2;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecordP2 record {|
+ @Element {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ int[] age?;
+ float salary?;
+ @Element {
+ minOccurs: 1,
+ maxOccurs: 2
+ }
+ string[] name?;
+|};
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueP1Record record {|
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueP1Record choice_XSDChoiceWithXmlValueP1Record?;
+|};
+
+type Choice_XSDChoiceWithXmlValueP1Record record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_choice"]}
+function testXsdChoiceWithXmlValueP1() returns error? {
+ xml xmlValue;
+ XSDChoiceWithXmlValueP1Record|Error v;
+
+ xmlValue = xml `10`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XSDChoiceWithXmlValueP1Record: {age: 10}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `10.5`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XSDChoiceWithXmlValueP1Record: {salary: 10.5}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `1011.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceWithXmlValueP1Record' occurs more than the max allowed times");
+
+ xmlValue = xml `11.111.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceWithXmlValueP1Record' occurs more than the max allowed times");
+
+ xmlValue = xml ``;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {});
+}
+
+@test:Config {groups: ["xsd", "xsd_Choice"]}
+function testXsdChoiceWithXmlValueP2() returns error? {
+ xml xmlValue = xml `10`;
+ XSDChoiceWithXmlValueRecordP2|Error v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XSDChoiceWithXmlValueRecordP2: {age: [10]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `ABC`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XSDChoiceWithXmlValueRecordP2: {name: ["ABC"]}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `11.1`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XSDChoiceWithXmlValueRecordP2: {salary: 11.1}});
+ test:assertEquals(toXml(check v), xmlValue);
+
+ xmlValue = xml `10101011.1ABC`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceWithXmlValueRecordP2' occurs more than the max allowed times");
+
+ xmlValue = xml `10ABC11.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceWithXmlValueRecordP2' occurs more than the max allowed times");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueRecord2 record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord2 choice_XSDChoiceWithXmlValueRecord2;
+
+ int num;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecord2 record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoiceWithXmlValue2() returns error? {
+ xml xmlValue = xml `310`;
+ XSDChoiceWithXmlValueRecord2|Error v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XSDChoiceWithXmlValueRecord2: {age: 10}, num: 3});
+ test:assertEquals((check v).choice_XSDChoiceWithXmlValueRecord2.age, 10);
+ test:assertEquals((check v).num, 3);
+ test:assertEquals(toXml(check v), xml `103`);
+
+ xmlValue = xml `11.13`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {choice_XSDChoiceWithXmlValueRecord2: {salary: 11.1}, num: 3});
+ test:assertEquals((check v).choice_XSDChoiceWithXmlValueRecord2.salary, 11.1);
+ test:assertEquals((check v).num, 3);
+ test:assertEquals(toXml(check v), xml `11.13`);
+
+ xmlValue = xml `11.1103`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceWithXmlValueRecord2' occurs more than the max allowed times");
+ xml|Error toXmlResult = toXml({choice_XSDChoiceWithXmlValueRecord2: {age: 10, salary: 11.1}, num: 3});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'choice_XSDChoiceWithXmlValueRecord2' occurs more than the max allowed times");
+
+ xmlValue = xml `10311.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'choice_XSDChoiceWithXmlValueRecord2' occurs more than the max allowed times");
+ toXmlResult = toXml({num: 3, choice_XSDChoiceWithXmlValueRecord2: {age: 10, salary: 11.1}});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'choice_XSDChoiceWithXmlValueRecord2' occurs more than the max allowed times");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueRecord3 record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord3 choice_XSDChoiceWithXmlValueRecord3;
+
+ record{int n;} num;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecord3 record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoiceWithXmlValue3() returns error? {
+ xml xmlValue = xml `310`;
+ XSDChoiceWithXmlValueRecord3|Error v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord3: {age: 10}, num: {n: 3}});
+ test:assertEquals((check v2).choice_XSDChoiceWithXmlValueRecord3.age, 10);
+ test:assertEquals((check v2).num, {n: 3});
+ test:assertEquals(toXml(check v2), xml `103`);
+
+ xmlValue = xml `11.13`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord3: {salary: 11.1}, num: {n: 3}});
+ test:assertEquals(toXml(check v2), xml `11.13`);
+
+ xmlValue = xml `10311.1`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord3' occurs more than the max allowed times");
+
+ xmlValue = xml `31011.1`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord3' occurs more than the max allowed times");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueRecord4 record {|
+ record{record {int n;} n;} num;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord4 choice_XSDChoiceWithXmlValueRecord4;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecord4 record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoiceWithXmlValue4() returns error? {
+ xml xmlValue = xml `310`;
+ XSDChoiceWithXmlValueRecord4|Error v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord4: {age: 10}, num: {n: {n: 3}}});
+ test:assertEquals((check v2).choice_XSDChoiceWithXmlValueRecord4.age, 10);
+ test:assertEquals((check v2).num, {n: {n: 3}});
+ test:assertEquals(toXml(check v2), xmlValue);
+
+ xmlValue = xml `11.13`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord4: {salary: 11.1}, num: {n: {n: 3}}});
+ test:assertEquals(toXml(check v2), xml `311.1`);
+
+ xmlValue = xml `10311.1`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord4' occurs more than the max allowed times");
+
+ xmlValue = xml `31011.1`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord4' occurs more than the max allowed times");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueRecord5 record {|
+ record{record {int n;} n;} num;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord5 choice_XSDChoiceWithXmlValueRecord5;
+ record{record {int n;} n;} num2;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecord5 record {|
+ int age?;
+ float salary?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoiceWithXmlValue5() returns error? {
+ xml xmlValue = xml `3310`;
+ XSDChoiceWithXmlValueRecord5|Error v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord5: {age: 10}, num: {n: {n: 3}}, num2: {n: {n: 3}}});
+ test:assertEquals(toXml(check v2), xml `3103`);
+
+ xmlValue = xml `311.13`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord5: {salary: 11.1}, num: {n: {n: 3}}, num2: {n: {n: 3}}});
+ test:assertEquals(toXml(check v2), xml `311.13`);
+
+ xmlValue = xml `11.133`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord5: {salary: 11.1}, num: {n: {n: 3}}, num2: {n: {n: 3}}});
+ test:assertEquals(toXml(check v2), xml `311.13`);
+
+ xmlValue = xml `103311.1`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord5' occurs more than the max allowed times");
+
+ xmlValue = xml `33`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "required field 'choice_XSDChoiceWithXmlValueRecord5' not present in XML");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueRecord6 record {|
+ record{record {int n;} n;} num;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord6_1 choice_XSDChoiceWithXmlValueRecord6_1;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord6_2 choice_XSDChoiceWithXmlValueRecord6_2;
+ record{record {int n;} n;} num2;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecord6_1 record {|
+ int age?;
+ float salary?;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecord6_2 record {|
+ string name?;
+ string status?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoiceWithXmlValue6() returns error? {
+ xml xmlValue = xml `3success310`;
+ XSDChoiceWithXmlValueRecord6|Error v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord6_1: {age: 10}, num: {n: {n: 3}}, num2: {n: {n: 3}}, choice_XSDChoiceWithXmlValueRecord6_2: {status: "success"}});
+ test:assertEquals((check v2).choice_XSDChoiceWithXmlValueRecord6_1.age, 10);
+ test:assertEquals((check v2).choice_XSDChoiceWithXmlValueRecord6_2.status, "success");
+ test:assertEquals((check v2).num, {n: {n: 3}});
+ test:assertEquals((check v2).num2, {n: {n: 3}});
+ test:assertEquals(toXml(check v2), xml `310success3`);
+
+ xmlValue = xml `SD33`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "required field 'choice_XSDChoiceWithXmlValueRecord6_1' not present in XML");
+
+ xmlValue = xml `33SD`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "required field 'choice_XSDChoiceWithXmlValueRecord6_1' not present in XML");
+
+ xmlValue = xml `SDsuccess33`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord6_2' occurs more than the max allowed times");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueRecord7 record {|
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord7_1 choice_XSDChoiceWithXmlValueRecord7_1;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord7_2 choice_XSDChoiceWithXmlValueRecord7_2;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecord7_1 record {|
+ int age?;
+ float salary?;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecord7_2 record {|
+ string name?;
+ string status?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoiceWithXmlValue7() returns error? {
+ xml xmlValue = xml `success11.1`;
+ XSDChoiceWithXmlValueRecord7|Error v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord7_1: {salary: 11.1}, choice_XSDChoiceWithXmlValueRecord7_2: {status: "success"}});
+ test:assertEquals(toXml(check v2), xml `11.1success`);
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueRecord8 record {|
+ XSDChoiceWithXmlValueRecord8P test;
+ int a;
+|};
+
+type XSDChoiceWithXmlValueRecord8P record {|
+ record{record {int n;} n;} num;
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord8_1 choice_XSDChoiceWithXmlValueRecord8_1;
+
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord8_2 choice_XSDChoiceWithXmlValueRecord8_2;
+ record{record {int n;} n;} num2;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecord8_1 record {|
+ int age?;
+ float salary?;
+|};
+
+type Choice_XSDChoiceWithXmlValueRecord8_2 record {|
+ RecChoice8 name?;
+ RecChoice8 status?;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoiceWithXmlValue8() returns error? {
+ xml xmlValue;
+ XSDChoiceWithXmlValueRecord8|Error v2;
+
+ xmlValue = xml `3SDAB3102`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {a: 2, test: {choice_XSDChoiceWithXmlValueRecord8_1: {age: 10}, num: {n: {n: 3}}, num2: {n: {n: 3}}, choice_XSDChoiceWithXmlValueRecord8_2: {name: {value1: "SD", value2: "AB"}}}});
+ test:assertEquals((check v2).test.choice_XSDChoiceWithXmlValueRecord8_1.age, 10);
+ test:assertEquals((check v2).test.choice_XSDChoiceWithXmlValueRecord8_2.name, {value1: "SD", value2: "AB"});
+ test:assertEquals((check v2).test.num, {n: {n: 3}});
+ test:assertEquals((check v2).test.num2, {n: {n: 3}});
+ test:assertEquals(toXml(check v2), xml `310SDAB32`);
+
+ xmlValue = xml `SuccessFail3311.12`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {a: 2, test: {choice_XSDChoiceWithXmlValueRecord8_1: {salary: 11.1}, num: {n: {n: 3}}, num2: {n: {n: 3}}, choice_XSDChoiceWithXmlValueRecord8_2: {status: {value1: "Success", value2: "Fail"}}}});
+ test:assertEquals((check v2).test.choice_XSDChoiceWithXmlValueRecord8_1.salary, 11.1);
+ test:assertEquals((check v2).test.num, {n: {n: 3}});
+ test:assertEquals((check v2).test.num2, {n: {n: 3}});
+
+ xmlValue = xml `33SuccessFail11.12`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {a: 2, test: {choice_XSDChoiceWithXmlValueRecord8_1: {salary: 11.1}, num: {n: {n: 3}}, num2: {n: {n: 3}}, choice_XSDChoiceWithXmlValueRecord8_2: {status: {value1: "Success", value2: "Fail"}}}});
+
+ xmlValue = xml `SDAB10SuccessFail11.12`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord8_2' occurs more than the max allowed times");
+
+ xmlValue = xml `10SuccessFail11.12`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord8_1' occurs more than the max allowed times");
+
+ xmlValue = xml `102`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord8_2' occurs less than the min required times");
+
+ xmlValue = xml `SuccessFail2`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord8_1' occurs less than the min required times");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueRecord9 record {
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord9_1 choice_XSDChoiceWithXmlValueRecord9_1;
+};
+
+type Choice_XSDChoiceWithXmlValueRecord9_1 record {
+ Choice_A field1?;
+ Choice_B field2?;
+ Choice_C field3?;
+};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoiceWithXmlValue9() returns error? {
+ xml xmlValue;
+ XSDChoiceWithXmlValueRecord9|Error v2;
+
+ xmlValue = xml `1`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord9_1: {field2: {value2: {a: "1"}}}});
+ test:assertEquals(toXml(check v2), xmlValue);
+
+ xmlValue = xml `1`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord9_1: {field3: {value3: {c: "1"}}}});
+ test:assertEquals(toXml(check v2), xmlValue);
+
+ xmlValue = xml `11`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord9_1' occurs more than the max allowed times");
+
+ xmlValue = xml `11`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'value1' occurs more than the max allowed times");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDChoiceWithXmlValueRecord10 record {
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord10_1 choice_XSDChoiceWithXmlValueRecord10_1?;
+
+ @Choice {
+ minOccurs: 1,
+ maxOccurs: 1
+ }
+ Choice_XSDChoiceWithXmlValueRecord10_2 choice_XSDChoiceWithXmlValueRecord10_2;
+};
+
+type Choice_XSDChoiceWithXmlValueRecord10_1 record {
+ Choice_A_10 field1?;
+ Choice_B_10 field2?;
+ Choice_C_10 field3?;
+};
+
+type Choice_XSDChoiceWithXmlValueRecord10_2 record {
+ Choice_D_10 field4?;
+ Choice_E_10 field5?;
+ Choice_F_10 field6?;
+};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdChoiceWithXmlValue10() returns error? {
+ xml xmlValue = xml `12`;
+ XSDChoiceWithXmlValueRecord10|Error v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {choice_XSDChoiceWithXmlValueRecord10_1: {field1: {value1: {a: "1"}}}, choice_XSDChoiceWithXmlValueRecord10_2: {field5: {value2: {"d": "2"}}}});
+ test:assertEquals(toXml(check v2), xmlValue);
+ Error? e = validate(xmlValue, XSDChoiceWithXmlValueRecord10);
+ test:assertEquals(e, ());
+
+ xmlValue = xml `112`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord10_1' occurs more than the max allowed times");
+ e = validate(xmlValue, XSDChoiceWithXmlValueRecord10);
+ test:assertEquals((e).message(), "Invalid XML found: ''choice_XSDChoiceWithXmlValueRecord10_1' occurs more than the max allowed times'");
+ xml|Error toXmlResult = toXml({choice_XSDChoiceWithXmlValueRecord10_1: {field1: {value1: {a: "1"}}, field2: {value2: {d: "1"}}}, choice_XSDChoiceWithXmlValueRecord10_2: {field5: {value2: {d: "2"}}}});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'choice_XSDChoiceWithXmlValueRecord10_1' occurs more than the max allowed times");
+
+ xmlValue = xml `122`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'choice_XSDChoiceWithXmlValueRecord10_2' occurs more than the max allowed times");
+ e = validate(xmlValue, XSDChoiceWithXmlValueRecord10);
+ test:assertEquals((e).message(), "Invalid XML found: ''choice_XSDChoiceWithXmlValueRecord10_2' occurs more than the max allowed times'");
+
+ xmlValue = xml `112`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'value2' occurs more than the max allowed times");
+ e = validate(xmlValue, XSDChoiceWithXmlValueRecord10);
+ test:assertEquals((e).message(), "Invalid XML found: ''value2' occurs more than the max allowed times'");
+ toXmlResult = toXml({choice_XSDChoiceWithXmlValueRecord10_1: {field2: {value2: {d: "1", e: "1"}}}, choice_XSDChoiceWithXmlValueRecord10_2: {field5: {value2: {d: "2"}}}});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'value2' occurs more than the max allowed times");
+}
diff --git a/ballerina/tests/xsd_choice_with_name_annotations.bal b/ballerina/tests/xsd_choice_with_name_annotations.bal
new file mode 100644
index 00000000..5b796a14
--- /dev/null
+++ b/ballerina/tests/xsd_choice_with_name_annotations.bal
@@ -0,0 +1,232 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+type XsdChoiceWithNameAnnotation record {
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_Name_EA1 seq_EA1?;
+};
+
+type Choice_Name_EA1 record {
+
+ @Element {
+ maxOccurs: 2,
+ minOccurs: 0
+ }
+ @Name {
+ value: "A1"
+ }
+ string[] EA1?;
+
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ @Name {
+ value: "A2"
+ }
+ string EA2?;
+
+ @Element {
+ maxOccurs: 4,
+ minOccurs: 2
+ }
+ @Name {
+ value: "A3"
+ }
+ string[] EA3?;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice", "xsd_element", "xsd_element_and_sequence"]}
+function testXsdChoiceWithNameAnnotation() returns error? {
+ string xmlStr;
+ XsdChoiceWithNameAnnotation|Error v;
+
+ xmlStr = string `ABC`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC"]}});
+
+ xmlStr = string ``;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {});
+
+ xmlStr = string `ABABABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_EA1: {EA3: ["AB", "AB", "AB", "AB"]}});
+
+ xmlStr = string `ABC`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA1":{"EA2": "ABC"}});
+
+ xmlStr = string `ABABABABAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs more than the max allowed times");
+
+ xmlStr = string `ABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA1":{EA3: ["AB", "AB"]}});
+
+ xmlStr = string `ABABABABABAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs more than the max allowed times");
+
+ xmlStr = string `AB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlStr = string `ABCABC`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC", "ABC"]}});
+
+ xmlStr = string `ABC`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC"]}});
+
+ xmlStr = string `ABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlStr = string `AB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlStr = string `ABCABCABC`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A1' occurs more than the max allowed times");
+
+ xmlStr = string `ABCABCABCABCABC`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'seq_EA1' occurs more than the max allowed times");
+}
+
+type XsdChoiceWithNameAnnotation2 record {
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_Name_EA2 seq_EA2;
+};
+
+type Choice_Name_EA2 record {
+ @Name {
+ value: "A"
+ }
+ record {
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ @Name {
+ value: "A1"
+ }
+ string EA1?;
+
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ @Name {
+ value: "A2"
+ }
+ string EA2?;
+
+ @Element {
+ maxOccurs: 4,
+ minOccurs: 2
+ }
+ @Name {
+ value: "A3"
+ }
+ string[] EA3?;
+ } EA;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice", "xsd_element", "xsd_element_and_sequence"]}
+function testXsdChoiceWithNameAnnotation2() returns error? {
+ string xmlStr;
+ XsdChoiceWithNameAnnotation2|Error v;
+
+ xmlStr = string `ABCABC`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlStr = string `ABCABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlStr = string `ABCABCABCD`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_EA2: {EA: {EA1: "ABC", EA2: "ABC", EA3: ["AB", "CD"]}}});
+
+ xmlStr = string `ABCABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA2": "ABC", EA3: ["AB", "AB"]}}});
+
+ xmlStr = string `ABCABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA2": "ABC", EA3: ["AB", "AB"]}}});
+
+ xmlStr = string `ABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA2": {EA: {EA3: ["AB", "AB"]}}});
+
+ xmlStr = string `ABABABABABAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs more than the max allowed times");
+
+ xmlStr = string `ABCABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA1": "ABC", EA3: ["AB", "AB"]}}});
+
+ xmlStr = string `ABCABCCD`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlStr = string `ABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlStr = string `ABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlStr = string `AB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlStr = string `ABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+}
diff --git a/ballerina/tests/xsd_choice_with_name_annotations_with_parse_type.bal b/ballerina/tests/xsd_choice_with_name_annotations_with_parse_type.bal
new file mode 100644
index 00000000..3cdfb6e9
--- /dev/null
+++ b/ballerina/tests/xsd_choice_with_name_annotations_with_parse_type.bal
@@ -0,0 +1,168 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+type XsdChoiceWithNameAnnotationWithXmlValue record {
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_Name_EA1 seq_EA1?;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice", "xsd_element", "xsd_element_and_sequence"]}
+function testXsdChoiceWithNameAnnotationWithXmlValue() returns error? {
+ xml xmlValue;
+ XsdChoiceWithNameAnnotationWithXmlValue|Error v;
+
+ xmlValue = xml `ABC`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC"]}});
+
+ xmlValue = xml ``;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {});
+
+ xmlValue = xml `ABABABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_EA1: {EA3: ["AB", "AB", "AB", "AB"]}});
+
+ xmlValue = xml `ABC`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA1":{"EA2": "ABC"}});
+
+ xmlValue = xml `ABABABABAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs more than the max allowed times");
+
+ xmlValue = xml `ABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA1":{EA3: ["AB", "AB"]}});
+
+ xmlValue = xml `ABABABABABAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs more than the max allowed times");
+
+ xmlValue = xml `AB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlValue = xml `ABCABC`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC", "ABC"]}});
+
+ xmlValue = xml `ABC`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_EA1: {EA1: ["ABC"]}});
+
+ xmlValue = xml `ABCAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlValue = xml `AB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlValue = xml `ABCABCABC`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A1' occurs more than the max allowed times");
+
+ xmlValue = xml `ABCABCABCABCABC`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'seq_EA1' occurs more than the max allowed times");
+}
+
+type XsdChoiceWithNameAnnotationWithXmlValue2 record {
+ @Choice {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Choice_Name_EA2 seq_EA2;
+};
+
+@test:Config {groups: ["xsd", "xsd_choice", "xsd_element", "xsd_element_and_sequence"]}
+function testXsdChoiceWithNameAnnotationWithXmlValue2() returns error? {
+ xml xmlValue;
+ XsdChoiceWithNameAnnotationWithXmlValue2|Error v;
+
+ xmlValue = xml `ABCABC`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlValue = xml `ABCABCAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlValue = xml `ABCABCABCD`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_EA2: {EA: {EA1: "ABC", EA2: "ABC", EA3: ["AB", "CD"]}}});
+
+ xmlValue = xml `ABCABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA2": "ABC", EA3: ["AB", "AB"]}}});
+
+ xmlValue = xml `ABCABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA2": "ABC", EA3: ["AB", "AB"]}}});
+
+ xmlValue = xml `ABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA2": {EA: {EA3: ["AB", "AB"]}}});
+
+ xmlValue = xml `ABABABABABAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs more than the max allowed times");
+
+ xmlValue = xml `ABCABAB`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {"seq_EA2": {EA: {"EA1": "ABC", EA3: ["AB", "AB"]}}});
+
+ xmlValue = xml `ABCABCCD`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlValue = xml `ABCAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlValue = xml `ABCAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlValue = xml `AB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+
+ xmlValue = xml `ABCAB`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'A3' occurs less than the min required times");
+}
diff --git a/ballerina/tests/xsd_element_test.bal b/ballerina/tests/xsd_element_test.bal
new file mode 100644
index 00000000..08f06976
--- /dev/null
+++ b/ballerina/tests/xsd_element_test.bal
@@ -0,0 +1,222 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+type ElementRecord record {
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ string name?;
+
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ int age?;
+};
+
+@test:Config{groups: ["xsd", "xsd_element"]}
+function testXsdElement() returns error? {
+ string xmlStr = "John25";
+ ElementRecord|Error rec = parseString(xmlStr);
+ test:assertEquals(rec, {name: "John", age: 25});
+
+ xmlStr = "John";
+ rec = parseString(xmlStr);
+ test:assertEquals(rec, {name: "John"});
+}
+
+type ElementRecord2 record {
+ @Element {
+ maxOccurs: 10,
+ minOccurs: 0
+ }
+ string[] name?;
+
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 1
+ }
+ int[] age?;
+};
+
+@test:Config{groups: ["xsd", "xsd_element"]}
+function testXsdElement2() returns error? {
+ string xmlStr = "John25";
+ ElementRecord2|Error rec = parseString(xmlStr);
+ test:assertEquals(rec, {name: ["John"], age: [25]});
+
+ xmlStr = "John";
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs less than the min required times");
+
+ xmlStr = "25";
+ rec = parseString(xmlStr);
+ test:assertEquals(rec, {age: [25]});
+
+ xmlStr = "1112131415";
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+
+ xmlStr = "11Abc12131415";
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+
+ xmlStr = "AbcAbcAbcAbcAbc1112131415";
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+
+ xmlStr = "11AbcAbc12Abc13Abc1415";
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+}
+
+type ElementRecord3 record {
+ record {
+ @Element {
+ maxOccurs: 10,
+ minOccurs: 0
+ }
+ string[] name?;
+
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 1
+ }
+ int[] age?;
+
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 2
+ }
+ int[] id?;
+ } user;
+
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 1
+ }
+ int status;
+};
+
+@test:Config{groups: ["xsd", "xsd_element"]}
+function testXsdElement3() returns error? {
+ string xmlStr;
+ ElementRecord3|Error rec;
+
+ xmlStr = "John12353";
+ rec = parseString(xmlStr);
+ test:assertEquals(rec, {user: {name: ["John"], age: [35], id: [1, 2]}, status: 3});
+
+ xmlStr = "John123";
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs less than the min required times");
+
+ xmlStr = "12353";
+ rec = parseString(xmlStr);
+ test:assertEquals(rec, {user: {age: [35], id: [1, 2]}, status: 3});
+
+ xmlStr = "1211131314153";
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+
+ xmlStr = "1211Abc131314153";
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+
+ xmlStr = "12AbcAbcAbcAbcAbc11131314153";
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+
+ xmlStr = "1211AbcAbc13Abc13Abc14153";
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+}
+
+type ElementRecord4 record {
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 2
+ }
+ record {
+ @Element {
+ maxOccurs: 4,
+ minOccurs: 3
+ }
+ string[] firstName;
+
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 3
+ }
+ string[] lastName;
+ }[] name;
+
+ @Element {
+ maxOccurs: 4,
+ minOccurs: 3
+ }
+ int[] status;
+
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 3
+ }
+ int[] age;
+};
+
+@test:Config{groups: ["xsd", "xsd_element"]}
+function testXsdElement4() returns error? {
+ string xmlStr = string `JohnJaneJimDoeSmithBrownJohnJaneJimDoeSmithBrown123202530`;
+ ElementRecord4|Error rec = parseString(xmlStr);
+ test:assertEquals(rec, {name: [{firstName: ["John", "Jane", "Jim"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim"], lastName: ["Doe", "Smith", "Brown"]}], status: [1, 2, 3], age: [20, 25, 30]});
+
+ xmlStr = string `JohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrown1234202530`;
+ rec = parseString(xmlStr);
+ test:assertEquals(rec, {name: [{firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}], status: [1, 2, 3, 4], age: [20, 25, 30]});
+
+ xmlStr = string `JohnJaneJimAnnaDoeSmithBrown1234202530`;
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'name' occurs less than the min required times");
+
+ xmlStr = string `JohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrown1234202530`;
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'name' occurs more than the max allowed times");
+
+ xmlStr = string `JohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrownBrownBrownBrownBrownBrownBrown1234202530`;
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'lastName' occurs more than the max allowed times");
+
+ xmlStr = string `JohnJaneJimDoeSmithBrownJohnJaneJimJimJimJimJimJimJimJimDoeSmithBrown123202530`;
+ rec = parseString(xmlStr);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'firstName' occurs more than the max allowed times");
+}
diff --git a/ballerina/tests/xsd_element_test_with_parse_type.bal b/ballerina/tests/xsd_element_test_with_parse_type.bal
new file mode 100644
index 00000000..af92fec2
--- /dev/null
+++ b/ballerina/tests/xsd_element_test_with_parse_type.bal
@@ -0,0 +1,288 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+@Name {
+ value: "Root"
+}
+type ElementRecordWithXmlValue record {
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ string name?;
+
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ int age?;
+};
+
+@test:Config{groups: ["xsd", "xsd_element"]}
+function testXsdElementWithXmlValue() returns error? {
+ xml xmlValue = xml `John25`;
+ ElementRecordWithXmlValue|Error rec = parseAsType(xmlValue);
+ test:assertEquals(rec, {name: "John", age: 25});
+ test:assertEquals(toXml(check rec), xmlValue);
+
+ xmlValue = xml `John`;
+ rec = parseAsType(xmlValue);
+ test:assertEquals(rec, {name: "John"});
+ test:assertEquals(toXml(check rec), xmlValue);
+}
+
+@Name {
+ value: "Root"
+}
+type ElementRecordWithXmlValue2 record {
+ @Element {
+ maxOccurs: 10,
+ minOccurs: 0
+ }
+ string[] name?;
+
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 1
+ }
+ int[] age;
+};
+
+@test:Config{groups: ["xsd", "xsd_element"]}
+function testXsdElementWithXmlValue2() returns error? {
+ xml xmlValue;
+ ElementRecordWithXmlValue2|Error rec;
+ xml|Error toXmlResult;
+
+ xmlValue = xml `John25`;
+ rec = parseAsType(xmlValue);
+ test:assertEquals(rec, {name: ["John"], age: [25]});
+ test:assertEquals(toXml(check rec), xmlValue);
+
+ xmlValue = xml `John`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs less than the min required times");
+ toXmlResult = toXml({name: ["John"], age: []});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'age' occurs less than the min required times");
+
+ xmlValue = xml `25`;
+ rec = parseAsType(xmlValue);
+ test:assertEquals(rec, {age: [25]});
+ test:assertEquals(toXml(check rec), xmlValue);
+
+ xmlValue = xml `1112131415`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is Error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+ toXmlResult = toXml({age: [11, 12, 13, 14, 15]});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'age' occurs more than the max allowed times");
+
+ xmlValue = xml `11Abc12131415`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+ toXmlResult = toXml({name: ["Abc"], age: [11, 12, 13, 14, 15]});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'age' occurs more than the max allowed times");
+
+ xmlValue = xml `AbcAbcAbcAbcAbc1112131415`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+ toXmlResult = toXml({name: ["Abc", "Abc", "Abc", "Abc", "Abc"], age: [11, 12, 13, 14, 15]});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'age' occurs more than the max allowed times");
+
+ xmlValue = xml `11AbcAbc12Abc13Abc1415`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+ toXmlResult = toXml({name: ["Abc", "Abc", "Abc", "Abc"], age: [11, 12, 13, 14, 15]});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'age' occurs more than the max allowed times");
+}
+
+@Name {
+ value: "Root"
+}
+type ElementRecordWithXmlValue3 record {
+ record {
+ @Element {
+ maxOccurs: 10,
+ minOccurs: 0
+ }
+ string[] name?;
+
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 1
+ }
+ int[] age;
+
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 2
+ }
+ int[] id;
+ } user;
+
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 1
+ }
+ int status;
+};
+
+@test:Config{groups: ["xsd", "xsd_element"]}
+function testXsdElementWithXmlValue3() returns error? {
+ xml xmlValue;
+ ElementRecordWithXmlValue3|Error rec;
+ xml|Error toXmlResult;
+
+ xmlValue = xml `John12353`;
+ rec = parseAsType(xmlValue);
+ test:assertEquals(rec, {user: {name: ["John"], id: [1, 2], age: [35]}, status: 3});
+ test:assertEquals(toXml(check rec), xml `John35123`);
+
+ xmlValue = xml `John123`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs less than the min required times");
+ toXmlResult = toXml({user: {name: ["John"], id: [1, 2], age: []}, status: 3});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'age' occurs less than the min required times");
+
+ xmlValue = xml `12353`;
+ rec = parseAsType(xmlValue);
+ test:assertEquals(rec, {user: {id: [1, 2], age: [35]}, status: 3});
+ test:assertEquals(toXml(check rec), xml `35123`);
+
+ xmlValue = xml `1211131314153`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+ toXmlResult = toXml({user: {id: [1, 2], age: [11, 13, 13, 14, 15]}, status: 3});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'age' occurs more than the max allowed times");
+
+ xmlValue = xml `1211Abc131314153`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+ toXmlResult = toXml({user: {id: [1,2], age: [11, 13, 13, 14, 15], name: ["Abc"]}, status: 3});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'age' occurs more than the max allowed times");
+
+ xmlValue = xml `12AbcAbcAbcAbcAbc11131314153`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+ toXmlResult = toXml({user: {id: [1,2], age: [11, 13, 13, 14, 15], name: ["Abc", "Abc", "Abc", "Abc"]}, status: 3});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'age' occurs more than the max allowed times");
+
+ xmlValue = xml `1211AbcAbc13Abc13Abc14153`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'age' occurs more than the max allowed times");
+ toXmlResult = toXml({user: {id: [1,2], age: [11, 13, 13, 14, 15], name: ["Abc", "Abc", "Abc", "Abc"]}, status: 3});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'age' occurs more than the max allowed times");
+}
+
+type ElementRecordWithXmlValue4 record {
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 2
+ }
+ record {
+ @Element {
+ maxOccurs: 4,
+ minOccurs: 3
+ }
+ string[] firstName;
+
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 3
+ }
+ string[] lastName;
+ }[] name;
+
+ @Element {
+ maxOccurs: 4,
+ minOccurs: 3
+ }
+ int[] status;
+
+ @Element {
+ maxOccurs: 3,
+ minOccurs: 3
+ }
+ int[] age;
+};
+
+@test:Config{groups: ["xsd", "xsd_element"]}
+function testXsdElementWithXmlValue4() returns error? {
+ xml|Error toXmlResult;
+
+ xml xmlValue = xml `JohnJaneJimDoeSmithBrownJohnJaneJimDoeSmithBrown123202530`;
+ ElementRecordWithXmlValue4|Error rec = parseAsType(xmlValue);
+ test:assertEquals(rec, {name: [{firstName: ["John", "Jane", "Jim"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim"], lastName: ["Doe", "Smith", "Brown"]}], status: [1, 2, 3], age: [20, 25, 30]});
+ test:assertEquals(toXml(check rec), xmlValue);
+
+ xmlValue = xml `JohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrown1234202530`;
+ rec = parseAsType(xmlValue);
+ test:assertEquals(rec, {name: [{firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}], status: [1, 2, 3, 4], age: [20, 25, 30]});
+ test:assertEquals(toXml(check rec), xmlValue);
+
+ xmlValue = xml `JohnJaneJimAnnaDoeSmithBrown1234202530`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'name' occurs less than the min required times");
+ toXmlResult = toXml({name: [{firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}], status: [1, 2, 3, 4], age: [20, 25, 30]});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'name' occurs less than the min required times");
+
+ xmlValue = xml `JohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrown1234202530`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'name' occurs more than the max allowed times");
+ toXmlResult = toXml({name: [{firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}], status: [1, 2, 3, 4], age: [20, 25, 30]});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'name' occurs more than the max allowed times");
+
+ xmlValue = xml `JohnJaneJimAnnaDoeSmithBrownJohnJaneJimAnnaDoeSmithBrownBrownBrownBrownBrownBrownBrown1234202530`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'lastName' occurs more than the max allowed times");
+ toXmlResult = toXml({name: [{firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown", "Brown", "Brown", "Brown", "Brown", "Brown", "Brown"]}], status: [1, 2, 3, 4], age: [20, 25, 30]});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'lastName' occurs more than the max allowed times");
+
+ xmlValue = xml `JohnJaneJimDoeSmithBrownJohnJaneJimJimJimJimJimJimJimJimDoeSmithBrown123202530`;
+ rec = parseAsType(xmlValue);
+ test:assertTrue(rec is error);
+ test:assertEquals((rec).message(), "'firstName' occurs more than the max allowed times");
+ toXmlResult = toXml({name: [{firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim", "Anna"], lastName: ["Doe", "Smith", "Brown"]}, {firstName: ["John", "Jane", "Jim", "Anna", "John"], lastName: ["Doe", "Smith", "Brown"]}], status: [1, 2, 3, 4], age: [20, 25, 30]});
+ test:assertTrue(toXmlResult is Error);
+ test:assertEquals((toXmlResult).message(), "'firstName' occurs more than the max allowed times");
+}
diff --git a/ballerina/tests/xsd_sequence_array_test.bal b/ballerina/tests/xsd_sequence_array_test.bal
new file mode 100644
index 00000000..eb5d2c9c
--- /dev/null
+++ b/ballerina/tests/xsd_sequence_array_test.bal
@@ -0,0 +1,400 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+type XsdSequenceArray record {|
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 2
+ }
+ Seq_XsdSequenceArray[] seq_XsdSequenceArray;
+|};
+
+type Seq_XsdSequenceArray record {|
+ @SequenceOrder {
+ value: 1
+ }
+ int age;
+
+ @SequenceOrder {
+ value: 2
+ }
+ float salary;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdSequenceArray() returns error? {
+ string xmlStr;
+ XsdSequenceArray|Error v;
+
+ xmlStr = string `1311.11415.1`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_XsdSequenceArray: [{age: 13, salary: 11.1}, {age: 14, salary: 15.1}]});
+
+ xmlStr = string `1311.11414.11515.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'seq_XsdSequenceArray' occurs more than the max allowed times");
+}
+
+type XsdSequenceArray2 record {|
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 2
+ }
+ Seq_XsdSequenceArray2[] seq_XsdSequenceArray2;
+
+ @Sequence {
+ minOccurs: 0,
+ maxOccurs: 2
+ }
+ Seq_XsdSequenceArray2_2[] seq_XsdSequenceArray2_2 = [];
+|};
+
+type Seq_XsdSequenceArray2 record {|
+ @SequenceOrder {
+ value: 1
+ }
+ int age;
+
+ @SequenceOrder {
+ value: 2
+ }
+ float salary;
+|};
+
+type Seq_XsdSequenceArray2_2 record {|
+ @SequenceOrder {
+ value: 1
+ }
+ int age2;
+
+ @SequenceOrder {
+ value: 2
+ }
+ float salary2;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdSequenceArray2() returns error? {
+ string xmlStr;
+ XsdSequenceArray2|Error v;
+
+ xmlStr = string `1311.11415.11311.11415.1`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_XsdSequenceArray2: [{age: 13, salary: 11.1}, {age: 14, salary: 15.1}], seq_XsdSequenceArray2_2: [{age2: 13, salary2: 11.1}, {age2: 14, salary2: 15.1}]});
+
+ xmlStr = string `1311.11311.11415.11311.11415.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'seq_XsdSequenceArray2' occurs more than the max allowed times");
+
+ xmlStr = string `1311.11415.11311.11311.11415.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'seq_XsdSequenceArray2_2' occurs more than the max allowed times");
+}
+
+type XSDSequenceArrayRecord13 record {
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Seq_XSDSequenceArrayRecord13_1[] seq_XSDSequenceArrayRecord13_1;
+
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Seq_XSDSequenceArrayRecord13_2[] seq_XSDSequenceArrayRecord13_2;
+};
+
+type Seq_XSDSequenceArrayRecord13_1 record {
+ @SequenceOrder {value: 1}
+ Seq_Array_A_3 field1;
+
+ @SequenceOrder {value: 2}
+ Seq_Array_B_3 field2;
+
+ @SequenceOrder {value: 3}
+ Seq_Array_C_3 field3;
+};
+
+type Seq_XSDSequenceArrayRecord13_2 record {
+ @SequenceOrder {value: 1}
+ Seq_Array_D_3 field4;
+
+ @SequenceOrder {value: 2}
+ Seq_Array_E_3 field5;
+
+ @SequenceOrder {value: 3}
+ Seq__Array_F_3 field6;
+};
+
+type Seq_Array_A_3 record {
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Seq_Array_3[] value1;
+};
+
+type Seq_Array_B_3 record {
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Seq2_Array_3[] value2;
+};
+
+type Seq_Array_C_3 record {
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Seq3_Array_3[] value3;
+};
+
+type Seq_Array_D_3 record {
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Seq_Array_3[] value1;
+};
+
+type Seq_Array_E_3 record {
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Seq2_Array_3[] value2;
+};
+
+type Seq__Array_F_3 record {
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Seq3_Array_3[] value3;
+};
+
+type Seq_Array_3 record {
+ @SequenceOrder {value: 1}
+ string a;
+
+ @SequenceOrder {value: 2}
+ string b;
+
+ @SequenceOrder {value: 3}
+ string c;
+};
+
+type Seq2_Array_3 record {
+ @SequenceOrder {value: 1}
+ string d;
+
+ @SequenceOrder {value: 2}
+ string e;
+
+ @SequenceOrder {value: 3}
+ string f;
+};
+
+type Seq3_Array_3 record {
+ @SequenceOrder {value: 1}
+ string g;
+
+ @SequenceOrder {value: 2}
+ string h;
+
+ @SequenceOrder {value: 3}
+ string i;
+};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXSDSequenceArrayRecord4() returns error? {
+ string xmlStr = string `123123123123123123`;
+ XSDSequenceArrayRecord13|Error v2 = parseString(xmlStr);
+ test:assertEquals(v2, {seq_XSDSequenceArrayRecord13_1: [{field1: {value1: [{a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}], seq_XSDSequenceArrayRecord13_2: [{field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}]}}]});
+
+ xmlStr = string `123123123123123123123123123123123123123123123123123123`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {seq_XSDSequenceArrayRecord13_1: [{field1: {value1: [{a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}, {field1: {value1: [{a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}, {field1: {value1: [{a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}], seq_XSDSequenceArrayRecord13_2: [{field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}]}}, {field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}]}}, {field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}]}}]});
+
+ xmlStr = string `123123123123123123123123123123123123`;
+ v2 = parseString(xmlStr);
+ test:assertEquals(v2, {seq_XSDSequenceArrayRecord13_1: [{field1: {value1: [{a: "1", b: "2", c: "3"}, {a: "1", b: "2", c: "3"}, {a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}], seq_XSDSequenceArrayRecord13_2: [{field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}, {d: "1", e: "2", f: "3"}, {d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}, {g: "1", h: "2", i: "3"}, {g: "1", h: "2", i: "3"}]}}]});
+
+ xmlStr = string `123123123123123123123123123123123123123123123123123123123123123123123123123123123`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'seq_XSDSequenceArrayRecord13_2' occurs more than the max allowed times");
+
+ xmlStr = string `123123123123123123123123123123123123123`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'value3' occurs more than the max allowed times");
+
+ xmlStr = string `123123123123123123123`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "Element(s) 'field6' is not found in 'seq_XSDSequenceArrayRecord13_2'");
+
+ xmlStr = string `123123123123121233123`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "Element(s) 'f' is not found in 'value2'");
+
+ xmlStr = string `123123123123123132`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "Element 'i' is not in the correct order in 'value3'");
+
+ xmlStr = string `132123123123123123`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "Element 'c' is not in the correct order in 'value1'");
+
+ xmlStr = string `123123123123123123`;
+ v2 = parseString(xmlStr);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "Element 'field5' is not in the correct order in 'seq_XSDSequenceArrayRecord13_2'");
+}
+
+type XsdSequenceArray5 record {|
+ @Sequence {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Seq_XsdSequenceArray5[] seq_XsdSequenceArray5;
+|};
+
+type Seq_XsdSequenceArray5 record {|
+ @SequenceOrder {
+ value: 1
+ }
+ int age;
+
+ @SequenceOrder {
+ value: 2
+ }
+ float salary;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdSequenceArray5() returns error? {
+ string xmlStr;
+ XsdSequenceArray5|Error v;
+
+ xmlStr = string `1311.11415.1`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_XsdSequenceArray5: [{age: 13, salary: 11.1}, {age: 14, salary: 15.1}]});
+
+ xmlStr = string `1311.11414.11515.1`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_XsdSequenceArray5: [{age: 13, salary: 11.1}, {age: 14, salary: 14.1}, {age: 15, salary: 15.1}]});
+
+ xmlStr = string `1311.11414.11515.11515.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'seq_XsdSequenceArray5' occurs more than the max allowed times");
+
+ xmlStr = string `1311.1`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'seq_XsdSequenceArray5' occurs less than the min required times");
+}
+
+type XSDSequenceArrayRecord6 record {
+ @Sequence {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Seq_XSDSequenceArrayRecord6_1[] seq_XSDSequenceArrayRecord6_1;
+
+ @Sequence {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Seq_XSDSequenceArrayRecord6_2[] seq_XSDSequenceArrayRecord6_2;
+};
+
+type Seq_XSDSequenceArrayRecord6_1 record {
+ @SequenceOrder {value: 1}
+ Seq_Array_A_6 field1;
+
+ @SequenceOrder {value: 2}
+ Seq_Array_B_6 field2;
+};
+
+type Seq_XSDSequenceArrayRecord6_2 record {
+ @SequenceOrder {value: 1}
+ Seq_Array_D_6 field4;
+
+ @SequenceOrder {value: 2}
+ Seq_Array_E_6 field5;
+};
+
+type Seq_Array_A_6 record {
+ @Sequence {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Seq_Array_6[] value1;
+};
+
+type Seq_Array_B_6 record {
+ @Sequence {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Seq2_Array_6[] value2;
+};
+
+type Seq_Array_D_6 record {
+ @Sequence {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Seq_Array_6[] value1;
+};
+
+type Seq_Array_E_6 record {
+ @Sequence {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Seq2_Array_6[] value2;
+};
+
+type Seq_Array_6 record {
+ @SequenceOrder {value: 1}
+ string a;
+};
+
+type Seq2_Array_6 record {
+ @SequenceOrder {value: 1}
+ string d;
+};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXSDSequenceArrayRecord6() returns error? {
+ string xmlStr = string `1111111111111111`;
+ XSDSequenceArrayRecord6|Error v2 = parseString(xmlStr);
+ test:assertEquals(v2, {"seq_XSDSequenceArrayRecord6_1":[{"field1":{"value1":[{"a":"1"},{"a":"1"}]},"field2":{"value2":[{"d":"1"},{"d":"1"}]}},{"field1":{"value1":[{"a":"1"},{"a":"1"}]},"field2":{"value2":[{"d":"1"},{"d":"1"}]}}],"seq_XSDSequenceArrayRecord6_2":[{"field4":{"value1":[{"a":"1"},{"a":"1"}]},"field5":{"value2":[{"d":"1"},{"d":"1"}]}},{"field4":{"value1":[{"a":"1"},{"a":"1"}]},"field5":{"value2":[{"d":"1"},{"d":"1"}]}}]});
+}
diff --git a/ballerina/tests/xsd_sequence_array_test_with_parse_type.bal b/ballerina/tests/xsd_sequence_array_test_with_parse_type.bal
new file mode 100644
index 00000000..91f099e6
--- /dev/null
+++ b/ballerina/tests/xsd_sequence_array_test_with_parse_type.bal
@@ -0,0 +1,329 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+@Name {
+ value: "Root"
+}
+type XsdSequenceArrayWithXmlValue record {|
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 2
+ }
+ Seq_XsdSequenceArrayWithXmlValue[] seq_XsdSequenceArrayWithXmlValue;
+|};
+
+type Seq_XsdSequenceArrayWithXmlValue record {|
+ @SequenceOrder {
+ value: 1
+ }
+ int age;
+
+ @SequenceOrder {
+ value: 2
+ }
+ float salary;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdSequenceArrayWithXmlValue() returns error? {
+ xml xmlValue;
+ XsdSequenceArrayWithXmlValue|Error v;
+ xml|Error toXmlresult;
+
+ xmlValue = xml `1311.11415.1`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_XsdSequenceArrayWithXmlValue: [{age: 13, salary: 11.1}, {age: 14, salary: 15.1}]});
+ test:assertEquals(toXml(check v), xml `1311.11415.1`);
+
+ xmlValue = xml `1311.11414.11515.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'seq_XsdSequenceArrayWithXmlValue' occurs more than the max allowed times");
+ toXmlresult = toXml({seq_XsdSequenceArrayWithXmlValue: [{age: 13, salary: 11.1}, {age: 14, salary: 14.1}, {age: 15, salary: 15.1}]});
+ test:assertTrue(toXmlresult is error);
+ test:assertEquals((toXmlresult).message(), "'seq_XsdSequenceArrayWithXmlValue' occurs more than the max allowed times");
+
+ xmlValue = xml `1311.114`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "Element(s) 'salary' is not found in 'seq_XsdSequenceArrayWithXmlValue'");
+
+ xmlValue = xml `131415.111.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'age' occurs more than the max allowed times in 'seq_XsdSequenceArrayWithXmlValue'");
+}
+
+@Name {
+ value: "Root"
+}
+type XsdSequenceArrayWithXmlValue2 record {|
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 2
+ }
+ Seq_XsdSequenceArrayWithXmlValue2[] seq_XsdSequenceArrayWithXmlValue2;
+
+ @Sequence {
+ minOccurs: 0,
+ maxOccurs: 2
+ }
+ Seq_XsdSequenceArrayWithXmlValue2_2[] seq_XsdSequenceArrayWithXmlValue2_2 = [];
+|};
+
+type Seq_XsdSequenceArrayWithXmlValue2 record {|
+ @SequenceOrder {
+ value: 1
+ }
+ int age;
+
+ @SequenceOrder {
+ value: 2
+ }
+ float salary;
+|};
+
+type Seq_XsdSequenceArrayWithXmlValue2_2 record {|
+ @SequenceOrder {
+ value: 1
+ }
+ int age2;
+
+ @SequenceOrder {
+ value: 2
+ }
+ float salary2;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdSequenceArrayWithXmlValue2() returns error? {
+ xml xmlValue;
+ XsdSequenceArrayWithXmlValue2|Error v;
+ xml|Error toXmlresult;
+
+ xmlValue = xml `1311.11415.11311.11415.1`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_XsdSequenceArrayWithXmlValue2: [{age: 13, salary: 11.1}, {age: 14, salary: 15.1}], seq_XsdSequenceArrayWithXmlValue2_2: [{age2: 13, salary2: 11.1}, {age2: 14, salary2: 15.1}]});
+ test:assertEquals(toXml(check v), xml `1311.11415.11311.11415.1`);
+
+ xmlValue = xml `1311.11311.11415.11311.11415.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'seq_XsdSequenceArrayWithXmlValue2' occurs more than the max allowed times");
+ toXmlresult = toXml({seq_XsdSequenceArrayWithXmlValue2: [{age: 13, salary: 11.1}, {age: 13, salary: 11.1}, {age: 14, salary: 15.1}], seq_XsdSequenceArrayWithXmlValue2_2: [{age2: 13, salary2: 11.1}, {age2: 14, salary2: 15.1}]});
+ test:assertTrue(toXmlresult is error);
+ test:assertEquals((toXmlresult).message(), "'seq_XsdSequenceArrayWithXmlValue2' occurs more than the max allowed times");
+
+ xmlValue = xml `1311.11415.11311.11311.11415.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is error);
+ test:assertEquals((v).message(), "'seq_XsdSequenceArrayWithXmlValue2_2' occurs more than the max allowed times");
+ toXmlresult = toXml({seq_XsdSequenceArrayWithXmlValue2: [{age: 13, salary: 11.1}, {age: 14, salary: 15.1}], seq_XsdSequenceArrayWithXmlValue2_2: [{age2: 13, salary2: 11.1}, {age2: 13, salary2: 11.1}, {age2: 14, salary2: 15.1}]});
+ test:assertTrue(toXmlresult is error);
+ test:assertEquals((toXmlresult).message(), "'seq_XsdSequenceArrayWithXmlValue2_2' occurs more than the max allowed times");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDSequenceArrayWithXmlValueRecord13 record {
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Seq_XSDSequenceArrayWithXmlValueRecord13_1[] seq_XSDSequenceArrayWithXmlValueRecord13_1;
+
+ @Sequence {
+ minOccurs: 1,
+ maxOccurs: 3
+ }
+ Seq_XSDSequenceArrayWithXmlValueRecord13_2[] seq_XSDSequenceArrayWithXmlValueRecord13_2;
+};
+
+type Seq_XSDSequenceArrayWithXmlValueRecord13_1 record {
+ @SequenceOrder {value: 1}
+ Seq_Array_A_3 field1;
+
+ @SequenceOrder {value: 2}
+ Seq_Array_B_3 field2;
+
+ @SequenceOrder {value: 3}
+ Seq_Array_C_3 field3;
+};
+
+type Seq_XSDSequenceArrayWithXmlValueRecord13_2 record {
+ @SequenceOrder {value: 1}
+ Seq_Array_D_3 field4;
+
+ @SequenceOrder {value: 2}
+ Seq_Array_E_3 field5;
+
+ @SequenceOrder {value: 3}
+ Seq__Array_F_3 field6;
+};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXSDSequenceArrayWithXmlValueRecord4() returns error? {
+ xml|Error toXmlresult;
+
+ xml xmlValue = xml `123123123123123123`;
+ XSDSequenceArrayWithXmlValueRecord13|Error v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {seq_XSDSequenceArrayWithXmlValueRecord13_1: [{field1: {value1: [{a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}], seq_XSDSequenceArrayWithXmlValueRecord13_2: [{field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}]}}]});
+ test:assertEquals(toXml(check v2), xml `123123123123123123`);
+
+ xmlValue = xml `123123123123123123123123123123123123123123123123123123`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {seq_XSDSequenceArrayWithXmlValueRecord13_1: [{field1: {value1: [{a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}, {field1: {value1: [{a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}, {field1: {value1: [{a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}], seq_XSDSequenceArrayWithXmlValueRecord13_2: [{field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}]}}, {field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}]}}, {field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}]}}]});
+ test:assertEquals(toXml(check v2), xml `123123123123123123123123123123123123123123123123123123`);
+
+ xmlValue = xml `123123123123123123123123123123123123`;
+ v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {seq_XSDSequenceArrayWithXmlValueRecord13_1: [{field1: {value1: [{a: "1", b: "2", c: "3"}, {a: "1", b: "2", c: "3"}, {a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}], seq_XSDSequenceArrayWithXmlValueRecord13_2: [{field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}, {d: "1", e: "2", f: "3"}, {d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}, {g: "1", h: "2", i: "3"}, {g: "1", h: "2", i: "3"}]}}]});
+ test:assertEquals(toXml(check v2), xml `123123123123123123123123123123123123`);
+
+ xmlValue = xml `123123123123123123123123123123123123123123123123123123123123123123123123123123123`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'seq_XSDSequenceArrayWithXmlValueRecord13_2' occurs more than the max allowed times");
+ toXmlresult = toXml({seq_XSDSequenceArrayWithXmlValueRecord13_1: [{field1: {value1: [{a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}, {field1: {value1: [{a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}, {field1: {value1: [{a: "1", b: "2", c: "3"}]}, field2: {value2: [{d: "1", e: "2", f: "3"}]}, field3: {value3: [{g: "1", h: "2", i: "3"}]}}], seq_XSDSequenceArrayWithXmlValueRecord13_2: [{field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}]}}, {field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}]}}, {field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}]}}, {field4: {value1: [{a: "1", b: "2", c: "3"}]}, field5: {value2: [{d: "1", e: "2", f: "3"}, {d: "1", e: "2", f: "3"}, {d: "1", e: "2", f: "3"}]}, field6: {value3: [{g: "1", h: "2", i: "3"}, {g: "1", h: "2", i: "3"}, {g: "1", h: "2", i: "3"}]}}]});
+ test:assertTrue(toXmlresult is error);
+ test:assertEquals((toXmlresult).message(), "'seq_XSDSequenceArrayWithXmlValueRecord13_2' occurs more than the max allowed times");
+
+ xmlValue = xml `123123123123123123123123123123123123123`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "'value3' occurs more than the max allowed times");
+}
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXSDSequenceArrayWithXmlValueRecord4P2() returns error? {
+ xml xmlValue = xml `123123123123123123123`;
+ XSDSequenceArrayWithXmlValueRecord13|Error v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "Element(s) 'field6' is not found in 'seq_XSDSequenceArrayWithXmlValueRecord13_2'");
+
+ xmlValue = xml `123123123123121233123`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "Element(s) 'f' is not found in 'value2'");
+
+ xmlValue = xml `123123123123123132`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "Element 'i' is not in the correct order in 'value3'");
+
+ xmlValue = xml `132123123123123123`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "Element 'c' is not in the correct order in 'value1'");
+
+ xmlValue = xml `123123123123123123`;
+ v2 = parseAsType(xmlValue);
+ test:assertTrue(v2 is Error);
+ test:assertEquals((v2).message(), "Element 'field5' is not in the correct order in 'seq_XSDSequenceArrayWithXmlValueRecord13_2'");
+}
+
+type XsdSequenceArrayWithXmlValue5 record {|
+ @Sequence {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Seq_XsdSequenceArrayWithXmlValue5[] seq_XsdSequenceArrayWithXmlValue5;
+|};
+
+type Seq_XsdSequenceArrayWithXmlValue5 record {|
+ @SequenceOrder {
+ value: 1
+ }
+ int age;
+
+ @SequenceOrder {
+ value: 2
+ }
+ float salary;
+|};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXsdSequenceArrayWithXmlValue5() returns error? {
+ xml xmlValue;
+ XsdSequenceArrayWithXmlValue5|Error v;
+
+ xmlValue = xml `1311.11415.1`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_XsdSequenceArrayWithXmlValue5: [{age: 13, salary: 11.1}, {age: 14, salary: 15.1}]});
+ test:assertEquals(toXml(check v), xml `1311.11415.1`);
+
+ xmlValue = xml `1311.11414.11515.1`;
+ v = parseAsType(xmlValue);
+ test:assertEquals(v, {seq_XsdSequenceArrayWithXmlValue5: [{age: 13, salary: 11.1}, {age: 14, salary: 14.1}, {age: 15, salary: 15.1}]});
+ test:assertEquals(toXml(check v), xml `1311.11414.11515.1`);
+
+ xmlValue = xml `1311.11414.11515.11515.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'seq_XsdSequenceArrayWithXmlValue5' occurs more than the max allowed times");
+
+ xmlValue = xml `1311.1`;
+ v = parseAsType(xmlValue);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'seq_XsdSequenceArrayWithXmlValue5' occurs less than the min required times");
+ xml|Error toXmlresult = toXml({seq_XsdSequenceArrayWithXmlValue5: [{age: 13, salary: 11.1}]});
+ test:assertTrue(toXmlresult is error);
+ test:assertEquals((toXmlresult).message(), "'seq_XsdSequenceArrayWithXmlValue5' occurs less than the min required times");
+}
+
+@Name {
+ value: "Root"
+}
+type XSDSequenceArrayWithXmlValueRecord6 record {
+ @Sequence {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Seq_XSDSequenceArrayWithXmlValueRecord6_1[] seq_XSDSequenceArrayWithXmlValueRecord6_1;
+
+ @Sequence {
+ minOccurs: 2,
+ maxOccurs: 3
+ }
+ Seq_XSDSequenceArrayWithXmlValueRecord6_2[] seq_XSDSequenceArrayWithXmlValueRecord6_2;
+};
+
+type Seq_XSDSequenceArrayWithXmlValueRecord6_1 record {
+ @SequenceOrder {value: 1}
+ Seq_Array_A_6 field1;
+
+ @SequenceOrder {value: 2}
+ Seq_Array_B_6 field2;
+};
+
+type Seq_XSDSequenceArrayWithXmlValueRecord6_2 record {
+ @SequenceOrder {value: 1}
+ Seq_Array_D_6 field4;
+
+ @SequenceOrder {value: 2}
+ Seq_Array_E_6 field5;
+};
+
+@test:Config {groups: ["xsd", "xsd_sequence"]}
+function testXSDSequenceArrayWithXmlValueRecord6() returns error? {
+ xml xmlValue = xml `1211111111111111`;
+ XSDSequenceArrayWithXmlValueRecord6|Error v2 = parseAsType(xmlValue);
+ test:assertEquals(v2, {"seq_XSDSequenceArrayWithXmlValueRecord6_1":[{"field1":{"value1":[{"a":"1"},{"a":"2"}]},"field2":{"value2":[{"d":"1"},{"d":"1"}]}},{"field1":{"value1":[{"a":"1"},{"a":"1"}]},"field2":{"value2":[{"d":"1"},{"d":"1"}]}}],"seq_XSDSequenceArrayWithXmlValueRecord6_2":[{"field4":{"value1":[{"a":"1"},{"a":"1"}]},"field5":{"value2":[{"d":"1"},{"d":"1"}]}},{"field4":{"value1":[{"a":"1"},{"a":"1"}]},"field5":{"value2":[{"d":"1"},{"d":"1"}]}}]});
+ test:assertEquals(toXml(check v2), xml `1211111111111111`);
+}
diff --git a/ballerina/tests/xsd_sequence_test_with_element_annotation.bal b/ballerina/tests/xsd_sequence_test_with_element_annotation.bal
new file mode 100644
index 00000000..5f8b8e16
--- /dev/null
+++ b/ballerina/tests/xsd_sequence_test_with_element_annotation.bal
@@ -0,0 +1,421 @@
+// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
+//
+// WSO2 LLC. licenses this file to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file except
+// in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+import ballerina/test;
+
+// TODO: Add tests with attributes
+type XsdSequenceWithElementAnnotation record {
+ @Sequence {
+ minOccurs: 0,
+ maxOccurs: 1
+ }
+ Seq_EA1 seq_EA1;
+};
+
+type Seq_EA1 record {
+
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ @SequenceOrder {
+ value: 1
+ }
+ string EA1?;
+
+ @Element {
+ maxOccurs: 1,
+ minOccurs: 0
+ }
+ @SequenceOrder {
+ value: 2
+ }
+ string EA2?;
+
+ @Element {
+ maxOccurs: 4,
+ minOccurs: 2
+ }
+ @SequenceOrder {
+ value: 3
+ }
+ string[] EA3?;
+};
+
+@test:Config {groups: ["xsd", "xsd_sequence", "xsd_element", "xsd_element_and_sequence"]}
+function testXsdSequenceWithElementAnnotation() returns error? {
+ string xmlStr;
+ XsdSequenceWithElementAnnotation|Error v;
+
+ xmlStr = string `ABCABC`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "Element(s) 'EA3' is not found in 'seq_EA1'");
+
+ xmlStr = string `ABCABCABABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {seq_EA1: {EA1: "ABC", EA2: "ABC", EA3: ["AB", "AB", "AB"]}});
+
+ xmlStr = string `ABCABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA1":{"EA2": "ABC", EA3: ["AB", "AB"]}});
+
+ xmlStr = string `ABCABABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA1":{"EA2": "ABC", EA3: ["AB", "AB", "AB"]}});
+
+ xmlStr = string `ABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA1":{EA3: ["AB", "AB"]}});
+
+ xmlStr = string `ABABABABABAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs more than the max allowed times");
+
+ xmlStr = string `ABCABAB`;
+ v = parseString(xmlStr);
+ test:assertEquals(v, {"seq_EA1":{"EA1": "ABC", EA3: ["AB", "AB"]}});
+
+ xmlStr = string `ABCABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlStr = string `ABCAB`;
+ v = parseString(xmlStr);
+ test:assertTrue(v is Error);
+ test:assertEquals((v).message(), "'EA3' occurs less than the min required times");
+
+ xmlStr = string `