Search code examples
scalalift

select NodeSeq by it's id (IdSelector?)


How do I select a NodeSeq by it's id?

In lift, there are "CssSelectors" that allow you to do something like "#myId" #> function, and the function will receive only the desired NodeSeq as input.

But how do I use this fine "css selector" to select nodes, not only apply a function straight afterwards?

I know it can by done by searching all "id" attributes in NodeSeq, but isn't it more CPU-consuming than the Lift's version? Any thoughts or suggestions?


Solution

  • Use the ^^ selector (it ignores the right side of #>):

    scala> import net.liftweb.util.Helpers._
    import net.liftweb.util.Helpers._
    
    scala> "#id ^^" #> "" apply <test><el id="id"/></test>
    res0: scala.xml.NodeSeq = NodeSeq(<el id="id"/>)
    

    As for performance, obviously Lift has to iterate over the id attributes itself.