Any way to simplify the following? or reduce the boilerplate code with another function?
scala> val ns = <foo><bar id="1"><tag>one</tag><tag>uno</tag></bar><bar id="2"><tag>two</tag><tag>dos</tag></bar></foo>
ns: scala.xml.Elem = <foo><bar id="1"><tag>one</tag><tag>uno</tag></bar><bar id="2"><tag>two</tag><tag>dos</tag></bar></foo>
scala> (ns \\ "bar" filterNot{_ \\ "@id" find { _.text == "1" } isEmpty}) \\ "tag"
res0: scala.xml.NodeSeq = NodeSeq(<tag>one</tag>, <tag>uno</tag>)
I could only find a minor improvement, the find
/isEmpty
test can be replaced with exists
:
(ns \\ "bar" filter { _ \\ "@id" exists (_.text == "1") }) \\ "tag"
Edit after clarifying comment:
That's a really nice idea! Try this for size:
import xml._
implicit def richNodeSeq(ns: NodeSeq) = new {
def \@(attribMatch: (String, String => Boolean)): NodeSeq =
ns filter { _ \\ ("@" + attribMatch._1) exists (s => attribMatch._2(s.text)) }
}
ns \\ "bar" \@ ("id", _ == "1") \\ "tag"
I used a predicate instead of hard-coding the attribute value comparison.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With