为什么Scala XML文字对标记之间的空格敏感?

Dav*_*vid 14 xml scala

我发现Scala XML文字对空白很敏感,这有点奇怪,不是吗?因为XML解析器通常不会对标签之间的空格给出任何关注.

这是一个无赖,因为我想在我的代码中整齐地设置我的XML:

<sample>
  <hello />
</sample>
Run Code Online (Sandbox Code Playgroud)

但斯卡拉认为这是一个不同的价值

<sample><hello /></sample>
Run Code Online (Sandbox Code Playgroud)

布丁的证据是:

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
Run Code Online (Sandbox Code Playgroud)

... 是什么赋予了?

Dan*_*ral 16

如果你喜欢它,你应该修剪它:

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
Run Code Online (Sandbox Code Playgroud)