使用特定的孩子创建scala.xml.Elem

问题描述:

我想基于具有特定孩子列表的节点创建新的scala.xml.Elem。 这将是递归替换函数中的返回情况。使用特定的孩子创建scala.xml.Elem

val childs: Seq[Node] = update(node.child) 
new Elem(node.prefix, node.label, node.attributes, node.scope, true, childs) 

这种结构会产生编译错误:

Error:(50, 11) overloaded method constructor Elem with alternatives: 
    (prefix: String,label: String,attributes: scala.xml.MetaData,scope: scala.xml.NamespaceBinding,child: scala.xml.Node*)scala.xml.Elem <and> 
    (prefix: String,label: String,attributes1: scala.xml.MetaData,scope: scala.xml.NamespaceBinding,minimizeEmpty: Boolean,child: scala.xml.Node*)scala.xml.Elem 
    cannot be applied to (String, String, scala.xml.MetaData, scala.xml.NamespaceBinding, Boolean, Seq[scala.xml.Node]) 
     new Elem(node.prefix, node.label, node.attributes, node.scope, true, childs) 
    ^

到VARARG处理的问题是相关的,我不明白为什么我有这样的错误。任何想法?

更新 我能够通过问题下面丑陋结构得到:

val childs: Seq[Node] = update(node.child) 
Elem(node.prefix, node.label, node.attributes, node.scope, true) 
    .copy(node.prefix, node.label, node.attributes, node.scope, true, childs) 

首先创建一个没有孩子的的ELEM,然后复制并添加孩子的。复制方法定义没有可变参数。

您的scopeminimizeEmpty参数按错误顺序排列。

尝试调用它像这样(请注意我用的同伴对象也节省了几个字符):

Elem(node.prefix, node.label, node.attributes, node.scope, true, childs) 

更新的问题进行了更新后 - 啊,现在我看到你的问题 - childsSeq[Node],但Elem构造函数方法期望Node*;所以你可以使用:

Elem(node.prefix, node.label, node.attributes, node.scope, true, childs:_*) 
+0

UUUps,从1000个试验中发布了错误的组合。不幸的是,正确的顺序也不起作用。 我编辑原来的帖子来解决“打字错误”。 无论如何谢谢你的答案。 – ZsJoska 2014-10-30 07:57:06

+0

谢谢你的回答 – ZsJoska 2014-10-30 15:47:43