JSTL, XML and XSLT - PowerPoint PPT Presentation

About This Presentation
Title:

JSTL, XML and XSLT

Description:

XML attributes. XML elements can have attributes employees employee empid='1' ... xsl:attribute /xsl:element The same XSL with apply-template ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 40
Provided by: fredri9
Category:
Tags: jstl | xml | xslt | attribute

less

Transcript and Presenter's Notes

Title: JSTL, XML and XSLT


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

2
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 its functionally is
    divided into four parts with its own TLD
  • Core
  • XML Processing
  • I18N
  • Database Access (SQL)

3
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

4
JSTL Core
  • The Core part of JSTL contain tags the core of
    JSTL such as iteration, conditional processing
    and expression language support
  • lt _at_ taglib prefix"c" uri"http//java.sun.com/js
    tl/ea/core" gt at the top of your JSPs

5
JSTL Core General tags
  • ltcset gt is used to declare variables and assign
    values to them
  • ltcset varname scopeapplicationrequestsessi
    on valueval /gt
  • ltcset varname scopeapplicationrequestsessi
    on/gt
  • ltbookshopshoppingCart /gt
  • lt/csetgt

6
JSTL Core General tags
  • ltcout gt is used to print values
  • ltcout value"customer.lastName"/gt
  • ltcout value"customer.lastName
    scopesession /gt
  • ltcout value"customer.phoneHome" default"no
    home phone specified"/gt

7
JSTL Core Conditional tags
  • Conditional tags are used for execution control
  • ltcif /gt
  • ltcif test"customer.address.country
    'USA'"gt ltcout value"customer"/gtltbrgt lt/cif
  • ltcchose /gt and ltcwhen /gt - a switch/case
    structure
  • ltcchoosegt
  • ltcwhen test"customer.address.country
    'USA'"gt ltfont color"blue"gt lt/cwhengt
  • ltcwhen test"customer.address.country
    'Canada'"gt ltfont color"red"gt lt/cwhengt
  • ltcotherwisegt ltfont color"green"gt lt/cotherwisegt
  • lt/cchoosegt

8
JSTL Core iteration tags
  • Iteration tags are used to loop over some data
    structure, often a Collection
  • ltcforEach varcurrent itemsCollection
    beginstart end10 /gt
  • ltcforEach var"customer" items"customers"gt
  • ltcout value"customer"/gtltbrgt
  • lt/cforEachgt
  • ltcforEach var"i" begin"1" end"10"gt
  • ltcout value"i"/gt
  • lt/cforEachgt

9
JSTL Core iteration tags
  • ltcforTokens vartoken itemsa,b,c
    delims,gt
  • ltcforTokens var"token" items"bleu,blanc,rougev
    ert,jauneblanc,rouge" delims""gt
  • ltcout value"token"/gt
  • lt/cforTokensgt
  • ltcforTokens var"token" items"bleu,blanc,rougev
    ert,jauneblanc,rouge" delims","gt
  • ltcout value"token"/gt
  • lt/cforTokensgt

10
JSTL Core Import tags
  • There are several tags for importing information
    from http and ftp (and more)
  • ltcimport urlurl varresultVar/gt
  • ltcimport urlhttp//developer.mimer.com /gt
  • Will get the content of the URL and print it
  • Relative path is ok
  • ltcimport url"LocalSample.jsp"/gt
  • ltcimport urlftp//ftp.mimer.se/readme_v.txt/
    varresult/gt
  • Will get the content and store it in the variable
    resultVar

11
JSTL Core Import tags
  • The ltcparam namename valuevalue /gt can be
    combined with ltcimport /gt to import content from
    dynamic sites
  • ltcimport urlhttp//developer.mimer.se/support/su
    pport_faq.tmlgt
  • ltcparam namecategory value3 /gt
  • lt/cimportgt

12
JSTL Core Working with URLs
  • The ltcurl /gt and ltcparam /gt is used together to
    construct URLs with proper escaping and rewriting
  • ltcurl valuebase.jsp /gt
  • ltcparam namename" valueFredrik Alund"/gt
  • lt/curlgt
  • Gives base.jsp?nameFredrik20Alund
  • Possibly with jsessionid appended

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

14
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

15
XML
  • XML tags are case sensitive
  • All XML elements must have a closing tag
  • ltagtlt/agt or lta /gt
  • All XML elements must be properly nested
  • ltagtltbgtltcgtlt/bgtlt/cgtlt/agt is not valid
  • All XML documents must have a root/start tag
  • ltagtlt/agtltbgtlt/bgt is not valid, but
    ltrootgtltagtlt/agtltbgtlt/bgtlt/rootgt is

16
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

17
XML Example 1
  • lt?xml version"1.0 encodingUTF-8 ?gt
  • ltpersongt
  • ltnamegtFredriklt/namegt
  • ltsurnamegtÅlundlt/namegt
  • ltagegt32lt/agegt
  • lt/persongt
  • The first line tells what version of XML is used
    and what encoding to use

18
XML bigger example
  • ltfamilygt
  • ltpersongt
  • ltnamegtFredriklt/namegt
  • ltsurnamegtÅlundlt/namegt
  • ltagegt32lt/agegt
  • lt/persongt
  • ltpersongt
  • ltnamegtAnnikalt/namegt
  • ltsurnamegtÅlundlt/namegt
  • ltagegt28lt/agegt
  • lt/persongt
  • ltpersongt
  • ltnamegtViktorlt/namegt
  • ltsurnamegtÅlundlt/namegt
  • ltagegt0,5lt/agegt
  • lt/persongt
  • lt/familygt

19
XML attributes
  • XML elements can have attributes
  • ltemployeesgt
  • ltemployee empid1gt
  • ltnamegtFredrik Ålundlt/namegt
  • ltdepartment depid3gtServiceslt/departmentgt
  • lt/employeegt
  • ltemployee empid2gt
  • ltnamegtHelena Larssonlt/namegt
  • ltdepartment depid3gt Services lt/departmentgt
  • lt/employeegt
  • lt/employeesgt

20
XML CDATA
  • If the value of an element contain binary data or
    some other illegal characters, CDATA can be used
  • ltelementgtlt!CDATAanytextgtlt/elementgt

21
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

22
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

23
A XSL example
  • A XSL to transform our employee list to html
    might look like
  • lt?xml version'1.0'?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/TR/WD
    -xsl"gt
  • ltxsltemplate match"/"gt
  • lthtmlgt ltbodygt
  • lttable border"2" bgcolor"yellow"gt
  • lttrgt
  • ltthgtNamelt/thgt
  • ltthgtDepartmentlt/thgt
  • lt/trgt
  • ltxslfor-each selectemployees/employee"gt
  • lttrgt
  • lttdgtltxslvalue-of selectname"/gtlt/tdgt
  • lttdgtltxslvalue-of selectdepartment"/gtlt/tdgt
  • lt/trgt lt/xslfor-eachgt
  • lt/tablegt
  • lt/bodygt lt/htmlgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

24
XSL templates
  • Stylesheets starts with ltxslstylesheet
    xmlnsxsl"http//www.w3.org/TR/WD-xsl"gt
  • A template matches a part of a XML document and
    is evaluted
  • ltxsltemplate match"/"gt - matches the root
  • ltxsltemplate matchemployees"gt - matches
    employees

25
XSL ltxslfor-each/gt
  • ltxslfor-each selectemployees/employee"gt loops
    over all employees
  • ltxslfor-each select" employees/employee "
    order-by" name"gt will order the elements on the
    name element ( ascending, - descending)

26
XSL ltxslfor-each/gt and filters
  • Filters can be applied to filter out elements
  • ltxslfor-each selectemployees/employeedepartmen
    t/_at_depid 3 gt
  • ltxslfor-each selectemployees/employeedepartmen
    t Services gt
  • Valid filter operations are
  •   (equal)
  • ! (not equal)
  • LT (less than)
  • GT (greater than)

27
XSL ltxslvalue-of/gt
  • ltxslvalue-of ..gt returns the value of an element
    or attribute
  • ltxslvalue-of selectname"/gt return the value of
    the name element for the current employee
  • ltxslvalue-of select_at_empid"/gt returns the empid
    attribute of the employee element

28
XSL IF
  • The conditional statement if is available in XSL
  • ltxslif match"._at_empid1'"gt
  • lt/xslifgt

29
XSL chose
  • ltxslchoosegt   ltxslwhen match"._at_empid3'"gt 
         ... some code ...   lt/xslwhengt  
    ltxslotherwisegt      ... some code ....  
    lt/xslotherwisegtlt/xslchoosegt

30
XSL Text
  • ltxsltext disable-output-escapingtrue/false /gt
  • Used to output text. Useful if the text is not
    valid XML since it can be combined with
    lt!CDATAxxxgt
  • ltxsltext disable-output-escaping"yes"gt
    lt!CDATAshop?actiondetailbookidgt
  • lt/xsltextgt

31
ltxslapply-template /gt
  • Instead of using for-each, we can use
    ltxslapply-templates matchelement/gt
  • Make ltxsltemplate matchxxx /gt for each
    element
  • Put ltxslapply-template where you want the output
    of the other template

32
ltxslelement /gt
  • In XSL you have to follow XML syntax rules. This
    makes it hard to construct HTML form elements and
    HREFs. ltxslelement gt can be used to ease this
  • A HREF link in XSL
  • ltxslelement name"input"gt
  • ltxslattribute name"href"gt
  • ltxsltext disable-output-escaping"yes"gt
  • lt!CDATA shop?actiondetailbookid  gt
  • lt/xsltextgt
  • ltxslvalue-of select"id" /gt
  • lt/xslattributegt
  • ltxsltextgtDetaillt/xsltextgt
  • lt/xslelementgt

33
ltxslelementgt
  • A HTML Form text input field
  • ltxslelement name"input"gt
  • ltxslattribute name"size"gt2lt/xslattributegt
  •  ltxslattribute name"type"gttextlt/xslattributegt
  •  ltxslattribute name"value"gt1lt/xslattributegt
  •  ltxslattribute name"name"gt
  • quantity
  • lt/xslattributegt
  • lt/xslelementgt

34
The same XSL with apply-template
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/TR/WD
    -xsl"gt
  • ltxsltemplate match"employees"gt
  • lthtmlgt ltbodygt
  • lttable border"2" bgcolor"yellow"gt
  • lttrgt ltthgtIdlt/thgtltthgtNamelt/thgt ltthgtDepartmentlt/thgt
    lt/trgt
  • ltxslapply-templates /gt
  • lt/tablegt
  • lt/bodygt lt/htmlgt
  • lt/xsltemplategt
  • ltxsltemplate match"employee"gt
  • lttrgt
  • lttdgtltxslvalue-of select"_at_empid"/gtlt/tdgt
  • lttdgtltxslvalue-of select"name"/gtlt/tdgt
  • lttdgtltxslvalue-of select"department"/gtlt/tdgt
  • lt/trgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

35
JSTL and XML, resumed
  • JSTL has support for navigating XML with XPath
    and to do XSLT processing
  • lt_at_ taglib prefix"x" uri"http//java.sun.com/jst
    l/xml" gt is used to specify that the XML part
    is to be used

36
JSTL XML - parsing
  • A XML structure can be parsed into a XML document
    that can be navigated
  • ltxparse var"a"gt ltagt ltbgt ltcgt foo lt/cgt lt/bgt ltdgt
    bar lt/dgt lt/agt lt/xparsegt
  • ltxout select"a/a/d"/gt
  • The XML is parsed into variable a
  • The value of the element d is selected

37
JSTL XSL Transformation
  • ltcset var"xml"gt
  • ltagtltbgtheader!lt/bgtlt/agt
  • lt/csetgt
  • ltcset var"xsl"gt
  • lt?xml version"1.0"?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform" version"1.0"gt
  • ltxsltemplate match"text()"gt lth1gt
  • ltxslvalue-of select"."/gtlt/h1gt
  • lt/xsltemplategt
  • lt/xslstylesheetgt
  • lt/csetgt
  • ltxtransform xml"xml" xslt"xsl"/gt

38
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

39
JSTL XSL Transformation combined with JSTL Core
and Custom Tags
  • ltcset var"booklist_xslt"gt
  • ltcimport url"booklist_xslt.xsl"/gt
  • lt/csetgt
  • ltxtransform xslt"booklist_xslt"gt
  • ltjspgetProperty name"bookList"
    property"xml"/gt
  • lt/xtransformgt
Write a Comment
User Comments (0)
About PowerShow.com