Extracting XML elements with specific child element contents with Scala
scala, xml
Solution
(fruits \\ "fruit").filter(x => // filter the sequence of fruits
(x \\ "name") // find name nodes
.flatMap(_.child.map(_.text)) // get all name node text values
.contains("banana")) // see which name nodes contain "banana"
Returns the `NodeSeq`:
<fruit>
<name>banana</name>
<taste>yellow</taste>
</fruit>
<fruit>
<name>banana</name>
<taste>green</taste>
</fruit>
Problem
For a XML snippet like this: ``` val fruits = <fruits> <fruit> <name>apple</name> <taste>red</taste> </fruit> <fruit> <name>banana</name> <taste>yellow</taste> </fruit> <fruit> <name>banana</name> <taste>green</taste> </fruit> <fruit> <name>apple</name> <taste>green</taste> </fruit> </fruits> ``` doing something like: ``` fruits \\ "fruit" ``` will return a sequence of type `scala.xml.NodeSeq` with all the fruits and sub nodes inside. How can I limit this sequence to contain only the fruit elements with 'banana' inside. ie, I want the result to be: ``` <fruits> <fruit> <name>banana</name> <taste>yellow</taste> </fruit> <fruit> <name>banana</name> <taste>green</taste> </fruit> <fruits> ```