score:2

for anyone with a similar question, i've found the following solution:

def removeduplicates(tree: node): node = {
  var ids = set.empty[string]
  def recurse(node: node): seq[node] = node match {
    case e: elem if (e.label == "leaf") => {
      val id = (e \\ "@id").text
      ids.contains(id) match {
        case true => seq.empty
        case _ => {
          ids = ids + id
          <new-leaf id={id}/>
        }
      }
    }
    case e: elem => e.copy(child = e.nonemptychildren.map(recurse(_).headoption).flatten)
    case _ => node
  }
  recurse(tree).head
}

this works because it handles node traversal manually, not using ruletransformer#transform, and therefore does not iterate over the same node more than once (although it's still stateful, unfortunately).


Related Query

More Query from same tag