XSLT - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

XSLT

Description:

xsl:apply-templates select='stanza'/ xsl:apply-templates select='date'/ /body ... xsl:template match='stanza' p xsl:apply-templates select='line'/ /p ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 18
Provided by: jiang79
Category:
Tags: xslt | stanza

less

Transcript and Presenter's Notes

Title: XSLT


1
XSLT
  • Jianguo Lu
  • University of Windsor

2
XML based programming
  • With the widespread adoption of XML standard,
    there are languages designed specifically for XML
    processing
  • XML query
  • XQuery
  • XML transformation
  • XSLT
  • relies on XPath and XML Schema
  • Web Service

3
Running XSLT from the client side
  • Browser gets the XMLXSLT, and interprets them
    inside the browser.
  • How to specify the XSL associated with the XML
    file?
  • lt?xml-stylesheet type"text/xsl"
    href"stock.xsl"?gt
  • Advantages
  • Easy to develop and deploy.
  • Disadvantages
  • Not every browser supports XMLXSL
  • Browsers do not support all XSLT features
  • Not secure you only want to show part of the XML
    data
  • Not efficient.

4
Run XSLT from the server side
  • XSL processor transforms the XML and XSLT to
    HTML, and the web server send the HTML to the
    browser.
  • Popular tool xalan
  • Download xalan.jar from
  • http//www.apache.org/dyn/closer.cgi/xml/xalan-j
  • Run Xalan to transform xml documents
  • java -classpath xalan/bin/xalan.jar
    org.apache.xalan.xslt.Process -in stock.xml -xsl
    stock.xsl -out stock.html

XSL Processor
Web server
5
Hello world example
  • lt?xml version"1.0" encoding"iso-8859-1"?gt
  • lt?xml-stylesheet type"text/xsl"
    href"hello.xsl"?gt
  • ltgreetinggtHello, world!lt/greetinggt
  • lt?xml version"1.0" encoding"iso-8859-1"?gt
  • ltxslstylesheet version"1.0"
  • xmlnsxsl
  • "http//www.w3.org/1999/XSL/Transform"gt
  • ltxsltemplate match"/"gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtToday's greetinglt/titlegt
  • lt/headgt
  • ltbodygt
  • ltpgtltxslvalue-of select"greeting"/gtlt/pgt
  • lt/bodygt
  • lthtmlgt
  • ltheadgt
  • ltMETA http-equiv"Content-Type"
    content"text/html charsetUTF-8"gt
  • lttitlegtToday's greetinglt/titlegt
  • lt/headgt
  • ltbodygt
  • ltpgtHello, world!lt/pgt
  • lt/bodygt
  • lt/htmlgt

6
XSLT(XML Stylesheet Language Transformation)
  • XSLT Version 1.0 is a W3C Recommendation, 1999
  • http//www.w3.org/Style/XSL/
  • XSLT is used to transform XML to other formats.

7
XSLT basics
  • XSLT is an XML document itself
  • It is a tree transformation language
  • It is a rule-based declarative language
  • XSLT program consists of a sequence of rules.
  • It is a functional programming language.
  • It has no side-effects

8
Poem example xml and intended html output
9
Poem example XSL
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/199
    9/XSL/Transform"
  • version"1.0"gt
  • ltxsltemplate match"poem"gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtltxslvalue-of select"title"/gtlt/title
    gt
  • lt/headgt
  • ltbodygt
  • ltxslapply-templates select"title"/gt
  • ltxslapply-templates select"author"/gt
  • ltxslapply-templates select"stanza"/gt
  • ltxslapply-templates select"date"/gt
  • lt/bodygt
  • lt/htmlgt
  • lt/xsltemplategt
  • ltxsltemplate match"title"gt
  • ltdiv align"center"gt
  • lth1gtltxslvalue-of select"."/gtlt/h1gt
  • lt/divgt
  • lt/xsltemplategt
  • ltxsltemplate match"author"gt
  • ltdiv align"center"gt
  • lth2gtBy ltxslvalue-of select"."/gtlt/h2gt
  • lt/divgt
  • lt/xsltemplategt
  • ltxsltemplate match"date"gt
  • ltpgtltigtltxslvalue-of select"."/gtlt/igtlt/pgt
  • lt/xsltemplategt
  • ltxsltemplate match"stanza"gt
  • ltpgtltxslapply-templates select"line"/gtlt/pgt
  • lt/xsltemplategt

10
Rule-based
  • XSLT consists of a sequence of template rules
  • Each describes how a particular element type (or
    other constructs) can be processed.
  • Rules are not arranged in any particular order
  • they dont have to match the order of input or
    output
  • Programmers dont know (and dont care) the
    execution ordering of the rules.
  • That is why it is called a declarative language
  • Programmer specify what output should produce
    when particular pattern occurs in input

11
No side effect
  • If functions have side effects, it is important
    to call them the right number of times, in a
    correct order, and in a correct context.
  • Why XSLT does not want to have side effects
  • Requirement from the rule-based language
  • Incremental development
  • Incremental rendering

12
XSLT process model
  • ltsourcegt
  • ltemployeegt
  • ltfirstNamegtJoelt/firstNamegt
  • ltsurNamegtSmithlt/surNamegt
  • lt/employeegt
  • ltemployeegt
  • ltfirstNamegtAndrewlt/firstNamegt
  • ltsurNamegtWanglt/surNamegt
  • ltsupervisorgt
  • ltemployeegt
  • ltfirstNamegtStevelt/firstNamegt
  • ltsurNamegtMillerlt/surNamegt
  • lt/employeegt
  • lt/supervisorgt
  • lt/employeegt
  • lt/sourcegt
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltxslstylesheet xmlnsxsl
  • "http//www.w3.org/1999/XSL/Transform"
  • version1.0"gt
  • ltxsltemplate match"employee"gt
  • Hello, Found you!
  • lt/xsltemplategt
  • lt/xslstylesheetgt
  • Output
  • Hello, Found you!
  • Hello, Found you!

13
Built-in template
  • Select the employee who is a supervisor
    (incorrect)
  • ltxsltemplate match"employee/supervisor/employee"
    gt
  • Hello, found you!
  • lt/xsltemplategt
  • lt/xslstylesheetgt
  • XSLT starts with the root of the document.
  • If there is no template rule defined, built-in
    template is invoked.
  • For element, the built-in template processes the
    children of the current element.
  • For text, the built-in template copy the text to
    the result tree.
  • Outout
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • Joe
  • Smith
  • Andrew
  • Wang
  • Hello, found you!

14
XML Tree
  • Output
  • Joe
  • sur name is Smith
  • Andrew
  • sur name is Wang
  • Steve
  • sur name is Miller
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
    XSL/Transform" version"1.0"gt
  • ltxsltemplate match"surName"gt
  • sur name is ltxslvalue-of select"." /gt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

document
ltsourcegt
ltemployee gt
ltemployeegt
text
text
text
ltsupervisorgt
ltfirstNamegt
ltfirstNamegt
ltsurNamegt
ltsurNamegt

Andrew
Joe
Smith
Wang
15
Override built-in template
  • Pick employees who are supervisors (still not
    correct)
  • ltxsltemplate match"employee"gt
  • lt/xsltemplategt
  • ltxsltemplate match"employee/supervisor/employee"
    gt
  • Hello, found you!
  • lt/xsltemplategt
  • lt/xslstylesheetgt
  • Output
  • nothing.

16
Override built-in template
  • Output
  • Joe
  • Smith
  • Andrew
  • Wang
  • Hello, found you!
  • When find outer employees, apply rules
  • ltxsltemplate match"employee"gt
  • ltxslapply-templates/gt
  • lt/xsltemplategt
  • ltxsltemplate match"employee/supervisor/employe
    e"gt
  • Hello, found you!
  • lt/xsltemplategt
  • lt/xslstylesheetgt

17
More specific about what to do
  • ltxsltemplate match"employee"gt
  • ltxslapply-templates selectsupervisor/gt
  • lt/xsltemplategt
  • ltxsltemplate match"employee/supervisor/employe
    e"gt
  • Hello, found you!
  • lt/xsltemplategt
  • lt/xslstylesheetgt
  • Output
  • Hello, found you!
Write a Comment
User Comments (0)
About PowerShow.com