This post helps you understand how to use the <x:set> tag in the JSTL XML tags library with code example.

In JSTL, the <x:set> tag is used to save the result of XPath expression into a scoped variable. The type of the result depends on the type of the result of XPath expression. If the result of the XPath expression is boolean, the result will be saved as a java.lang.Boolean, for result of string the result will be saved as java.lang.String and so on.

 

JSTL <x:set> Syntax:

<x:set var="<string>" select="<string>" scope="<string>"/>

 

Attributes:

Name

Required

Description

var

True

Name of the variable to save the result of the XPath expression. The type of this variable depends on the type of the result after the XPath expression is evaluated.

select

False

XPath expression to be evaluated.

scope

False

Scope of the var to store into.

 

JSTL <x:set> Example:

 The following JSP example saves the XPath expression result into variables and displays them using <c:out> tag.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd">
<html> 
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>&lt;x:set&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;x:set&gt; Demo</h1>
    <c:import 
      url="http://localhost:8080/JSTL-Tag-Reference/tag-types/xml/citizens.xml" 
      var="citizenXML"/>
    <x:parse var="doc" xml="${citizenXML}" />
   <x:set var="citizen1firstname" select="$doc/citizens/citizen[1]/firstname/text()"/>
   <c:out value="${citizen1firstname}"/>
   <x:set var="citizen1lastname" select="$doc/citizens/citizen[1]/lastname/text()"/>
   <c:out value="${citizen1lastname}"/>
  </body>
</html>

 

XML Document (given for reference)

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<citizens>
    <citizen>
        <ssn>Z345T</ssn>
        <firstname>Cheryl</firstname>
        <lastname>Johnson</lastname>
        <role>Manager</role>
        <salary>12000</salary>
    </citizen>
    <citizen>
        <ssn>Z446T</ssn>
        <firstname>John</firstname>
        <lastname>Smith</lastname>
        <role>Employee</role>
        <salary>1000</salary>
    </citizen>
    <citizen>
        <ssn>Z335T</ssn>
        <firstname>Justin</firstname>
        <lastname>Claire</lastname>
        <role>Senior Manager</role>
        <salary>14000</salary>
    </citizen>
    <citizen>
        <ssn>Z389T</ssn>
        <firstname>Clark</firstname>
        <lastname>Rick</lastname>
        <role>Employee</role>
        <salary>2000</salary>
    </citizen>
</citizens>

 

Output:

x-set

 

Recommended Usage of <x:set> tag:

Using <x:set>, we can conveniently store the result of the XPath expression into a scoped variable for later use.

 

Other JSTL XML Tags:

forEach  |  if  |  out  |  param  |  parse  |  transform  |  choose, when, otherwise


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment