10.5 Using Scoped Variables in XPath Expressions

   

10.5 Using Scoped Variables in XPath Expressions

As evidenced by the JSP page listed in Listing 10.2, you can access JSP scoped variables in XPath expressions as long as the Java type of those variables matches an expected XPath type. XPath data types and their corresponding Java types are listed in Table 10.7.

Table 10.7. XPath Data Types and Corresponding Java Types

XPath Type

Java Type

boolean

java.lang.Boolean

node-set

Implementation-specific, including the type of object exported by <x:parse>

number

java.lang.Number

string

java.lang.String

You can use scoped variables in XPath expressions as long as those scoped variables reference an object whose Java type is listed in Table 10.7. Furthermore, you can only use scoped variables where XPath expects a variable of the corresponding XPath type ( boolean , number , string , or node-set ). For example, you can do this ”

 <c:import var='rolodex_xml' url='rolodex.xml'/>  <x:parse var='  document  ' xml='${rolodex_xml}'/> <x:out select='count(  $document  //contact)'/> 

”but you can't do this:

 <c:import var='rolodex_xml' url='rolodex.xml'/>  <x:parse var='document' xml='${rolodex_xml}'/> <c:set var='  doc  ' value='document'/> <x:out select='count(  $doc  //contact)'/> 

The preceding code won't work because the doc scoped variable references a string ; for the preceding <x:out> select attribute, XPath expects $doc to reference a node-set . However, you could do this:

 <c:import var='rolodex_xml' url='rolodex.xml'/>  <x:parse var='document' xml='${rolodex_xml}'/> <c:set var='  doc  ' value='  ${  document  }  '/> <x:out select='count(  $doc  //contact)'/> 

The preceding code works because the doc scoped variable references a node-set , not a string . Realize that the preceding code, which adds a level of indirection to access the document scoped variable, does not provide any advantage over the initial code fragment discussed above. The preceding code is presented for illustration.

Table 10.8 shows the different types of expressions that you can use in XPath expressions to access cookies, request parameters and headers, context initialization parameters, and JSP scoped variables.

Table 10.8. XPath Variable Bindings

Expression

Interpretation

$identifier

pageContext.findAttribute("identifier")

$cookie:identifier

Returns the value of the cookie named identifier

$header:identifier

request.getHeader("identifier")

$initParam:identifier

application.getInitParameter("identifier")

$param:identifier

request.getParameter("identifier")

$pageScope:identifier

pageContext.getAttribute("identifier", PageContext.PAGE_SCOPE)

$requestScope:identifier

pageContext.getAttribute("identifier", PageContext.REQUEST_SCOPE)

$sessionScope:identifier

pageContext.getAttribute("identifier", PageContext.SESSION_SCOPE)

$applicationScope:identifier

pageContext.getAttribute("identifier", PageContext.APPLICATION_SCOPE)

So how does the syntax listed in Table 10.8 differ from the syntax used to access the same data with EL expressions, as listed in Table 2.5 on page 64? Here's an example:

 <%-- Store a string in request scope --%>  <c:set value='hello' var='greeting' scope='request'/> <%-- Access the greeting scoped variable with EL expressions --%> <c:out value='  ${greeting}  '/><br> <c:out value='  ${requestScope.greeting}  '/><p> <%-- Access the greeting scoped variable with XPath expressions --%> <x:out select='  $greeting  '/><br> <x:out select='  $requestScope:greeting  '/> 

The preceding code uses <c:set> to store a string in a scoped variable in request scope. Subsequently, that code accesses the scoped variable with <c:out> and <x:out> actions. The first <c:out> action and the first <x:out> action access the greeting scoped variable without specifying its scope, whereas the second <c:out> action and the second <x:out> action specify that scope with the requestScope identifier. The output of the preceding code fragment looks like this:

 hello  hello hello hello 

Realize that the expression specified for the <x:out> action's select attribute must be a valid XPath expression; for example, the following code fragment is valid ”

 <c:set value='hello' var='greeting' scope='request'/>  <c:out value='  ${Greeting: requestScope.greeting}  '/> 

”because the expression specified for the <c:out> action's value attribute is a valid EL expression, but the following code fragment will throw an exception because Greeting: $requestScope:greeting is not a valid XPath expression ”

 <c:set value='hello' var='greeting' scope='request'/>  <x:out select='  Greeting: $requestScope:greeting  '/> 

However, you can do this ”

 <c:set value='hello' var='greeting' scope='request'/>  <x:out select='  string-length($requestScope:greeting)  '/> 

”because the value specified for the <x:out> select attribute is a valid XPath expression.

The Web application shown in Figure 10-3 illustrates how you can use request parameters in an XPath expression. That application consists of two JSP pages, one that lets you enter an area code, and another that displays all contacts in the Rolodex XML file that have work phone numbers with the specified area code.

Figure 10-3. Accessing Request Parameters in XPath Expressions

graphics/10fig03.jpg

The JSP page shown in the top picture in Figure 10-3 is listed in Listing 10.3.

The preceding JSP page contains a simple form that lets you select an area code. The action for the form is show_contacts.jsp , which is listed in Listing 10.4.

The preceding JSP page iterates over all the contacts in the Rolodex XML file and displays information associated with contacts whose work phone number corresponds to the selected area code. Those contacts are determined with an <x:if> action whose select attribute is starts-with (phone[@type="work"], $param:areaCode) . That XPath expression, which uses the XPath starts-with function listed in Table 10.4 on page 431, evaluates to true if the contact's phone number starts with the specified area code. The JSP page accesses that area code with the syntax for request parameters listed in Table 10.8 on page 438.

Listing 10.3 index.jsp (Using Scoped Variables with XPath)
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>    <head>       <title>Using Scoped Variables with XPath</title>    </head>    <body>       <%-- A form that lets the user select an area code --%>       <form action='show_contacts.jsp'>          Select an area code:          <select name='areaCode'>             <option value='915'>915</option>             <option value='716'>716</option>          </select>          <p><input type='submit' value='Show Contacts'/>       </form>    </body> </html> 
Listing 10.4 show_contacts.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>    <head>       <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>       <%@ taglib uri='http://java.sun.com/jstl/xml'  prefix='x' %>       <title>Using Scoped Variables with XPath</title>    </head>    <body>       <%-- Import the phone book XML file --%>       <c:import var='rolodex_xml' url='rolodex.xml'/>       <%-- Parse the phone book XML file --%>       <x:parse var='document' xml='${rolodex_xml}'/>       <%-- For each address --%>       <x:forEach select='$document//contact'>          <%-- If this contact's work phone starts with the               specified area code, show contact information --%>          <x:if  select='starts-with(phone[@type="work"],$param:areaCode)'  >             <table>                <tr>                   <td>First Name:</td>                    <td><x:out select='firstName'/></td>                </tr>                <tr>                   <td>Last Name:</td>                    <td><x:out select='lastName'/></td>                </tr>                <tr>                   <td>Email:</td>                    <td><x:out select='email'/></td>                </tr>                <tr>                   <td>Work Phone:</td>                    <td><x:out select='phone[@type="work"]'/></td>                </tr>                <%-- Home phone number is optional, so we check                     to see if it exists before creating a table                     row --%>                <x:if select='phone[@type="home"]'>                   <tr>                      <td>Home Phone:</td>                      <td><x:out select='phone[@type="home"]'/></td>                   </tr>                </x:if>             </table><br>          </x:if>       </x:forEach>    </body> </html> 

Now that we've seen how to parse XML, access the contents of the parsed XML, and access scoped variables and JSTL implicit objects in XPath expressions, let's move on to transforming XML with XSLT.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net