programming4us
programming4us
WEBSITE

Java EE 6 with GlassFish 3 Application Server : JSP Standard Tag Library - XML JSTL tag library

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
The XML JSTL tag library provides an easy way to parse XML documents and to do Extensible Stylesheet Language Transformations (XSLT). This tag library uses XPath expressions to navigate through elements in an XML document.

XPath is an expression language used for finding information in an XML document, or for making calculations based on the content of an XML document. For more information about XPath, refer to http://www.w3.org/TR/xpath.


The following example illustrates the most commonly used tags in the XML JSTL tag library:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:import url="customers.xml" var="xml" />
<x:parse doc="${xml}" var="doc" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>XML Tag Demo</title>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Email</td>
</tr>
<x:forEach select="$doc/customers/customer">
<tr>
<td>
<x:out select="firstName" />
</td>
<td>
<x:out select="lastName" />
</td>
<td>
<x:choose>
<x:when select="email">
<x:out select="email" />
</x:when>
<x:otherwise>
<c:out value="N/A" />
</x:otherwise>
</x:choose>
</td>
</tr>
</x:forEach>
</table>
</body>
</html>



					  

The first thing we should notice in this example is the use of the<c:import> core JSTL tag to import an XML file from a URL. The value of the URL attribute defines the URL where the XML file can be located; it can be a relative or absolute URL. In the example, the customers.xml file is in the same directory as the JSP, therefore a relative path is used to obtain it. The customers.xml file has customer information including first name, last name, and email, as shown next:

<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer>
<firstName>Karl</firstName>
<lastName>Smith</lastName>
<email>[email protected]</email>
</customer>
<customer>
<firstName>Jenny</firstName>
<lastName>Conte</lastName>
<email>[email protected]</email>
</customer>
<customer>
<firstName>Rhonda</firstName>
<lastName>Benedict</lastName>
</customer>
</customers>

After packaging the previous two files in a WAR file and visiting the JSP's URL, we should see a page like the following:

The first JSTL XML tag we see in the example is the<x:parse> tag. This tag parses an XML document and stores it in the variable defined by its var attribute. The XML document to parse is defined in its doc attribute.

The XML JSTL tag library contains several tags that are analogous to similar tags in the Core JSTL tag libraries. These tags include<x:if>, <x:choose>, <x:when>, <x:otherwise>, <x:forEach>, <x:param>, and<x:set>. Usage of these tags is very similar to their core tag counterparts. The main difference is that these tags contain a select attribute containing an XPath expression to evaluate, instead of the value attribute that the corresponding core tags contain. The example illustrates the usage of most of these tags.

The next JSTL XML tag we see in the example is the<x:forEach> tag. This tag iterates over the elements of an XML document. Elements to iterate over are specified as an XPath expression through the select attribute.

The next JSTL XML tag we see in the example is the<x:out> tag, which outputs the value of the XPath expression defined in its select attribute.

Next, we see the<x:choose> tag, which is the parent tag of the<x:when> and (optionally)<x:otherwise> tags. The body of the first nested<x:when> tag containing an XPath expression evaluating to true as its select attribute is executed. select expressions for subsequent<x:when> attributes are not evaluated after one of them evaluates to true. If no select attributes for any of the<x:when> tags evaluate to true, the body of the optional<x:otherwise> tag is executed.

An additional XML JSTL tag is the<x:transform> tag, which is used to do XSLT transformations on XML documents. This tag is typically used with two attributes. The xml attribute indicates the location of the XML document to transform. It can be imported via the<c:import> tag, as illustrated in the example. The xslt attribute indicates the XSL stylesheet used to transform the document. This stylesheet can also be imported via the<c:import> tag.

The following table lists all of the JSTL XML tags:

Tag Description Example
<x:choose> Used to wrap the<x:when> and (optionally)<x:otherwise> tags. The body of the first<x:when> tag containing a select expression that evaluates to true is executed. If none of the<x:when> tags contain a test expression that evaluates to true, then the body of the<x:otherwise> tag is executed. See example for<x:forEach>.
<x:forEach> Iterates over the elements of an XML document. The elements to iterate over are specified through the select attribute. <x:forEach select= "$doc/customers/customer">

<tr>

<td>

<x:out select="firstName" />

</td>

<td>

<x:out select="lastName" />

</td>

<td>

<x:choose>

<x:when select="email">

<x:out select="email" />

</x:when>

<x:otherwise>

<c:out value="N/A" />

</x:otherwise>

</x:choose>

</td>

</tr>

</x:forEach>
<x:otherwise> Its body gets executed if none of the test expressions in the<x:when> tags nested in the same<x:choose> tag evaluate to true. See example for<x:forEach>.
<x:out> Outputs an XPath expression defined by the select attribute. See example for<x:forEach>.
<x:param> Adds a parameter to the containing<x:transform> tag. See example for<x:transform>.
<x:parse> Parses an XML document and stores it in the variable defined by its var attribute. <x:parse doc="${xml}" var="doc" />
<x:set> Saves the result of the XPath expression defined in its select attribute into a variable in the specified scope. If no scope is defined, a default scope of page is used. <x:set var="custEmail" select="email"/>
<x:transform> Transforms the XML document defined by the xml attribute using the XSL stylesheet defined by the xslt attribute. <x:transform xml="${someXmlDoc}" xslt="${xslt}">

<x:param name="paramName" value="${paramValue}"/>

</x:transform>
<x:when> Its body gets executed when its select expression evaluates to true. See example for<x:forEach>.
Other  
 
Top 10
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us