I'm trying to extract attributes from a regular XML structure; it seems natural to first exclude the elements for which a particular attribute is missing.
I don't know why the following doesn't work (see answer for why I ever got the idea to test vs. null
):
val test = <top><el attr="1"></el><el></el><el attr="2"></el></top>
test.child.filter(_ \ "@attr" != null).map(_ \ "@attr")
// ArrayBuffer(1, NodeSeq(), 2)
Why is the middle element still there after the filter
?
I've confirmed it's not operator precedence:
test.child.filter(x => (x \ "@attr") != null).map(_ \ "@attr")
// ArrayBuffer(1, NodeSeq(), 2)
Alternatively (assuming this is optimized internally), how could I exclude the NodeSeq()
elements after the map
step?