Scala 的 XML 相等问题 [英] XML equality problem with Scala

查看:50
本文介绍了Scala 的 XML 相等问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了 Scala 中 XML 相等的一个特性:

I've stumbled upon a peculiarity 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

我认为正在发生的是正在创建两个 xml.Text 对象,这与一个不同.但是,这不是它在 XML 规范中的工作方式 :) 我想知道是否有任何方法可以比较相等性以便返回 true.

What I think is happening is that two xml.Text objects are being created, and that is different than one. However, this isn't how it works in the XML spec :) and I wonder if there is any way to compare equality so that this would return true.

谢谢!

推荐答案

<a>12</a> 表示具有值为12"的单个子节点的元素,而<a>{1}2</a> 表示具有两个子节点的元素,值分别为1"和2".

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

它们在 Scala 中在逻辑上是可区分的:x.childArrayBuffer(12)y.childArrayBuffer(1), 2),因此它们是不等的.

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

XML 规范怎么样?根据我的阅读,这两个 XML 对象 不相等.根据 XML 规范,元素的内容由一个或更多的东西(DOM 称之为节点"),这些东西可以是 CharData.因此,一个元素有两个相邻的 CharData 子元素是合乎逻辑的,这在逻辑上被认为与单个串联的 CharData 子元素不同.

What about the XML spec? By my reading, those two XML objects are not equal. According to the XML spec, an element's contents consists of a sequence of one or more things (which DOM calls "nodes"), and those things can be CharData. Therefore, it is logical for an element to have two adjacent CharData children, and this is considered logically different from a single concatenated CharData child.

如果你真的想认为它们相等,你应该编写一个规范化过程,它接受一个 XML 对象并连接任何相邻的文本节点,然后执行相等测试.

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

这篇关于Scala 的 XML 相等问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆