给定一个具有以下结构的 HTML 文件 html -> body -> 一堆 div 查找所有具有非空白标签属性的 div 的正确 groovy 语句是什么?

以下内容不起作用:

def nodes = html.body.div.findAll { it.@tags != null }

因为它找到了所有节点。

有帮助吗?

解决方案

尝试以下操作(Groovy 1.5.6):

def doc = """
<html>
    <body>
        <div tags="1">test1</div>
        <div>test2</div>
        <div tags="">test3</div>
        <div tags="4">test4</div>
    </body>
</html>
"""

def html = new XmlSlurper().parseText( doc)

html.body.div.findAll { it.@tags.text()}.each { div ->
    println div.text()
}

这输出:

test1
test4
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top