I'm facing an issue with the deep equality check for XML values. In the provided code sample, I have two XML strings (string1 and string2) that represent similar XML content, but when I compare them using xml1 == xml2
, it returns false
.
import ballerina/io;
xmlns "http://www.so2w.org" as s;
public function main() returns error? {
string string1 = string
`<s:FuelEvents xmlns:s="http://www.so2w.org">
<s:FuelEvent employeeId="2312">
<s:odometerReading>230</s:odometerReading>
<s:gallons>18.561</s:gallons>
<s:gasPrice>4.56</s:gasPrice>
</s:FuelEvent>
</s:FuelEvents>`;
string string2 = string
`<s:FuelEvents xmlns:s="http://www.so2w.org">
<s:FuelEvent employeeId="2312">
<s:odometerReading>230</s:odometerReading>
<s:gallons>18.561</s:gallons>
<s:gasPrice>4.56</s:gasPrice>
</s:FuelEvent>
</s:FuelEvents>`;
xml xml1 = check xml:fromString(string1);
xml xml2 = check xml:fromString(string2);
io:println(xml1 == xml2); // returns false
}
Is this the expected behaviour and if so, what's the reason for that?
The issue you're facing with the deep equality check for Ballerina XML values is related to the presence of whitespaces in the XML strings.
In Ballerina, whitespace in XML strings gets parsed as xml:Text
items. Therefore, when you have extra newlines in the string1
value, it introduces additional xml:Text
members, causing the deep equality check to return false
even though the content is conceptually the same.