Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala XML, Get nodes where parent has attribute value match

Tags:

xml

scala

xpath

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>)
like image 378
eptx Avatar asked Sep 27 '11 19:09

eptx


1 Answers

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.

like image 86
Lachlan Avatar answered Nov 03 '22 20:11

Lachlan