JSTL XML Tags

In JSTL, XML tags are used for creating and manipulating xml documents. The following syntax represent the URL of XML tag: -

<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>

To utilize the functionality of XML tags, it is required to download and add two jar files within the lib directory of your project. Following are the required jar files: -

  • XercesImpl.jar
  • xalan.jar

XML Tags

  • <x:out>: - This role of this tag is similar to <%=expression%> tag but for XPath expression.
  • <x:set>: - This tag associate the value with variable for XPath expression.
  • <x:if>: - This tag treats as a conditional statement for XPath expression.
  • <x:choose>: - This tag function as a switch statement for XPath expression.
  • <x:when>: - This tag is used to test the conditions just same as Case in switch statement.
  • <x:otherwise>: - This tag invokes if none of the <x:when> tag condition satisfies. Thus, it treats as default of switch statement.

Example of JSTL XML Tag

This example shows the functionality of various tags of JSTL XML.

index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %> 
<html> 
<head> 
<title>Tutorial and Example</title> 
</head> 
<body> 
<h2>JSTL XML Tag Demo </h2> 
<c:set var="website"> 
<website> 
      <name>Tutorial and Example</name> 
      <tutorial>Java Server Pages</tutorial>
    </website> 
</c:set> 
<x:parse xml="${website}" var="output"/> 
<b>Website :: </b>
<x:out select="$output/website[1]/name" /><br> 
<b>Tutorial ::</b>
<x:out select="$output/website[1]/tutorial" /> 
</body> 
</html>
Output: