XML equality issue with Scala

I came across a feature of XML equality in Scala:

scala> val x = <a>12</a> x: scala.xml.Elem = <a>12</a> scala> val y = <a>{1}2</a> y: scala.xml.Elem = <a>12</a> scala> x == y res0: Boolean = false 

I think it happens that two xml.Text objects are xml.Text , and this is different from one. However, this is not how it works in the XML specification :), and I am wondering if there is a way to compare equality so that it returns true.

Thanks!

+7
source share
1 answer

<a>12</a> represents an element with a single child element node with a value of "12", while <a>{1}2</a> represents an element with two child nodes with a value of "1" and "2" respectively.

They are logically distinguishable in Scala: x.child is ArrayBuffer(12) , while y.child is ArrayBuffer(1, 2) , and therefore they are unequal.

What about the XML specification? According to my data, these two XML objects are not equal. According to the XML specification , the content of an element consists of a sequence of one or more things (which the DOM calls "nodes"), and these things can be CharData . Therefore, it is logical that an element has two adjacent CharData children, and this is considered to be logically different from one concatenated CharData child.

If you really want to consider them equal, you should write a normalization pass that takes an XML object and concatenates any adjacent text nodes, and then performs an equality test.

+4
source

All Articles