Describing the JSTL XML Tag


JSTL XML TAG:

In this part of the tutorial on JSTL, I explain the use of the XML tags of JSTL and show their wonderful simplicity, ease of use and raw power. The XML tag library is used to work with XML data used in JSP pages. The XML tag library helps parse and transform the data used in a JSP page. The XML tags can be used with the JSP pages after importing the following tag library by using the following code:

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

XML tags are accessed by using the prefix 'x'. It is a predefined prefix used for XML tag libraries.

Types of XML Tags:

XML Core Tags:
The Core tags include the tags for parsing XML documents and performing X path operations. The XML Core tags are of three types:

  • The<x:parse> Tag: It parses the given XML document and stores the resulted XML document object model into the specified variable. The syntax for it is:
    <x:parse attributes>[body content]</x:parse>
  • The<x:out> Tag: The <x:out> tag evaluates an XML xpath expression and writes the result of the evaluation to the current JSP writer. The syntax for it is:
    <x:out attributes/>
  • The <x:set> Tag: The <x:set> tag evaluates an XML xpath expression and stores the result of the evaluation in  a scoped variable. The syntax for it is:
    <x:set attributes/>


XML Flow Control Tags:
The flow control tags help perform various operations such as easily parse and access XML data , iterate over elements in an XML document, and conditionally process JSP code fragments depending on the result of the given XPath expression.
The Xml Flow Control tags are of five types:

  • The<x:if> Tag: The <x:if> Tag evaluates an xPath expression that is given in its 'select' attribute. If the resulting value is true, then the body of the <x:if> tag is evaluated; othervise not. The syntax for it is:
    <x:if attribue> body content</x:if>
  • The<x:choose> Tag: The <x:choose> tag acts similar to the java switch statement. The <x:choose> tag encloses  one or more <x:when> tags and a <x:otherwise> tag. The syntax for it is:
    <x:choose>body content<x:choose>
  • The<x:when> Tag: It encloses a single case within the <x:choose> tag.The syntax for it is:
    <x:when> body content</x:when>
  • The<x:otherwise> Tag: It is same as <x:when> but it is unconditional; this is equivalent to default case in the java switch statement.The syntax for it is:
    <x:otherwise> body content</x:otherwise>
  • The <x:forEach> Tag: the <x:forEach> tag is used for looping over a list of elements obtained after evaluating the given XPath expression.  The syntax for it is:
    <x:forEach> body content</x:forEach>

XML Transformation Tags: This tag provide support to transform the XML document with the given XSL stylesheet. This tag contain two tags:

  • <x:Transform> Tag: This tag transforms an XML document using the given XSL style sheet. The syntax for it is:
    <x:transform attributes>body content</x:transform>
  • <x:param> Tag: It is used to set transformation parameters. The<x:param> tag should be used within the body of the <x:transform> tag. The syntax for it is:
    <x:param >body content</x:param>

Working With JSTL XML Tag: JSTL is mainly used for the web application, here i'll given a example in which i used JSTL Tags.

Create Table: First we create a table of Employee in which we take four fields EMPNO, DEPTNO, ENAME, SALORY. After this we create a DSN of name jstlxmltag.

Table:

table.gif

Create DSN:

dsn.gif
 

Create GetEmDetails.jsp Page:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<c:if test="${!empty param.language}" var="lang_flag">
<fmt:setLocale value="${param.language}"/>
</c:if>
<c:if test="${!empty param.country && lang_flag}">
<fmt:setLocale value="${param.language}_${param.country}"/>
</c:if>
<sql:setDataSource var="dataSource" driver="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:jstlxmltag" scope="request"/>

<sql:query sql="select EMPNO,DEPTNO,ENAME, SAL from jstlxml" var="result" scope="page"
dataSource="${requestScope.dataSource}"/>
<html>
<body bgcolor="cyan">
Display Currency and Date in:<pre>
<a href="GetEmpDetails.jsp?language=en&country=US">English(US)</a>
<a href="GetEmpDetails.jsp?language=en&country=GB">English(UK)</a>
<a href="GetEmpDetails.jsp?language=en&country=AU">English(AU)</a>
</pre>
<table border="2">
<tr>
<c:forEach items="${pageScope.result.columnNames}" var="colname">
<th><c:out value="${colname}"/></th>
</c:forEach>
<th>&nbsp;</th>
</tr>
<c:forEach items="${pageScope.result.rows}" var="rows">
<tr><td>

<c:out value="${rows.EMPNO}"/>
</td><td>
<c:out value="${rows.DEPTNO}"/>
</td><td>
<c:out value="${rows.ENAME}"/>
</td><td>
<fmt:formatNumber value="${rows.SAL}" type="currency" />
</td></tr>
</c:forEach>
</table>

</body>
</html>

Build.xml File:

<?xml version="1.0" encoding="UTF-8"?>
<project name="JstlXmlTag" default="default" basedir=".">
<description>Builds, tests, and runs the project JstlXmlTag.</description>
<import file="nbproject/build-impl.xml"/>
</project>

Running the application:

Run the tomcat then write the below link in the URL
http://localhost:8081/jsp/

Here jsp is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

OUTPUT:

ENGLISH (us):

eng_us.gif

English (uk):

enguk.gif

English (Au):

engau.gif

These are some another articles links related to this topic:

Starting with JSTL: http://www.c-sharpcorner.com/UploadFile/0d4935/started-with-jsp-standard-tag-library/


Similar Articles