在Scala 2.10中不推荐使用Elem(prefix,label,attribs,scope,child)方法,应使用Elem(前缀,标签,attribs,scope,minimizeEmpty,child)。但是至少在空元素上,新的apply方法具有不同的行为。不推荐的版本以这种方式工作:
val inputXML = <PC Price="100" CurrencyCode="USD"/>
val rule1 = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case Elem(prefix, "PC", attribs, scope, content @ _*) =>
Elem(prefix, "Mac", attribs, scope, content:_*)
case other => other
}
}
println(new RuleTransformer(rule1).transform(inputXML))
打印:
List(<Mac Price="100" CurrencyCode="USD"/>)
但是当使用新的Elem方法时,匹配不起作用:
val inputXML = <PC Price="100" CurrencyCode="USD"/>
val rule1 = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case Elem(prefix, "PC", attribs, scope, minimizeEmpty, content @ _*) =>
Elem(prefix, "Mac", attribs, scope, true, content:_*)
case other => other
}
}
println(new RuleTransformer(rule1).transform(inputXML))
上面的代码返回:
<PC Price="100" CurrencyCode="USD"/>
这是一个错误还是我做错了什么?我正在使用Scala 2.10.2。
谢谢,
答案 0 :(得分:3)
unapply
中只有一个Elem
方法:
def unapplySeq(n: Node): Option[(String, String, scala.xml.MetaData, scala.xml.NamespaceBinding, Seq[scala.xml.Node])]
它返回Tuple5
,但您尝试使用它,就像返回Tuple6
一样。您无法使用minimizeEmpty
方法获取unapplySeq
。
val inputXML = <PC Price="100" CurrencyCode="USD"/>
val rule1 = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case Elem(prefix, "PC", attribs, scope, content @ _*) =>
Elem(prefix, "Mac", attribs, scope, true, content:_*)
case other => other
}
}
println(new RuleTransformer(rule1).transform(inputXML))
// List(<Mac Price="100" CurrencyCode="USD"/>)
但我想这里有一个错误。此方法应命名为unapply
以显示编译错误:
object ElemHelper {
def unapply(n: Node): Option[(String, String, scala.xml.MetaData, scala.xml.NamespaceBinding, Seq[scala.xml.Node])] = Elem.unapplySeq(n)
}
val rule1 = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case ElemHelper(prefix, "PC", attribs, scope, minimizeEmpty, content @ _*) =>
Elem(prefix, "Mac", attribs, scope, true, content:_*)
case other => other
}
}
// <console>:16: error: wrong number of arguments for object ElemHelper
我创建了bug report。