How to find element by attribute value in GPath?

后端 未结 4 1400
故里飘歌
故里飘歌 2021-02-18 18:35

What is an alternative to this XPath //div[@id=\'foo\'] in GPath? In general, where I can find this documentation?

4条回答
  •  不思量自难忘°
    2021-02-18 18:57

    To mimic the expression //div[@id='foo'] the closest thing you can do with a GPath is:

    def xml = new XmlParser().parseText(text)
    xml.'**'.div.findAll { it.@id=="foo" }
    

    the '**' is pretty much the same as '//' in your XPath.

    xml.'**'.div
    

    will yield all the nodes of type div at any level.

    Later filtering with findAll() with the given closure you get a list of nodes as you do in the XPath case

提交回复
热议问题