Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Combine the power of XPath and JSP tag libraries

Achieve simple data access in JSPs using XPath expressions

  • Print
  • Feedback
Java Server Pages and XML represent natural partners for building Web applications that use heterogeneous data sources. XML's DOM API provides a universal way to represent these diverse data sources. XPath provides a standard and simple syntax to access the DOM. JSP pages execute as Java servlets, typically in the context of a Web server where they generate session and data-dependent response documents. By combining these technologies, along with an XPath custom tag library, it's possible to make a standard and uniform approach to both representing the model data and accessing it.

The XPath tag library provides a framework within which dynamic content represented as a DOM document can be manipulated and inserted into a JSP to:

  • Simplify presentation code in the JSP and make it Java-free
  • Simplify templating or substitution points within the JSP
  • Remove complexity from dynamic data access, yet make it powerful


The XPath tag library combined with JSP further aids efficiency by engendering a clear separation between the role of a page author and a programmer.

This article covers intermediate-to-advanced JSP, XML, and DOM topics, so you may need to do some background reading in Resources to get up to speed.

Note: The XPath custom tag library is still in the proof-of-concept stage, hence it is not available as a standalone tag library. However, you can download the XPath-JSP Test Application WAR file and extract the relevant parts to create your own standalone tag library.

XPath syntax

XPath, a W3C recommendation since November 1999, provides an easy notation for specifying and selecting parts of an XML document. An XML document is a tree of elements with only one route or path from the root node of the tree to any other node of the tree. XPath defines this path.

Let's take an example XML document and some XPath expressions used to locate its parts.

The document below represents a user who has a userid and a password. Moreover, a user can have multiple roles, in this case two: Domain Administrator and Help Desk Administrator:

<user> 
            <userid>someone</userid> 
            <password>somewhere</password> 
            <roles> 
               <role id="admin">Domain Administrator</role> 
               <role id="helpdesk">Help Desk Administrator</role> 
             </roles> 
</user> 


The basic XPath syntax is similar to filesystem addressing. As such, if the path starts with /, then it represents an absolute path to the required element:

/user/userid/text()   select 'someone' 
/user/roles/role      select  all 'role' elements 
//role                select  all 'role' elements [ same as 2.] 
//role[@id='admin']/text()  select  'Domain Administrator' 
/user/roles/role[1]    select the first role element 
                          i.e. 'Domain Administrator' 
/user/roles/role[last()]   select the last role element
                          i.e. 'Help Desk Administrator'


The above examples represent just a sample of XPath's power. For a more complete list of XPath syntax see the XPath tutorial in Resources.

Architecture for a Web application using the XPath tag library

Having seen how XPath expressions can be used on an XML document, let us now discuss the architecture of a Web application using the XPath custom tag library, illustrated in Figure 1.

  • Print
  • Feedback

Resources
  • JavaWorld resources
  • Other important resources