Given the following XML elements --
val nodes = List(
<foo/>,
<bar/>,
<baz/>,
<bar>qux</bar>,
<bar quux="corge"/>,
<bar quux="grauply">waldo</bar>,
<bar quux="fred"></bar>
)
-- how do I construct a pattern that matches all <bar/>
s? I've tried, for instance:
nodes flatMap (_ match {
case b @ <bar/> => Some(b)
case _ => None
})
but this matches only the empties.
res17: List[scala.xml.Elem] = List(<bar/>, <bar quux="corge"/>, <bar quux="fred"></bar>)
And if I allow a placeholder for content:
nodes flatMap (_ match {
case b @ <bar>{content}</bar> => Some(b)
case _ => None
})
this matches only the non-empties.
res20: List[scala.xml.Elem] = List(<bar>qux</bar>, <bar quux="grauply">waldo</bar>)
I could of course give up on XML literals and just write
nodes flatMap (_ match {
case e: Elem if e.label == "bar" => Some(e)
case _ => None
})
but it seems like there must be a more clever way.