Searching in XML with XPath

XPath is a W3C-standard query language for selecting nodes from an XML document. That is somewhat equivalent to SQL for databases or regular expressions for text. XPath is a very powerful query language, and it's beyond the scope of this book to delve into the extended XPath capabilities. This recipe will show some basic queries to select nodes and groups of nodes.

Getting ready

Let's start as usual by defining an XML document that we can use for selecting nodes:

def todos = ''' <?xml version="1.0" ?> <todos> <task created="2012-09-24" owner="max"> <title>Buy Milk</title> <priority>3</priority> <location>WalMart</location> <due>2012-09-25</due> <alarm-type>sms</alarm-type> <alert-before>1H</alert-before> </task> <task ...

Get Groovy 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.