1

Elem(prefix, label, attribs, scope, child) 方法在 Scala 2.10 中被弃用,应该使用 Elem(prefix, label, 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。

谢谢,

4

1 回答 1

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

我创建了一个错误报告

于 2013-06-28T06:03:30.803 回答