skhaapioloir
skhaapioloir

Reputation: 31

How to check if NodeSeq contains a particular element

I tried using "contains" function which is supposed to give a boolean if the nodeSeq contains an element, but its not working for me.

For example: seq =

<parent>
  <child1 />
  <child2 />
</parent>

If I use seq.contains("child1"), it gives me false. Am I missing something ?

Upvotes: 0

Views: 679

Answers (1)

Angelo Genovese
Angelo Genovese

Reputation: 3398

NodeSeq seems to contain a single node (you're "parent" tag) which has children. This code seems to work:

nodeSeq.child.contains(<child1 />)

Alternatively you can use xpath to find the children

(nodeSeq \\ "child3").nonEmpty == false
(nodeSeq \\ "child2").nonEmpty == true
(nodeSeq \\ "parent").nonEmpty == true

Upvotes: 1

Related Questions