JSP & JSTL: Setting a Variable to the Content of an XPath

I had a problem in JSP and JSTL where I could not get a core variable to contain the content of an XML XPath expression.

Following a combination of reading a book, searching the Internet and speaking to a colleague, I found two solutions to my problem, but unfortunately not an answer to the question of why my original code does not work.

Here is a code: fragment

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>

  <title>JSTL / JSP test</title>


<%--c:import url="/xml_files/document.xml" var="url"/>
<x:parse xml="${url}" var="doc" scope="page" /--%>

<x:parse var="doc" scope="page">
      <element_name>the element value</element_name>

    <td>This doesn't work; why?</td>
    <x:set var="element_value1" scope="page"
    <td><c:out value="${element_value1}"/></td>
    <td>This does work</td>
    <x:set var="element_node2" scope="page"
    <x:set var="element_value2" scope="page"
    <td><c:out value="${element_value2}"/></td>
    <td>This works too</td>
    <c:set var="element_value3" scope="page">
      <x:out select="$doc/root_element/more_elements/element_name"/>
    <td><c:out value="${element_value3}"/></td>


This was run on Apache Tomcat 6.0.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: