jstl, xml and xslt

39
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout

Upload: annona

Post on 08-Jan-2016

105 views

Category:

Documents


4 download

DESCRIPTION

JSTL, XML and XSLT. An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout. JSP Standard Tag Library (JSTL). JSTL is a standardized set of Custom Tags with several implementations http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: JSTL, XML and XSLT

JSTL, XML and XSLT

An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout

Page 2: JSTL, XML and XSLT

JSP Standard Tag Library (JSTL) JSTL is a standardized set of Custom Tags

with several implementations http://jakarta.apache.org/taglibs/doc/standard

-doc/intro.html JSTL is one Tag Library, but it’s functionally

is divided into four parts with its own TLD Core XML Processing I18N Database Access (SQL)

Page 3: JSTL, XML and XSLT

JSTL - Prerequisites

In all examples a couple of objects are supposed to be available customers - A collection of Customer objects intArray - An array of int's stringArra - An array of Strings The Customer Object

int key String lastName String firstName Date birthDate Address address

The Address Object String line1 String line2 String city String state String country

Page 4: JSTL, XML and XSLT

JSTL Core

The Core part of JSTL contain tags the core of JSTL such as iteration, conditional processing and expression language support

< %@ taglib prefix="c" uri="http://java.sun.com/jstl/ea/core" %> at the top of your JSPs

Page 5: JSTL, XML and XSLT

JSTL Core – General tags

<c:set …> is used to declare variables and assign values to them <c:set var=“name” scope=“application|request|

session” value=“val” /> <c:set var=“name” scope=“application|request|

session”/> <bookshop:shoppingCart />

</c:set>

Page 6: JSTL, XML and XSLT

JSTL Core – General tags

<c:out …> is used to print values <c:out value="${customer.lastName}"/> <c:out value="${customer.lastName}“

scope=”session” /> <c:out value="${customer.phoneHome}"

default="no home phone specified"/>

Page 7: JSTL, XML and XSLT

JSTL Core – Conditional tags

Conditional tags are used for execution control <c:if />

<c:if test="${customer.address.country == 'USA'}"> <c:out value="${customer}"/><br> </c:if

<c:chose /> and <c:when /> - a switch/case structure <c:choose>

<c:when test="${customer.address.country == 'USA'}"> <font color="blue"> </c:when>

<c:when test="${customer.address.country == 'Canada'}"> <font color="red"> </c:when>

<c:otherwise> <font color="green"> </c:otherwise> </c:choose>

Page 8: JSTL, XML and XSLT

JSTL Core – iteration tags

Iteration tags are used to loop over some data structure, often a Collection

<c:forEach var=“current” items=“Collection ” begin=“start” end=“10” /> <c:forEach var="customer" items="${customers}">

<c:out value="${customer}"/><br> </c:forEach> <c:forEach var="i" begin="1" end="10">

<c:out value="${i}"/> • </c:forEach>

Page 9: JSTL, XML and XSLT

JSTL Core – iteration tags

<c:forTokens var=“token” items=“a,b,c” delims=“,”> <c:forTokens var="token" items="bleu,blanc,rouge|

vert,jaune|blanc,rouge" delims="|"> <c:out value="${token}"/> •

</c:forTokens> <c:forTokens var="token" items="bleu,blanc,rouge|

vert,jaune|blanc,rouge" delims="|,"> <c:out value="${token}"/> •

</c:forTokens>

Page 10: JSTL, XML and XSLT

JSTL Core – Import tags

There are several tags for importing information from http and ftp (and more)

<c:import url=“url” var=“resultVar”/> <c:import url=“http://developer.mimer.com” />

Will get the content of the URL and print it Relative path is ok

<c:import url="LocalSample.jsp"/> <c:import url=“ftp://ftp.mimer.se/readme_v.txt/”

var=“result”/> Will get the content and store it in the variable resultVar

Page 11: JSTL, XML and XSLT

JSTL Core – Import tags

The <c:param name=“name” value=“value” /> can be combined with <c:import /> to import content from dynamic sites <c:import url=http://

developer.mimer.se/support/support_faq.tml> <c:param name=“category” value=“3” />

</c:import>

Page 12: JSTL, XML and XSLT

JSTL Core – Working with URLs The <c:url /> and <c:param /> is used

together to construct URLs with proper escaping and rewriting

<c:url value=“base.jsp” /> <c:param name=“name" value=“Fredrik Alund"/>

</c:url> Gives base.jsp?name=Fredrik%20Alund Possibly with jsessionid appended

Page 13: JSTL, XML and XSLT

JSTL XML

JSTL contains several tags for working with XML and XSLT But first an introduction to XML and XSLT

Page 14: JSTL, XML and XSLT

XML

XML is an abbreviation for EXtensible Markup Language

A markup language like HTML Not used to generate layouts but to describe

data No tags are defined in XML, just syntax rules

for tags XML uses a DTD (Document Type Definition)

to formally describe the data

Page 15: JSTL, XML and XSLT

XML

XML tags are case sensitive All XML elements must have a closing tag

<a></a> or <a /> All XML elements must be properly nested

<a><b><c></b></c></a> is not valid All XML documents must have a root/start tag <a></a><b></b> is not valid, but

<root><a></a><b></b></root> is

Page 16: JSTL, XML and XSLT

XML

A XML document is well formed if it conforms to the XML syntax rules

A XML document is valid if it is well formed and conform to the rules of a DTD

Page 17: JSTL, XML and XSLT

XML Example 1

<?xml version="1.0“ encoding=“UTF-8” ?>

<person>

<name>Fredrik</name>

<surname>Ålund</name>

<age>32</age>

</person> The first line tells what version of XML is

used and what encoding to use

Page 18: JSTL, XML and XSLT

XML bigger example

<family><person>

<name>Fredrik</name><surname>Ålund</name><age>32</age>

</person><person>

<name>Annika</name><surname>Ålund</name><age>28</age>

</person><person>

<name>Viktor</name><surname>Ålund</name><age>0,5</age>

</person></family>

Page 19: JSTL, XML and XSLT

XML attributes

XML elements can have attributes<employees>

<employee empid=“1”><name>Fredrik Ålund</name><department depid=“3”>Services</department>

</employee><employee empid=“2”>

<name>Helena Larsson</name><department depid=“3”> Services </department>

</employee>

</employees>

Page 20: JSTL, XML and XSLT

XML CDATA

If the value of an element contain binary data or some other illegal characters, CDATA can be used <element><!CDATA[anytext]]></element>

Page 21: JSTL, XML and XSLT

Why XML?

Describe your data Give you a structure of your data Easier integration of systems The same XML document can be

transformed to HTML, WML or VoiceXML to support different clients

Page 22: JSTL, XML and XSLT

XSL and XSLT

XSL is an abbreviation of eXtensible Stylesheet Language

XSLT means XSL Transformation XSL consists of two parts

A method for transforming XML documents A XML document can be transformed into a HTML

document A method for formatting XML documents

Elements can be sorted and formatted in different ways

XSL files are valid XML documents

Page 23: JSTL, XML and XSLT

A XSL example

A XSL to transform our employee list to html might look like<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html> <body> <table border="2" bgcolor="yellow"> <tr> <th>Name</th> <th>Department</th> </tr> <xsl:for-each select=“employees/employee"> <tr> <td><xsl:value-of select=“name"/></td> <td><xsl:value-of select=“department"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

Page 24: JSTL, XML and XSLT

XSL templates

Stylesheets starts with <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

A template matches a part of a XML document and is evaluted <xsl:template match="/"> - matches the root <xsl:template match=“employees"> - matches

employees

Page 25: JSTL, XML and XSLT

XSL <xsl:for-each/>

<xsl:for-each select=“employees/employee"> loops over all employees

<xsl:for-each select=" employees/employee " order-by="+ name"> will order the elements on the name element (+ ascending, - descending)

Page 26: JSTL, XML and XSLT

XSL <xsl:for-each/> and filters Filters can be applied to filter out elements <xsl:for-each

select=“employees/employee[department/@depid = ‘3’“ >

<xsl:for-each select=“employees/employee[department = ‘Services’“ >

Valid filter operations are =  (equal) =! (not equal) &LT& (less than) &GT& (greater than)

Page 27: JSTL, XML and XSLT

XSL <xsl:value-of/>

<xsl:value-of ..> returns the value of an element or attribute

<xsl:value-of select=“name"/> return the value of the name element for the current employee

<xsl:value-of select=“@empid"/> returns the empid attribute of the employee element

Page 28: JSTL, XML and XSLT

XSL IF

The conditional statement if is available in XSL <xsl:if match=".[@empid=‘1']"> </xsl:if>

Page 29: JSTL, XML and XSLT

XSL chose

<xsl:choose>   <xsl:when match=".[@empid=‘3']">      ... some code ...   </xsl:when>   <xsl:otherwise>      ... some code ....   </xsl:otherwise></xsl:choose>

Page 30: JSTL, XML and XSLT

XSL Text

<xsl:text disable-output-escaping=“true/false” /> Used to output text. Useful if the text is not valid

XML since it can be combined with <![CDATA[xxx]]>

<xsl:text disable-output-escaping="yes"> <![CDATA[shop?action=detail&bookid=]]>

</xsl:text>

Page 31: JSTL, XML and XSLT

<xsl:apply-template />

Instead of using for-each, we can use <xsl:apply-templates match=“element”/>

Make <xsl:template match=“xxx” /> for each element

Put <xsl:apply-template where you want the output of the other template

Page 32: JSTL, XML and XSLT

<xsl:element />

In XSL you have to follow XML syntax rules. This makes it hard to construct HTML form elements and HREFs. <xsl:element > can be used to ease this

A HREF link in XSL<xsl:element name="input">

<xsl:attribute name="href"><xsl:text disable-output-escaping="yes">

<![CDATA[ shop?action=detail&bookid=  ]]> </xsl:text><xsl:value-of select="id" />

</xsl:attribute><xsl:text>Detail</xsl:text>

</xsl:element>

Page 33: JSTL, XML and XSLT

<xsl:element>

A HTML Form text input field

<xsl:element name="input"><xsl:attribute name="size">2</xsl:attribute>

 <xsl:attribute name="type">text</xsl:attribute>

 <xsl:attribute name="value">1</xsl:attribute>

 <xsl:attribute name="name">

quantity

</xsl:attribute>

</xsl:element>

Page 34: JSTL, XML and XSLT

The same XSL with apply-template <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match="employees"> <html> <body> <table border="2" bgcolor="yellow"> <tr> <th>Id</th><th>Name</th> <th>Department</th> </tr> <xsl:apply-templates /> </table> </body> </html> </xsl:template>

<xsl:template match="employee"> <tr> <td><xsl:value-of select="@empid"/></td> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="department"/></td> </tr> </xsl:template> </xsl:stylesheet>

Page 35: JSTL, XML and XSLT

JSTL and XML, resumed

JSTL has support for navigating XML with XPath and to do XSLT processing

<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %> is used to specify that the XML part is to be used

Page 36: JSTL, XML and XSLT

JSTL XML - parsing

A XML structure can be parsed into a XML document that can be navigated

<x:parse var="a"> <a> <b> <c> foo </c> </b> <d> bar </d> </a> </x:parse>

<x:out select="$a/a/d"/> The XML is parsed into variable a The value of the element d is selected

Page 37: JSTL, XML and XSLT

JSTL XSL Transformation

<c:set var="xml"> <a><b>header!</b></a>

</c:set> <c:set var="xsl">

<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

version="1.0"> <xsl:template match="text()"> <h1><xsl:value-of select="."/></h1> </xsl:template> </xsl:stylesheet>

</c:set><x:transform xml="${xml}" xslt="${xsl}"/>

Page 38: JSTL, XML and XSLT

JSTL XSL Transformation combined with JSTL Core and Custom Tags Combine the Core import with XSLT to read

the XSL file from disk Get XML output from a Custom tag (or Java

Bean) Do XSL Transformation

Page 39: JSTL, XML and XSLT

JSTL XSL Transformation combined with JSTL Core and Custom Tags<c:set var="booklist_xslt">

<c:import url="booklist_xslt.xsl"/>

</c:set>

<x:transform xslt="${booklist_xslt}">

<jsp:getProperty name="bookList" property="xml"/>

</x:transform>