diff --git a/ballerina/tests/to_xml_test.bal b/ballerina/tests/to_xml_test.bal
index 9a278f88..80dd843c 100644
--- a/ballerina/tests/to_xml_test.bal
+++ b/ballerina/tests/to_xml_test.bal
@@ -74,7 +74,7 @@ isolated function testMapStringToXml2() returns error? {
type Customer record {
@Name {
- value: "ns:employeeName"
+ value: "employeeName"
}
@Attribute
string ns\:name;
@@ -103,7 +103,7 @@ isolated function testRecordWithAnnotationToXml1() returns error? {
type Customer2 record {
@Name {
- value: "ns:employeeName"
+ value: "employeeName"
}
@Attribute
string ns\:name;
@@ -133,7 +133,7 @@ type Customer3 record {
@Attribute
@Name {
- value: "ns:employeeName"
+ value: "employeeName"
}
string ns\:name;
@@ -162,7 +162,7 @@ type Customer4 record {
@Attribute
@Name {
- value: "ns:employeeName"
+ value: "employeeName"
}
string ns\:name;
@@ -537,8 +537,14 @@ type BookStore6 record {
string storeName;
int postalCode;
boolean isOpen;
- Address6 address;
- Codes6 codes;
+ @Name{
+ value: "address"
+ }
+ Address6 add;
+ @Name{
+ value: "codes"
+ }
+ Codes6 codeValues;
@Attribute
string status;
@Attribute
@@ -552,7 +558,10 @@ type Address6 record {
};
type Codes6 record {
- int[] item;
+ @Name{
+ value: "item"
+ }
+ int[] items;
};
@test:Config {
@@ -564,13 +573,13 @@ isolated function testComplexRecordToXml() returns error? {
storeName: "foo",
postalCode: 94,
isOpen: true,
- address: {
+ add: {
street: "Galle Road",
city: "Colombo",
country: "Sri Lanka"
},
- codes: {
- item: [4, 8, 9]
+ codeValues: {
+ items: [4, 8, 9]
},
'xmlns\:ns0: "http://sample.com/test",
status: "online"
@@ -600,9 +609,9 @@ isolated function testComplexRecordToXml() returns error? {
@Namespace {
uri: "example.com"
}
-type Purchesed_Bill record {
- Purchesed_Items PurchesedItems;
- Purchesed_Address Address;
+type Purchased_Bill record {
+ Purchased_Items PurchasedItems;
+ Purchased_Address Address;
@Attribute
string 'xmlns\:ns?;
@Attribute
@@ -611,16 +620,16 @@ type Purchesed_Bill record {
string ns\:attr?;
};
-type Purchesed_Items record {
- Purchesed_Purchase[] PLine;
+type Purchased_Items record {
+ Purchased_Purchase[] PLine;
};
-type Purchesed_Purchase record {
- string|Purchesed_ItemCode ItemCode;
+type Purchased_Purchase record {
+ string|Purchased_ItemCode ItemCode;
int Count;
};
-type Purchesed_ItemCode record {
+type Purchased_ItemCode record {
@Attribute
string discount;
string \#content?;
@@ -629,7 +638,7 @@ type Purchesed_ItemCode record {
@Namespace {
uri: ""
}
-type Purchesed_Address record {
+type Purchased_Address record {
string StreetAddress;
string City;
int Zip;
@@ -639,9 +648,9 @@ type Purchesed_Address record {
@test:Config {
groups: ["toXml"]
}
-isolated function testRecordWithNamaspaceAnnotationToXml() returns error? {
- Purchesed_Bill input = {
- PurchesedItems: {
+isolated function testRecordWithNamespaceAnnotationToXml() returns error? {
+ Purchased_Bill input = {
+ PurchasedItems: {
PLine: [
{ItemCode: "223345", Count: 10},
{ItemCode: "223300", Count: 7},
@@ -662,8 +671,8 @@ isolated function testRecordWithNamaspaceAnnotationToXml() returns error? {
ns\:attr: "ns-attr-val"
};
string expected =
- "" +
- "" +
+ "" +
+ "" +
"" +
"223345" +
"10" +
@@ -676,14 +685,14 @@ isolated function testRecordWithNamaspaceAnnotationToXml() returns error? {
"200777" +
"7" +
"" +
- "" +
- "" +
+ "" +
+ "" +
"20, Palm grove, Colombo 3" +
"Colombo" +
"300" +
"LK" +
"" +
- "";
+ "";
xml result = check toXml(input);
test:assertEquals(result.toString(), expected, msg = "testComplexRecordToXml result incorrect");
}
@@ -691,8 +700,11 @@ isolated function testRecordWithNamaspaceAnnotationToXml() returns error? {
@Namespace {
uri: "example.com"
}
-type Purchesed_Bill1 record {
- Purchesed_Items1 PurchesedItems;
+type Purchased_Bill1 record {
+ @Name{
+ value: "PurchasedItems"
+ }
+ Purchased_Items1 PurchasedItem;
@Attribute
string 'xmlns\:ns?;
@Attribute
@@ -705,12 +717,12 @@ type Purchesed_Bill1 record {
prefix: "ns0",
uri: "example.com"
}
-type Purchesed_Items1 record {
- Purchesed_Purchase1[] PLine;
+type Purchased_Items1 record {
+ Purchased_Purchase1[] PLine;
};
-type Purchesed_Purchase1 record {
- string|Purchesed_ItemCode1 ItemCode;
+type Purchased_Purchase1 record {
+ string|Purchased_ItemCode1 ItemCode;
int Count;
};
@@ -718,7 +730,7 @@ type Purchesed_Purchase1 record {
prefix: "ns2",
uri: "example1.com"
}
-type Purchesed_ItemCode1 record {
+type Purchased_ItemCode1 record {
@Attribute
string discount;
string \#content?;
@@ -728,8 +740,8 @@ type Purchesed_ItemCode1 record {
groups: ["toXml"]
}
isolated function testRecordWithNamaspaceAnnotationToXml1() returns error? {
- Purchesed_Bill1 input = {
- PurchesedItems: {
+ Purchased_Bill1 input = {
+ PurchasedItem: {
PLine: [
{ItemCode: "223345", Count: 10},
{ItemCode: "223300", Count: 7},
@@ -744,8 +756,8 @@ isolated function testRecordWithNamaspaceAnnotationToXml1() returns error? {
ns\:attr: "ns-attr-val"
};
string expected =
- "" +
- "" +
+ "" +
+ "" +
"" +
"223345" +
"10" +
@@ -758,8 +770,8 @@ isolated function testRecordWithNamaspaceAnnotationToXml1() returns error? {
"200777" +
"7" +
"" +
- "" +
- "";
+ "" +
+ "";
xml result = check toXml(input);
test:assertEquals(result.toString(), expected, msg = "testRecordWithNamaspaceAnnotationToXml1 result incorrect");
}
@@ -768,8 +780,8 @@ isolated function testRecordWithNamaspaceAnnotationToXml1() returns error? {
prefix: "ns0",
uri: "example.com"
}
-type Purchesed_Bill2 record {
- Purchesed_Items2 purchesedItems;
+type Purchased_Bill2 record {
+ Purchased_Items2 PurchasedItems;
@Attribute
string 'xmlns\:ns?;
@Attribute
@@ -782,23 +794,23 @@ type Purchesed_Bill2 record {
prefix: "ns1",
uri: "example1.com"
}
-type Purchesed_Items2 record {
- Purchesed_Purchase2[] pLine;
+type Purchased_Items2 record {
+ Purchased_Purchase2[] pLine;
};
@Namespace {
prefix: "ns2",
uri: "example2.com"
}
-type Purchesed_Purchase2 record {
- string|Purchesed_ItemCode2 itemCode;
+type Purchased_Purchase2 record {
+ string|Purchased_ItemCode2 itemCode;
int count;
};
@Namespace {
uri: "example1.com"
}
-type Purchesed_ItemCode2 record {
+type Purchased_ItemCode2 record {
@Attribute
string discount;
string \#content?;
@@ -808,8 +820,8 @@ type Purchesed_ItemCode2 record {
groups: ["toXml"]
}
isolated function testRecordWithNamaspaceAnnotationToXml2() returns error? {
- Purchesed_Bill2 input = {
- purchesedItems: {
+ Purchased_Bill2 input = {
+ PurchasedItems: {
pLine: [
{itemCode: "223345", count: 10},
{itemCode: "223300", count: 7},
@@ -824,8 +836,8 @@ isolated function testRecordWithNamaspaceAnnotationToXml2() returns error? {
ns\:attr: "ns-attr-val"
};
string expected =
- "" +
- "" +
+ "" +
+ "" +
"" +
"223345" +
"10" +
@@ -838,8 +850,8 @@ isolated function testRecordWithNamaspaceAnnotationToXml2() returns error? {
"200777" +
"7" +
"" +
- "" +
- "";
+ "" +
+ "";
xml result = check toXml(input);
test:assertEquals(result.toString(), expected, msg = "testRecordWithNamaspaceAnnotationToXml2 result incorrect");
}
@@ -869,7 +881,7 @@ isolated function testRecordWithAnnotationToXml5() returns error? {
CustomerDetails data = {ns\:name: "Asha", age: 10};
xml result = check toXml(data);
test:assertEquals(result,
- xml `10`,
+ xml `10`,
msg = "testRecordWithAnnotationToXml5 result incorrect");
}
@@ -879,7 +891,7 @@ isolated function testRecordWithAnnotationToXml5() returns error? {
}
type Invoices record {
int id;
- string purchesedItem;
+ string PurchasedItem;
@Attribute
string 'xmlns?;
@Attribute
@@ -894,7 +906,7 @@ type Invoices record {
isolated function testRecordWithAnnotationToXml6() returns error? {
Invoices data = {
id: 1,
- purchesedItem: "soap",
+ PurchasedItem: "soap",
attr: "attr-val",
'xmlns: "example2.com",
ns\:attr: "example1.com"
@@ -902,7 +914,7 @@ isolated function testRecordWithAnnotationToXml6() returns error? {
string expected =
"" +
"1" +
- "soap" +
+ "soap" +
"";
xml result = check toXml(data);
test:assertEquals(result.toString(), expected, msg = "testRecordWithAnnotationToXml6 result incorrect");
@@ -957,3 +969,115 @@ isolated function testRecordWithAnnotationToXml7() returns error? {
xml result = check toXml(data);
test:assertEquals(result.toString(), expected, msg = "testRecordWithAnnotationToXml6 result incorrect");
}
+
+@Namespace {
+ prefix: "nso",
+ uri: "example.com"
+}
+@Name {
+ value: "PurchasedBill"
+}
+type Example record {
+ PurchasedItems PurchasedItems;
+ PurchasedAddress Address;
+ @Attribute
+ string 'xmlns\:ns?;
+ @Attribute
+ string attr?;
+ @Attribute
+ string ns\:attr?;
+};
+
+@Namespace {
+ prefix: "ns1",
+ uri: "example1.com"
+}
+@Name {
+ value: "PurchasedPurchase"
+}
+type PurchasedItems record {
+ Example1[] PLine;
+};
+
+@Namespace {
+ prefix: "ns2",
+ uri: "example1.com"
+}
+type Example1 record {
+ string|PurchasedItemCode ItemCode;
+ int Count;
+ @Attribute
+ string attr?;
+};
+
+@Namespace {
+ prefix: "ns3",
+ uri: "example1.com"
+}
+type PurchasedItemCode record {
+ @Attribute
+ string discount;
+ string \#content?;
+};
+
+@Namespace {
+ uri: "example3.com"
+}
+type PurchasedAddress record {
+ string StreetAddress;
+ string City;
+ int Zip;
+ string Country;
+};
+
+@test:Config {
+ groups: ["toXml"]
+}
+isolated function testRecordWithNamespaceAnnotationToXml1() returns error? {
+ Example input = {
+ PurchasedItems: {
+ PLine: [
+ {ItemCode: "223345", Count: 10, attr: "1"},
+ {ItemCode: "223300", Count: 7},
+ {
+ ItemCode: {discount: "22%", \#content: "200777"},
+ Count: 7
+ }
+ ]
+ },
+ Address: {
+ StreetAddress: "20, Palm grove, Colombo 3",
+ City: "Colombo",
+ Zip: 300,
+ Country: "LK"
+ },
+ 'xmlns\:ns: "ns.com",
+ attr: "attr-val",
+ ns\:attr: "ns-attr-val"
+ };
+ string expected =
+ "" +
+ "" +
+ "" +
+ "223345" +
+ "10" +
+ "" +
+ "" +
+ "223300" +
+ "7" +
+ "" +
+ "" +
+ "200777" +
+ "7" +
+ "" +
+ "" +
+ "" +
+ "20, Palm grove, Colombo 3" +
+ "Colombo" +
+ "300" +
+ "LK" +
+ "" +
+ "";
+ xml result = check toXml(input);
+ test:assertEquals(result.toString(), expected, msg = "testComplexRecordToXml result incorrect");
+}
diff --git a/ballerina/xmldata.bal b/ballerina/xmldata.bal
index d9380f47..2f5febdd 100644
--- a/ballerina/xmldata.bal
+++ b/ballerina/xmldata.bal
@@ -21,9 +21,9 @@ const string CONTENT = "#content";
const string ATTRIBUTE_PREFIX = "attribute_";
const string XMLNS = "xmlns";
-# Defines the new name of the name.
+# Defines the name of the XML element.
#
-# + value - The value of the new name
+# + value - The name of the XML element
public type NameConfig record {|
string value;
|};
@@ -47,8 +47,7 @@ public annotation NamespaceConfig Namespace on type;
public annotation Attribute on record field;
# Converts a `Map` or `Record` representation to its XML representation.
-# The record has annotations to configure namespaces and attributes,
-# but others don't have these.
+# XML `namespaces` and `attributes` can only defined through record annotations are only supported for record values.
#
# + mapValue - The `Map` or `Record` representation source to be converted to XML
# + return - XML representation of the given source if the source is
@@ -373,7 +372,7 @@ public isolated function toJson(xml xmlValue, XmlOptions options = {}) returns j
# + xmlValue - The XML source to be converted to a Record
# + preserveNamespaces - Instructs whether to preserve the namespaces of the XML when converting
# + returnType - The `typedesc` of the record that should be returned as a result.
-# The optional value fields are not allowed in the record type.
+# The optional value fields are not allowed in the record type.
# + return - The Record representation of the given XML on success, else returns an `xmldata:Error`
# # Deprecated
# This function is going away in a future release. Use `fromXml` instead.
@@ -384,13 +383,14 @@ returns returnType|Error = @java:Method {
} external;
# Converts an XML to its `Map` or `Record` representation.
-# The namespaces and attributes will not be considered a special case.
+# XML `namespaces` and `attributes` can only defined through record annotations are only supported for record values.
#
-# + xmlValue - The XML source to be converted to a given target type
-# + returnType - The `typedesc` of the `map` that should be returned as a result
+# + xmlValue - The XML source to be converted to a given target type. If the XML elements have a prefix,
+# the mapping field names of the record must also have the same prefix.
+# + returnType - The `typedesc` of the returned value. this should be either `map` or `record` type.
# + return - The given target type representation of the given XML on success,
-# else returns an `xmldata:Error`
-public isolated function fromXml(xml xmlValue, typedesc<(map)> returnType = <>)
+# else returns an `xmldata:Error`
+public isolated function fromXml(xml xmlValue, typedesc