scala - Filtering XML elements with null attributes -


i'm trying extract attributes regular xml structure; seems natural first exclude elements particular attribute missing.

i don't know why following doesn't work (see answer why ever got idea 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 middle element still there after filter?

i've confirmed it's not operator precedence:

test.child.filter(x => (x \ "@attr") != null).map(_ \ "@attr") // arraybuffer(1, nodeseq(), 2) 

alternatively (assuming optimized internally), how exclude nodeseq() elements after map step?

just figured out. filter wasn't return null, nodeseq(), following works:

test.child.filter(_ \ "@attr" != scala.xml.nodeseq.empty).map(_ \ "@attr") // arraybuffer(1, 2) 

followed this q&a discover how create nodeseq() object hand


i discovered problem derived crossing own wires. had been using following:

test.child.map(_.attributes("attr")) // arraybuffer(1, null, 2) 

which got idea test vs. null originally. of course, if had stuck that, initial approach have worked:

test.child.filter(_.attributes("attr") != null).map(_ \ "@attr") // arraybuffer(1, 2) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -