XSLT programming examples - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

XSLT programming examples

Description:

author Bertrand Russel /author /book book title Investigations /title ... xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0' ... – PowerPoint PPT presentation

Number of Views:117
Avg rating:3.0/5.0
Slides: 16
Provided by: jaa4
Category:

less

Transcript and Presenter's Notes

Title: XSLT programming examples


1
XSLT programming examples
  • Jaana Holvikivi
  • EVTEK

2
Recursion xml file
  • lt?xml version'1.0'?gt
  • lt?xml-stylesheet type"text/xsl"
    href"books.xsl"?gt
  • ltlibrarygt
  • ltbookgt
  • lttitlegtPennies from heavenlt/titlegt
  • ltauthorgtMae Westlt/authorgt
  • lt/bookgt
  • ltbookgt
  • lttitlegtMemorieslt/titlegt
  • ltauthorgtBertrand Russellt/authorgt
  • lt/bookgt
  • ltbookgt
  • lttitlegtInvestigationslt/titlegt
  • ltauthorgtLudwig Wittgensteinlt/authorgt
  • lt/bookgt
  • lt/librarygt

3
Recursion books.xsl
  • lt?xml version"1.0"?gt
  • ltxslstylesheet xmlnsxsl"http//www.w3.org/199
    9/XSL/Transform" version"1.0"gt
  • ltxsltemplate match"/"gt
  • lthtmlgtltbodygt
  • ltxslelement name"table"gt
  • ltxslapply-templates select"//book"/gt
  • lt/xslelementgt
  • lt/bodygtlt/htmlgt
  • lt/xsltemplategt
  • ...continues

4
Recursion (continues)
  • ltxsltemplate match""gt
  • ltxslif test"count(ancestor) 1"gt
  • ltxslelement name"tr"gt
  • ltxslapply-templates select"child"/gt
  • lt/xslelementgt
  • lt/xslifgt
  • ltxslif test"count(ancestor) !1"gt
  • ltxslelement name"td"gt
  • ltxslvalue-of select"."/gt
  • lt/xslelementgt
  • lt/xslifgt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

5
Generic process for copying
  • lt?xml version"1.0"?gt
  • ltxslstylesheet version"1.0" xmlnsxsl"http//ww
    w.w3.org/1999/XSL/Transform"gt
  • ltxsloutput method"xml" /gt
  • ltxsltemplate match" _at_ processing-instruct
    ion() "gt
  • ltxslcopygt
  • ltxslapply-templates select" _at_ text
    processing-instruction() "/gt
  • lt/xslcopygt
  • lt/xsltemplategt
  • lt/xslstylesheetgt

6
Variables and parameters the alphabet one by one
  • ltxsltemplate name"alphabetTemplate"gt
  • ltxslparam name"alphabet" select"
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ' " /gt
  • ltxslvariable name"letter"
    select"substring(alphabet, 1, 1)" /gt
  • ltxslvariable name"remainder"
    select"substring(alphabet, 2)" /gt
  • .
  • ltxslif test"remainder"gt
  • ltxslcall-template name"alphabetTemplate"/gt
  • ltxslwith-param name"alphabet" select"
    remainder " /gt
  • lt/xslcall-templategt
  • lt/xslifgt
  • lt/xsltemplategt

7
Another HTML exampleAn XML document
representing the results of a soccer tournament
  • ltresults group"A"gt
  • ltmatchgt
  • ltdategt10-Jun-1998lt/dategt
  • ltteam score"2"gtBrazillt/teamgt
  • ltteam score"1"gtScotlandlt/teamgt
  • lt/matchgt
  • ltmatchgt
  • ltdategt10-Jun-1998lt/dategt
  • ltteam score"2"gtMoroccolt/teamgt
  • ltteam score"2"gtNorwaylt/teamgt
  • lt/matchgt
  • ltmatchgt
  • ltdategt16-Jun-1998lt/dategt
  • ltteam score"1"gtScotlandlt/teamgt
  • ltteam score"1"gtNorwaylt/teamgt
  • lt/matchgt

8
Another HTML example, page 2An XML document
representing the results of a soccer tournament
  • ltmatchgt
  • ltdategt16-Jun-1998lt/dategt
  • ltteam score"3"gtBrazillt/teamgt
  • ltteam score"0"gtMoroccolt/teamgt
  • lt/matchgt
  • ltmatchgt
  • ltdategt23-Jun-1998lt/dategt
  • ltteam score"1"gtBrazillt/teamgt
  • ltteam score"2"gtNorwaylt/teamgt
  • lt/matchgt
  • ltmatchgt
  • ltdategt23-Jun-1998lt/dategt
  • ltteam score"0"gtScotlandlt/teamgt
  • ltteam score"3"gtMoroccolt/teamgt
  • lt/matchgt
  • lt/resultsgt

9
A basic style sheet for the soccer results
  • ltxsltransform
  • xmlnsxsl"http//www.w3.org/1999/XSL/Trans
    form" version"1.0"gt
  • ltxsltemplate match"results"gt
  • lthtmlgt
  • ltheadgtlttitlegt
  • Results of Group ltxslvalue-of
    select"_at_group"/gt
  • lt/titlegtlt/headgt
  • ltbodygtlth1gt
  • Results of Group ltxslvalue-of
    select"_at_group"/gt
  • lt/h1gt
  • ltxslapply-templates/gt
  • lt/bodygtlt/htmlgt
  • lt/xsltemplategt
  • ltxsltemplate match"match"gt
  • lth2gt
  • ltxslvalue-of select"team1"/gt
    versus ltxslvalue-of select"team2"/gt
  • lt/h2gt
  • ltpgtPlayed on ltxslvalue-of
    select"date"/gtlt/pgt
  • ltpgtResult

10
  • A style sheet that computes team standings (part
    1)
  • ltxsltransform
  • xmlnsxsl"http//www.w3.org/1999/XSL/Transfor
    m"
  • version"1.0"gt
  • ltxslvariable name"teams" select"//teamnot(.
    precedingteam)"/gt
  • ltxslvariable name"matches"
    select"//match"/gt
  • ltxsltemplate match"results"gt
  • lthtmlgtltbodygt
  • lth1gtResults of Group ltxslvalue-of
    select"_at_group"/gtlt/h1gt
  • lttable cellpadding"5"gt
  • lttrgt
  • lttdgtTeamlt/tdgt
  • lttdgtPlayedlt/tdgt
  • lttdgtWonlt/tdgt

11
  • A style sheet that computes team standings (part
    2)
  • ltxslfor-each select"teams"gt
  • ltxslvariable name"this" select"."/gt
  • ltxslvariable name"played"
    select"count(matchesteamthis)"/gt
  • ltxslvariable name"won"
  • select"count(matchesteam.this
    /_at_score gt team.!this/_at_score)"/gt
  • ltxslvariable name"lost"
  • select"count(matchesteam.this
    /_at_score lt team.!this/_at_score)"/gt
  • ltxslvariable name"drawn"
  • select"count(matchesteam.this
    /_at_score team.!this/_at_score)"/gt
  • ltxslvariable name"for"
  • select"sum(matches/team.current
    ()/_at_score)"/gt
  • ltxslvariable name"against"
  • select"sum(matchesteamcurrent()
    /team/_at_score) - for"/gt
  • lttrgt
  • lttdgtltxslvalue-of select"."/gtlt/tdgt
  • lttdgtltxslvalue-of select"played"/gtlt/t
    dgt
  • lttdgtltxslvalue-of select"won"/gtlt/tdgt

12
A style sheet that computes team standings result
  • The data are rearranged and processed

13
Explanations
  • ltxslvariable name"teams" select"//teamnot(.pr
    ecedingteam)"/gt
  • create a global variable teams (node-set which
    has all team elements, the same element cannot be
    selected twice)
  • this node set is processed in the loop on the
    next page
  • ltxslfor-each select"teams"gt the dollar sign
    refers to variable teams
  • ltxslvariable name"this" select"."/gt
    declare a local variable this, that gets the
    value of the current node
  • count(matchesteamthis)
  • counts the number of those matches for which it
    is true that the name of this team is in the
    element value
  • /_at_score gt team.!this/_at_score
  • compare values of score attributes has this
    team more (gt gt) scores than the other !
    means NOT

14
XSL as a functional programming language the
factorial
  • lt?xml version"1.0"?gtltxslstylesheet
    xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
    version"1.0"gt
  • lt!-- Defining and Calling the Factorial Function
    in XSLT --gtlt!-- A separate file
    factorial-main.xml provides specific arguments
    --gtlt!-- call factorial on selected integer
    argument n --gt
  • ltxsltemplate match"/arguments/a1"gt
  • lthtmlgtltheadgtlttitlegtfactorial(ltxslvalue-of
    select"."/gt)lt/titlegtlt/headgtltbodygt
  • ltxslcall-template name"factorial"gtltxslwith-par
    am name"n" select"."/gtlt/xslcall-templategtlt/bo
    dygt
  • lt/htmlgtlt/xsltemplategt

15
XSL as a functional programming language the
factorial, cont.
  • ltxsltemplate name"factorial"gtltxslparam
    name"n"/gtltxslchoosegtltxslwhen test"n
    0"gt1lt/xslwhengtlt!-- factorial(0) 1
    --gtltxslwhen test"n gt 0"gt lt!-- factorial(n)
    --gtltxslvariable name"factor"gtltxslcall-templat
    e name"factorial"gtltxslwith-param name"n"
    select"n - 1"/gtlt/xslcall-templategtlt/xslvaria
    blegtltxslvalue-of select"n factor"/gt lt!--
    nfactorial(n-1) --gtlt/xslwhengtlt/xslchoosegtlt/x
    sltemplategt
Write a Comment
User Comments (0)
About PowerShow.com