XSLT eXtensible Stylesheet Language Transformations - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

XSLT eXtensible Stylesheet Language Transformations

Description:

student Jack Black /student /class xsl:template match='class' ... Zack Black /name address street 101 Main St /street city Searcy /city state AR /state ... – PowerPoint PPT presentation

Number of Views:1208
Avg rating:3.0/5.0
Slides: 30
Provided by: frankm2
Category:

less

Transcript and Presenter's Notes

Title: XSLT eXtensible Stylesheet Language Transformations


1
XSLTeXtensible Stylesheet Language
Transformations
  • Advanced Internet Dev COMP 431
  • Frank McCown
  • Spring 2004
  • Modified slides from www.corewebprogramming.com

2
Agenda
  • XSLT overview
  • Understanding XPath notation
  • Processing elements in XSLT templates
  • Example

3
XSLT Overview
  • W3C technology for transforming an XML document
    into some other text-based form XML, HTML, WML,
    etc.
  • XSLT Versions
  • XSLT 1.0 (Nov 1999)
  • XSLT 2.0 (Nov 2002)
  • Official web sitehttp//www.w3c.org/Style/XSL

4
eXtensible Stylesheet Language (XSL)
  • XSL is a language for expressing stylesheets.
  • XSLT
  • Transforming XML document
  • Xpath
  • Expression language used by XSLT to locate
    elements and attributes in an XML doc.
  • XSL-FO (Formatting Objects)
  • Specifies formatting properties for rendering the
    doc to some other format.

5
XSLT Advantages Disadvantages
  • Advantages
  • Easy display formatted XML data in browser.
  • Easier to modify when XML data format changes
    than to modify DOM and SAX parsing code.
  • Can be used with database queries that return
    XML.
  • Disadvantages
  • Memory intensive, performance hit.
  • Difficult to implement complex business rules.
  • Have to learn new language.

6
XSLT Processors
  • Apache Xalanhttp//xml.apache.org/xalan
  • SAXONhttp//saxon.sourceforge.net
  • Microsofts XML Parser 4.0 (MSXML)http//www.micr
    osoft.com/xml

7
XSLT and Java
  • JDK 1.4 contains all necessary classes
  • See javax.xml.transform package.
  • Lower versions require downloading XSLT processor
    and SAX parser.

8
XML Transformation Example
hello.xml
HTML
hello.xsl
9
lt?xml version"1.0" ?gt lt!-- hello.xml
--gt lt?xmlstylesheet type"text/xsl"
href"hello.xsl"?gt ltmyMessagegt ltmessagegtHello
XSLT!lt/messagegt lt/myMessagegt
lthtmlgt ltbodygt lth1gtHello XSLT!lt/h1gt lt/bodygt lt/htmlgt
lt?xml version"1.0" ?gt lt!-- hello.xsl
--gt ltxslstylesheet version"1.0"
xmlnsxsl"http//www.w3.org/1999/XSL/Transform"gt
ltxsltemplate match"myMessage"gt
lthtmlgtltbodygt lth1gtltxslvalue-of
select"message"/gtlt/h1gt lt/bodygtlt/htmlgt
lt/xsltemplategt lt/xslstylesheetgt
10
Running Example
  • Load hello.xml into IE.
  • Run Xalan from the command linejava
    org.apache.xalan.xslt.Process -in hello.xml -xsl
    hello.xsl
  • Run our Transform.java from the command line.

11
XPath
  • XPath is an expression language used to
  • Find nodes and attributes (location paths) in the
    XML file
  • Test boolean conditions
  • Manipulate strings
  • Perform numerical calculations

12
Location Paths
  • Example ltxsltemplate matchentry/address/
    street/gt lt/xsltemplategt
  • Can be relative or absolute
  • Each step in path separated by / or //
  • Evaluated in reference to the current node

13
Location Paths (cont.)
  • Match root node
  • ltxsltemplate match//gt
    lt/xsltemplategt
  • Match all children
  • ltxsltemplate match/gt
    lt/xsltemplategt

14
Location Paths (cont.)
  • Match an element
  • Use // to indicate zero or more elements may
    occur between slashes
  • ltxsltemplate matchentry//street/gt
    lt!-- Match all street elements that are
  • grandchildren of entry. --gt
    lt/xsltemplategt
  • ltxsltemplate matchorder//item/gt lt!--
    Match all item elements that are
  • descendants of order. --gt
  • lt/xsltemplategt

15
Location Paths (cont.)
  • Match a specific element
  • Use as a predicate filter to select a
    particular element
  • ltxsltemplate matchauthor/namemiddle/gt
    lt!-- Match all name elements that have an
    author
  • parent and a middle child. --gt
  • lt/xsltemplategt
  • ltxsltemplate matchentry/phone1/gt lt!--
    Match the first phone element that is a
  • child of entry. --gt
  • lt/xsltemplategt

16
Location Paths (cont.)
  • Match a specific attribute
  • Use _at_attribute to select a particular attribute
  • ltxsltemplate matchphone_at_location/gt lt!--
    Match all phone elements that have a
  • location attribute. --gt
  • lt/xsltemplategt
  • ltxsltemplate matchcatalog/item_at_id123/gt
    lt!-- Match only item elements with id attribute
  • of 123. --gt
  • lt/xsltemplategt

17
XSLT Stylesheet Elements
  • Matching and selection templates
  • xsltemplate
  • xslapply-templates
  • xslvalue-of
  • Branching elements
  • xslfor-each
  • xslif
  • xslchoose

18
XSLT template Element
  • xsltemplate matchXPath
  • Defines a template rule for producing output
  • Is applied only to nodes that match the pattern
  • Invoked by using ltxslapply-templates/gt
  • ltxsltemplate match"/"gt
  • lthtmlgtltbodygt
  • ltxslapply-templates/gt
  • lt/bodygtlt/htmlgt
  • lt/xsltemplategt
  • ltxsltemplate matchname"gt
  • Your name is ltxslvalue-of select."/gt
  • lt/xsltemplategt

19
XSLT value-of Element
  • xslvalue-of selectexpression
  • Evaluates the expression as a string and outputs
    the result
  • Applied only to the first match
  • . selects the text value of the current node
  • ltname prefixMr.gtJohn Doelt/namegt
  • _________________________________________________
  • ltxsltemplate matchname"gt
  • Your name is ltxslvalue-of select_at_prefix"/gt
    ltxslvalue-of select."/gt
  • lt/xsltemplategt

20
XSLT for-each Element
  • xslfor-each selectexpression
  • Processes each node selected by the XPath
    expression
  • Applied only to the first match
  • . selects the text value of the current node
  • ltclassgt
  • ltstudentgtKim Smithlt/studentgt
  • ltstudentgtJack Blacklt/studentgt
  • lt/classgt
  • _________________________________________________
    ______
  • ltxsltemplate matchclass"gt
  • ltxslfor-each selectstudent"gt
  • ltbgtltxslvalue-of select."/gtlt/bgt
  • lt/xslfor-eachgt
  • lt/xsltemplategt

21
XSLT if Element
  • xslif testexpression
  • Evaluates the expression and if true applies the
    template
  • No if-else, use choose instead
  • ltxsltemplate matchclass"gt
  • lt!-- Select the first node in the set. --gt
  • ltxslif selectposition() first()"gt
  • ltbgtltxslvalue-of select."/gtlt/bgt
  • lt/xslifgt
  • lt/xsltemplategt

22
XSLT choose Element
  • xslchoose
  • Selects any number of alternatives
  • Use instead of if-else, or switch statement used
    in other programming languages
  • ltxslchoosegt
  • ltxslwhen testnot(text())"gt
  • Missing value!
  • lt/xslwhengt
  • ltxslotherwisegt
  • ltxslvalue-of select./gt
  • lt/xslotherwisegt
  • lt/xslchoosegt

23
XSLT Functions
  • Large set of utility functions
  • Examples
  • count returns the number of nodes in a node
    setltxslvalue-of select"count(/addressbook/e
    ntry)"/gt
  • starts-with returns true if string starts with
    a given characterltxslif test"starts-with(_at_loca
    tion, 'h')"gt Starts with hlt/xslifgt

24
Address Book Example
ltaddressbookgt ltentrygt ltname
prefix"Mr."gt Zack Blacklt/namegt ltaddressgt
ltstreetgt101 Main Stlt/streetgt
ltcitygtSearcylt/citygt ltstategtARlt/stategt
ltzipgt12345lt/zipgt lt/addressgt ltphone
location"home" number"501-279-9999" /gt
ltphone location"work" number"501-279-1111"
/gt lt/entrygt ltentrygt ltnamegtJohn
Brownlt/namegt ltphone number"501-305-2222" /gt
lt/entrygt
ltentrygt ltnamegtSusan Whitelt/namegt
ltaddressgt ltstreetgt2000 Center Rdlt/streetgt
ltcitygtDallaslt/citygt ltstategtTXlt/stategt
ltzipgt67790lt/zipgt lt/addressgt ltphone
location"home number"501-279-0000" /gt
lt/entrygt lt/addressbookgt
25
lt?xml version"1.0" encoding"ISO-8859-1"?gt ltxsl
stylesheet xmlnsxsl"http//www.w3.org/1999/XS
L/Transform" version"1.0"gt ltxsloutput
method"html"/gt ltxsltemplate
match"/addressbook"gt lthtmlgtltbodygt
lttable border"1"gt ltcaptiongtAddress
Booklt/captiongt lttrgt ltthgtNamelt/thgtltthgtA
ddresslt/thgtltthgtPhone Number(s)lt/thgt lt/trgt
ltxslapply-templates select"entry"/gt
lt/tablegt lt/bodygtlt/htmlgt
lt/xsltemplategt
26
ltxsltemplate match"entry"gt lttrgt
lttdgtltxslapply-templates select"name"/gtlt/tdgt
lttdgtltxslapply-templates select"address"/gtlt/tdgt
lttdgtltxslapply-templates select"phone"/gtlt/td
gt lt/trgt lt/xsltemplategt ltxsltemplate
match"name"gt ltxslvalue-of
select"_at_prefix"/gt ltxsltextgt lt/xsltextgt
ltxslvalue-of select"."/gt lt/xsltemplategt
ltxsltemplate match"address"gt ltxslvalue-of
select"street"/gtltbr/gt ltxslvalue-of
select"city"/gt, ltxslvalue-of select"state"/gt
ltxsltextgt lt/xsltextgt ltxslvalue-of
select"zip"/gt lt/xsltemplategt ltxsltemplate
match"phone"gt ltxslvalue-of
select"_at_location"/gt ltxslvalue-of
select"_at_number"/gt ltbr/gt lt/xsltemplategt lt/
xslstylesheetgt
27
(No Transcript)
28
References
  • www.corewebprogramming.com
  • www.w3c.org/Style/XSL
  • XML How to Program by Deitel

29
Esercizio 3
  • Scrivere una trasformata XSLT per renderizzare in
    HTML il file XML costruito allesercizio 2.
Write a Comment
User Comments (0)
About PowerShow.com