Selecting using XmlSlurper like a WHERE clause
-
30-06-2021 - |
質問
Given the following HTML snippet, I need to extract the text of the content attribute for the meta
tag with attribute name
equal to description
and the meta
tag with attribute property
equal to og:title
. I've tried what is shown at Groovy: Correct Syntax for XMLSlurper to find elements with a given attribute, but it doesn't seem to work the same in Groovy 1.8.6.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<meta property="fb:admins" content="100003979125336" />
<meta name="description" content="Shiny embossed blue butterfly" />
<meta name="keywords" content="Fancy That Blue Butterfly Platter" />
<meta property="og:title" content="Fancy That Blue Butterfly Platter" />
Is there a clean way to retrieve these with GPath?
解決
This works with groovy 2.0.1 - I don't have 1.8.6 handy at the moment:
def slurper = new XmlSlurper()
File xmlFile = new File('sample.xml')
def xml = slurper.parseText(xmlFile.text)
println 'description = ' + xml.head.children().find{it.name() == 'meta' && it.@name == 'description'}.@content
println 'og:title = ' + xml.head.children().find{it.name() == 'meta' && it.@property == 'og:title'}.@content
所属していません StackOverflow