Reputation: 5214
I've discovered that Scala XML literals are sensitive to whitespace, which is kinda strange, isn't it? since XML parsers don't normally give a damn about spaces between the tags.
This is a bummer because I'd like to set out my XML neatly in my code:
<sample>
<hello />
</sample>
but Scala considers this to be a different value to
<sample><hello /></sample>
Proof is in the pudding:
scala> val xml1 = <sample><hello /></sample>
xml1: scala.xml.Elem = <sample><hello></hello></sample>
scala> val xml2 = <sample>
| <hello />
| </sample>
xml2: scala.xml.Elem =
<sample>
<hello></hello>
</sample>
scala> xml1 == <sample><hello /></sample>
res0: Boolean = true
scala> xml1 == xml2
res1: Boolean = false
... What gives?
Upvotes: 14
Views: 1871
Reputation: 297205
If you liked it you should have put a trim on it:
scala> val xml1 = <sample><hello /></sample>
xml1: scala.xml.Elem = <sample><hello></hello></sample>
scala> val xml2 = <sample>
| <hello />
| </sample>
xml2: scala.xml.Elem =
<sample>
<hello></hello>
</sample>
scala> xml1 == xml2
res14: Boolean = false
scala> xml.Utility.trim(xml1) == xml.Utility.trim(xml2)
res15: Boolean = true
Upvotes: 16
Reputation: 4193
If you want to convert the XML literal to a StringBuilder
:
scala> val xml1 = <sample><hello /></sample>
xml1: scala.xml.Elem = <sample><hello></hello></sample>
scala> xml.Utility.toXML(xml1, minimizeTags=true)
res2: StringBuilder = <sample><hello /></sample>
Upvotes: 0