JSTL XML Tags

PART-3 JSTL & XML-TAGS
Tutorial
Home |
Part
1 | Part 2 | Part
3 | Part 4
----------------------------------------------
In this third part of the tutorial on JSTL, the author
explains the use of xml tags of the JSTL and shows their wonderful simplicity
,ease of use and raw power.
----------------------------------------------
No one can have any second opinion about the elegance of
xml tags in JSTL.If the readers have been following the earlier instalments of
this J2EE series of tutorials, they would have come across JAXP,DOM,SAX ,JDOM
and such terms, and it may have been none too easy to learn. But the xml
tags in JSTL , make XML processing and even Transformation , a cynch! And ,we
now proceed to study them.
Making our study
even easier, many of the xml tags in JSTL ,
are very much similar to the 'core' tags. For example, just like
<c:out>, we have <x:out>.
Similarly,
<x:forEach>, <x:if>,<x:when> etc.
So, if we have understood the syntax of the 'core'; tags,
it will not be difficult to use the 'xml' tags.
All the following examples use the books.xml file.It contains 'elements' like 'title' and
'author'..
books.xml
<?xml version="1.0" ?>
<books>
<book>
<title>cobol</title>
<author>roy</author>
</book>
<book>
<title>java</title>
<author>herbert</author>
</book>
<book>
<title>c++</title>
<author>robert</author>
</book>
<book>
<title>coldfusion</title>
<author>allaire</author>
</book>
<book>
<title>xml unleashed</title>
<author>morrison</author>
</book>
<book>
<title>jrun</title>
<author>allaire</author>
</book>
</books>
-----------------------------------------------
demo1
The following program reads the xml file using 'forEach' tag and displays the title and author of each book..
The syntax:
<x:forEach var="n"
select="$doc/books/book">
is used to select the elements from the xml file.
<x:out select="$n/title" />
is used to print the elements of the xml file. We begin
by
importing the reference to the XML file to be parsed.
<c:import url="books.xml" var="url" />
-----
We have given a symbolic name for this file as
'url'.Next we ask
the program to parse this XML file.The resulting tree is given a symbolic
name as 'doc'.
<x:parse
xml="${url}" var="doc" />
In the next step, we direct the program to select each title and each author in the XPATH
expression $doc/books/book. If
we refer to the xml file , we find that the root element of the document is
'books'. Inside this, we have 'book'.So, XPATH can be thought of as just a file
hierarchy. Just like <c:out, we have <x:out!
demo1.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="c"
>uri="http://java.sun.com/jstl/xml" %>
<html>
<body>
<c:import url="books.xml" var="url" />
<x:parse
xml="${url}" var="doc" />
-----------------------------------------------<br>
<x:forEach
var="n"
select="$doc/books/book">
<x:out
select="$n/title" />
<br>
<x:out
select="$n/author" />
<br>
========
<br>
</x:forEach>
</body>
</html>
Magically, we have parsed a given XML document and extracted information,
without any mention about DOM,SAX and such words., atall!Wonderful!As a famous author
would say, 'anything that makes my job easier, I like!'.
When we execute the 'program', we get the following
result.
-------------------------------------------
(Result for executing demo1.jsp)
==========================
cobol roy ========== java herbert ========== c++ robert ========== coldfusion allaire ========== xml unleashed morrison ========== jrun allaire ==========
***********************************************
The following program (demo2)displays the books and authors
of xml file in
table format.
demo2.jsp
<%@ page contentType="text/html" %>
<%@ taglib
prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib
prefix="c" >uri="http://java.sun.com/jstl/xml" %>
<html>
<body>
<c:import url="books.xml" var="url" />
<x:parse
xml="${url}" var="doc" />
<table border=1>
<th>
<tr>
<td>title</td>
<td>author</td>
</tr>
</th>
<x:forEach
var="n"
select="$doc/books/book">
<td>
<tr> <x:out
select="$n/title" /></tr>
<tr> <x:out
select="$n/author" /></tr>
</td>
</x:forEach>
</table>
</body>
</html>
|
title |
author
|
|
cobol |
roy
|
|
java |
herbert |
|
c++
|
robert |
|
coldfusion |
allaire |
|
xml
unleashed |
morrison |
|
jrun |
allaire |
------------------------------------------------
demo3 deals
with the selection of particular book's author
from the xml file ,when we give the title,
with the help of <x:if> action tag.The title
is choosen from combo box in demo2.htm file and
submitted.We get a display of the selected title and its author.
Think of this as an sql query like
"select * from table1 where
title='jrun'"
--------------------------------------------------
demo3.htm
<html>
<body>
SELECT THE
TITLE.<BR>
YOU WILL GET TITLE &
AUTHOR.
<form method=post
action="demo3.jsp">
<select name="combo1">
<option value="xml unleashed">xml
<option value="c++">c++
<option value="coldfusion">cold fusion
<option value="java">java
<option
value="cobol">cobol
</select>
<input type=submit>
</form>
</body>
</html>
--------------------------
demo3.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/xml" %>
<html>
<body>
<c:import url="books.xml" var="url" />
<x:parse
xml="${url}" var="doc" />
<c:set var="s" value="${param.combo1}"/>
------
<x:forEach
var="n" select="$doc/books/book" >
<x:if
select="$n/title=$s" >
<x:out select="$n/title" />
<br>
<x:out select="$n/author" />
<br>
</x:if>
</x:forEach>
</body>
</html>
-**********************************************
demo4 is a simple
variation on the same theme. In this case, the user selects the author name from
the combo and any books by that author are displayed, due to the code.
It will be noted that 'allaire' has two books
to his credit and so if we choose 'allaire' in the combo,his two books are
displayed.If 'haris' is chosen, we should display the message that it is yet to
be published as there is no such entry in the xml file. But there is no
'if-else' construct and so we improvise.
We have created a variable 'a' and assigned the value 'ok'
to it. If there is no author to match the user's selection, the conditional
block is ignored and 'a' will not be 'ok'.
From this, we conclude that 'the book is not ready'.
----------------------------------
demo4.htm
<html>
<body>
Select name of author & view his books<br>
<form method=post action="demo4.jsp">
<select name="combo1">
<option value="morrison">morrison
<option value="robert">robert
<option value="allaire">allaire
<option value="herbert">herbert
<option value="roy">roy
<option value="haris">haris
</select>
<input type=submit>
</form>
</body>
</html>
==============
demo4.jsp
========
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/xml" %>
<html>
<body>
<c:import
url="books.xml" var="url" />
<x:parse
xml="${url}" var="doc" />
<c:set
var="s"
value="${param.combo1}"/>
<x:forEach var="n" select="$doc/books/book" >
<x:if
select="$n/author=$s" >
<c:set var="a" value="ok" />
<x:out
select="$n/title" />
<br>
<x:out select="$n/author" />
<br>
</x:if>
</x:forEach>
<c:if
test="${a!='ok'}" />
<c:out value="not
yet ready!"
/>
</c:if>
</body>
</html>
===============================================
In
demo5 also, we display the title & author for a given title,
but we now use <x:choose, <x:when logic. This is similar to <c:choose,
<c:when & <c:otherwise logic in the core library.
----------------------------------------------
demo5.htm
<html>
<body>
SELECT THE
TITLE.<BR>
YOU WILL GET TITLE &
AUTHOR.
<form method=post
action="demo5.jsp">
<select name="combo1">
<option value="xml unleashed">xml
<option value="c++">c++
<option value="coldfusion">cold fusion
<option value="java">java
<option
value="cobol">cobol
</select>
<input type=submit>
</form>
</body>
</html>
-----------
demo5.jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/xml" %>
<html>
<body>
<c:import url="books.xml"
var="url" />
<c:set var="s" value="${param.combo1}"
/>
<x:parse xml="${url}"
var="doc" />
<x:forEach var="n"
select="$doc/books/book">
<x:choose>
<x:when select="$n/title=$s">
<c:set var="m" value="ok" />
<x:out select="$n/title" />
</x:when>
<x:otherwise>
</x:otherwise>
</x:choose>
</x:forEach>
<c:if
test="${m!='ok'}">
<c:out
value="no such book"/>
</c:if>
</body>
</html>
Result
-------------------- title sent
from user: c++ -------------------- c++ robert
========================- title
sent from user: VB -------------------- no such
records
***************************************
In demo6 , we see XSLtransform using
JSTL. ( as promised in the earlier tutorial on XSLT in October issue).For the sake of continuity
with the earlier tutorial,we revert back to students.xml and xsl1.xsl, as given in October issue. Just
three lines and DONE!)
-----------------------------------------------
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x"
uri="http://java.sun.com/jstl/xml" %>
<c:import
url="students.xml"
var="url" />
<c:import url="xsl1.xsl"
var="xsl" />
<x:transform xml="${url}" xslt="${xsl}" />
-----------------------------------------------
// students.xml
<?xml version="1.0"?>
<students>
<student>
<name>Thomas</name>
<place>Delhi</place>
<number>1111</number>
<mark>78</mark>
</student>
<student>
<name>David</name>
<place>Bombay</place>
<number>4444</number>
<mark>90</mark>
</student>
<student>
<name>Mathew</name>
<place>Bangalore</place>
<number>5555</number>
<mark>92</mark>
</student>
<student>
<name>John</name>
<place>Hyderabad</place>
<number>6666</number>
<mark>72</mark>
</student>
</students>
-----------------------------------------------
xsl1.xsl
=======
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<table border="2"
bgcolor="yellow">
<tr>
<th>Name</th>
<th>Place</th>
<th>Number</th>
<th>Mark</th>
</tr>
<xsl:for-each select="students/student">
<tr>
<td><xsl:value-of
select="name"/> </td>
<td><xsl:value-of
select="place"/>
</td>
<td><xsl:value-of select="number"/>
</td>
<td><xsl:value-of
select="mark"/> </td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
---------------------------------------------
|
Name
|
Place
|
Number
|
Mark
|
|
Thomas |
Delhi |
1111 |
78 |
|
David |
Bombay |
4444 |
90 |
|
Mathew |
Bangalore
|
5555 |
92 |
|
John |
Hyderabad
|
6666 |
72
|
===============================================
That completes our study of 'xml' tags in JSTL.We now move
ahead to the fourth and final part of the present tutorial, dealing with 'sql'
tags in JSTL.
Tutorial
Home |
Part
1 | Part 2 | Part
3 | Part 4
|
Current Comments
0 comments so far (post your own) View All Comments Latest 10 Comments: