In this tutorial you will learn some more example about JSTL XML
In this tutorial you will learn some more example about JSTL XML
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> ============== demo4.jsp ======== <%@ page contentType="text/html" %> <html> <x:if
select="$n/author=$s" > <c:if
test="${a!='ok'}" /> =============================================== 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 <select name="combo1"> ----------- demo5.jsp <%@ page contentType="text/html" %> <html> Result ========================- *************************************** In demo6 , we see XSLtransform using
JSTL. ----------------------------------------------- ----------------------------------------------- // students.xml <?xml version="1.0"?> <students> <student> <student> <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="/"> <xsl:for-each select="students/student"> </table> --------------------------------------------- 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. Ads
<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>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/xml" %>
<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" >
<c:set var="a" value="ok" />
<x:out
select="$n/title" />
<br>
<x:out select="$n/author" />
<br>
</x:if>
</x:forEach>
<c:out value="not
yet ready!"
/>
</c:if>
</body>
</html>
<html>
<body>
SELECT THE
TITLE.<BR>
YOU WILL GET TITLE &
AUTHOR.
<form method=post
action="demo5.jsp">
<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>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/xml" %>
<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">
<x:out select="$n/title" />
</x:when>
<x:otherwise>
</x:otherwise>
</x:choose>
<c:if
test="${m!='ok'}">
<c:out
value="no such book"/>
</c:if>
</body>
</html>
--------------------
title sent
from user:
c++
--------------------
c++
robert
title
sent from user:
VB
--------------------
no such
records
<%@ 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}" />
<student>
<name>Thomas</name>
<place>Delhi</place>
<number>1111</number>
<mark>78</mark>
</student>
<name>David</name>
<place>Bombay</place>
<number>4444</number>
<mark>90</mark>
</student>
<name>Mathew</name>
<place>Bangalore</place>
<number>5555</number>
<mark>92</mark>
</student>
<name>John</name>
<place>Hyderabad</place>
<number>6666</number>
<mark>72</mark>
</student>
<html>
<body>
<table border="2"
bgcolor="yellow">
<tr>
<th>Name</th>
<th>Place</th>
<th>Number</th>
<th>Mark</th>
</tr>
<tr>
<td><xsl:value-of
select="name"/> </td>
<td><xsl:value-of
select="mark"/> </td>
</tr>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>