我错过了一些我收集的相当简单的语法.我正在尝试将元素标签重写为其他内容,并保持其他所有内容不变.
object htmlRule extends RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case Elem(prefix, "document", attribs, scope, child@_*) =>
Elem(prefix, "html", attribs, scope, child)
case other => other
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我要求解释两件事:
1)"child @ _*"究竟用简单的英语表示什么?
2)如何捕获"child @ _*"的值并让它直接传递给新元素?目前,我得到以下错误,这是有道理的.
[error] found : Seq[scala.xml.Node]
[error] required: scala.xml.Node
[error] Elem(prefix, "html", attribs, scope, child)
Run Code Online (Sandbox Code Playgroud)
我也不是这样,所以如果有一个更好的方法来简单地更改特定节点的元素名称,那就让我们来吧......
谢谢, -
符号:
case ... bindVar @ patternConstraint ... => /* bindVar is bound to what patternConstraint matched here */
Run Code Online (Sandbox Code Playgroud)
允许您在捕获受约束的整体值(作为bindVar)时匹配值(withinConstraint部分).
在您的特定情况下,child @ _*中,_表示"不关心",该*办法值的序列,并child @表示结合孩子的整个序列.
经常使用此功能是嵌套模式(通常提到案例类):
case expr @ Expr(op, lhs, rhs) => // Do stuff with expr, op, lhs and rhs
Run Code Online (Sandbox Code Playgroud)
在这里,测试匹配的目标以查看它是否是一个实例,Expr如果是,op则绑定到Expr运算符,lhs并且rhs绑定到它的左侧和右侧.并expr绑定到Expr实例本身.