@Grab(group='net.sf.saxon', module='saxon', version='8.7') import net.sf.saxon.* import net.sf.saxon.query.* import org.xml.sax.* import javax.xml.transform.sax.* import javax.xml.transform.stream.* // source url:CO2排出量(kt) yearFrom = "2002" yearTo = "2007" countries = "jpn" base = "http://api.worldbank.org/countries/${countries}/indicators/EN.ATM.CO2E.KT" param = "?per_page=1000&date=${yearFrom}:${yearTo}" sourceurl = "${base}${param}" // XQuery String query = ''' declare namespace wb = "http://www.worldbank.org"; for $elem in //wb:data/wb:data order by $elem/wb:date/text() return <result> <name>{$elem/wb:date/text()}</name> <value>{$elem/wb:value/text()}</value> </result> ''' println query // XQueryを実行 conf = new Configuration() sqc = new StaticQueryContext(conf) dqc = new DynamicQueryContext(conf) xq = sqc.compileQuery(query) sqc =xq.getStaticContext() di = sqc.buildDocument(new SAXSource(new InputSource(new URL(sourceurl).newInputStream()))) dqc.setContextNode(di) xq.run(dqc, new StreamResult(System.out), new Properties())動作環境
groovy1.8.2, JDK6 Update27, saxon 8.7
0 件のコメント:
コメントを投稿