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).
Source: stackoverflow.com
Related Query
- Eliminate duplicates, change label with scala.xml.transform.RuleTransformer
- Scala - xml transform throws away transformed elements with multiple attributes
- How do I change intellij idea to compile with scala 2.11?
- Construct XML with dynamic label and attributes in Scala?
- How to change attribute on Scala XML Element
- Scala String interpolation with Format, how to change locale?
- How to use Scala XML with Apache Flink?
- How will xml modularisation in scala 2.11 play with xml literals?
- XML to JSON with Scala
- Join strings with XML node between in scala
- XML equality problem with Scala
- Parse XML using Scala with colon (:) inside XML tag
- Changing XML Namespace with Scala
- substituting xml values programmatically with scala
- Scala - Spark - How to transform a dataframe containing one string column to a DF with columns with the rigth type?
- How can I remove duplicates from a list in Scala with pattern matching?
- Scala XML pattern with whitespace
- Extracting XML elements with specific child element contents with Scala
- How to transform a collection of tuples with scala 2.13
- process a text file with xml column in apache spark scala
- Scala how to retrieve xml tag with optional attribute
- JSON to XML in Scala and dealing with Option() result
- Using Scala pattern matching to extract XML elements with a certain name, regardless of content
- Scala "functional" way to convert sequence with 'bad nesting" to well-formed XML
- Use Scala to create a list with the squares from 0 to count – 1 as lists with duplicates
- Scala - replace xml element with specific text
- scala - how to change field names when decomposing with json4s?
- Parsing xml files with scala
- schema-aware XSLT transform on saxonica 9 EE with scala
- Change fields of case class based on map entries with simple type check in scala macros
More Query from same tag
- Dependency injected controllers in Play with @
- Preconditioning (require) an uninitialized value's property in a Scala trait
- Confuse about scala private field variable
- SQL JOIN to find mismatches
- How to filter a dataframe based on multiple column matches in other dataframes in Spark Scala
- Populate H2 in-mem db from .csv file using scala slick
- Using `map` function on Map in Scala
- Creating html link programmatically in Play Framework
- Sort Array of Int
- Scala Compile Error Inserting into Postgres via Slick
- Reading multiple csv files at different folder depths
- Generic type unification: multiple parameters (T,T) vs. multiple parameter lists (T)(T)?
- empty file creating in root container while writing file into a blob container
- play framework call helper function from another template
- Lagom with Gradle - java.lang.NoClassDefFoundError: scala/Function1$class
- How to Await in Play Framework 2 controller action?
- Caused by: java.time.format.DateTimeParseException: Text '2020-05-12 10:23:45' could not be parsed, unparsed text found at index 10
- how to put a for loop in sleep for a certain interval(scala)
- It would be nice to have Vec[Mem] in Chisel
- scala convert deep quoted arrays to array<array<array<array<double>>
- Read hive struct type and modify value
- Converting column containing Strings to column containing List of Objects in Apache Spark UDF
- Cannot Use Relative Path in Scala.IO.Source
- Why is it that I add ORG.mongodb.smth to dependencies and then import COM.mongodb.smth?
- Type Class Implicits with Context Bound
- Spark: How to delete a specific variable from spark-shell memory namespace?
- Scala - shell commands with pipe
- Await.result or just a simple call?
- What are the available parser(s) for scala programming language?
- How to get spark dataframes from grouped data