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

You know, the <x:out> tag displays the result of an XPath expression. This tag is the JSTL XML counterpart of the JSP expression <%= %>.

 

JSTL <x:out> Syntax:

<x:out select="<string>" escapeXml="<true|false>"/>

 

Attributes:

Name

Required

Description

select

True

XPath expression to evaluate and display the result.

escapeXml

False

Defaults to true. By default if resulting content has any XML or HTML or any other markup language tags, the content will be shown with raw XML or HTML or markup language (we can see the tags as well). If set to false, the XML or HTML will be evaluated and shown.

 

JSTL <x:out> Example:

The following JSP code reads an XML document and displays the node values using <x: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:out&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;x:out&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}" />
     <table border="1">
       <x:forEach var="citizen" select="$doc/citizens/citizen">            
    <tr>                                        
      <td><x:out select="ssn" /></td>
      <td><x:out select="firstname" /></td>
      <td><x:out select="lastname" /></td>
      <td><b><x:out select="role" /></b></td>
          <td><b><x:out select="salary"/></b></td>                                             
      </tr>        
     </x:forEach>
   </table>
  </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-out



 

Recommended Usage of <x:out> tag:

This is the tag we use to display the result of the XPath expression to the output. If we want the HTML which is part of the result to be evaluated then we can specify escapeXml as false. 

 

Other JSTL XML Tags:

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


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment