Search code examples
scalaselectattributesliftnodes

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?


Solution

  • try

    scala> var x= <b>
     | <h id="bla"/>
     | <h id="blub"/>
     | </b>
    
    x \\ "h" filter (h=>(h \ "@id" toString) == "bla")
    

    This should work.