JSTL XML Tags Part 2

In this tutorial you will learn some more example about JSTL XML

JSTL XML Tags Part 2

JSTL XML Tag Part 2

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.