Björn Jacobs
Björn Jacobs

Reputation: 4262

Lift: How to select a Node in a NodeSeq by attribute and value?

I have an NodeSeq object and want to select a given node which is marked with an attribute. For example let's say there is a tag <div id="content">...</div> within the NodeSeq.

I tried to select in with the method \\ on NodeSeq as well as with filter.

Let's say seq is my NodeSeq object.

seq \\ "div" works, but this selects all <div> elements.

seq.filter(_.attribute("id").equals("content")) doesn't select any node at all, the resulting list is empty.

How can I select this node?

Upvotes: 1

Views: 1798

Answers (2)

macemers
macemers

Reputation: 2222

Alternatively, you could try the following:

seq.filter(_.attribute("id").exists(_.text.equals("content")))

Upvotes: 0

Alex
Alex

Reputation: 9448

try

scala> var x= <b>
 | <h id="bla"/>
 | <h id="blub"/>
 | </b>

x \\ "h" filter (h=>(h \ "@id" toString) == "bla")

This should work.

Upvotes: 2

Related Questions