我面临 XML 值深度相等检查的问题。在提供的代码示例中,我有两个 XML 字符串(string1 和 string2),它们表示相似的 XML 内容,但是当我使用
xml1 == xml2
比较它们时,它返回 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
}
这是预期的行为吗?如果是,原因是什么?
您在 Ballerina 中对 XML 值进行深度相等检查时遇到的问题与 XML 字符串中存在空格有关。
在 Ballerina 中,XML 字符串中的空格被解析为
xml:Text
项。因此,当 string1
值中有额外的换行符时,它会引入额外的 xml:Text
成员,导致深度相等检查返回 false
,即使内容在概念上是相同的。