Accessing XML Data Using XPath Expressions

With the parsing out of the way, we can turn to how to access parts of the XML data. The JSTL XML library contains a number of actions for this purpose, similar to the ones you’re familiar with from the JSTL core library: <x:out>, <x:set>, <x:if>, <x:choose>, <x:when>, <x:otherwise>, and <x:forEach>. The main difference between the XML and the core flavor is that the XML actions use a special language for working with XML data, named XPath, instead of the standard JSP EL. XPath 1.0 is a W3C recommendation that has been around since 1999, and it’s used in XSLT stylesheets and other XML applications.[1] The language details are beyond the scope for this book, but here’s a brief summary.

An XPath expression identifies one or more nodes (root, elements, attributes, namespace attributes, comments, text, and processing instructions) in an XML document. The simplest expression type is a plain location path, similar to a Unix filesystem path, to a set of nodes in the document. For instance, the path /meerkat/image/url identifies the <url> element in the Meerkat XML document:

... <meerkat> <title>Meerkat: An Open Wire Service</title> <link>http://meerkat.oreillynet.com</link> <description> Meerkat is a Web-based syndicated content reader providing a simple interface to RSS stories. While maintaining the original association of a story with a channel, Meerkat's focus is on chronological order -- the latest stories float to the top, regardless of their ...

Get JavaServer Pages, 3rd Edition 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.