Intermediate XSLT - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Intermediate XSLT

Description:

shirt colorCode='c4' oxford button-down /shirt ... blue oxford button-down. yellow poly blend, straight collar. white monogrammed, tab collar ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 34
Provided by: snee
Category:

less

Transcript and Presenter's Notes

Title: Intermediate XSLT


1
  • Intermediate XSLT
  • Bob DuCharme
  • www.snee.com/bob
  • bob_at_snee.com
  • these slides www.snee.com/xml
  • 1.0

2
Outline
  • Variables and parameters
  • Named templates and parameters
  • xslfor-each and looping
  • XSLT extensions and EXSLT
  • Using keys for faster lookups
  • XSLT and browsers

3
Variables
  •       xmlnsxsl"http//www.w3.org/1999/XSL/Transf
    orm"
  • John
  •   Congratulations, 
  •   
  •   , you are a winner!
  •   
  • Input document
  •     
  • Output
  •     Congratulations, John, you are a winner!

4
Passing Parameters to Stylesheets
  •       xmlnsxsl"http//www.w3.org/1999/XSL/Transf
    orm"
  • John
  •   Congratulations, 
  •   
  •   , you are a winner!
  •   
  • Same input document, same result, but when we
    override the default parameter value
  • C\saxon temp.xml test.xsl winnerNameJane

5
Named Templates
  •   
  •   
  •   
  •   

6
Passing Parameters to Named Templates
  •   
  •     h4
  •     
  •       
  •     
  •   
  •   
  •     
  •       
  • name"headerElement"h1
  •     
  •   
  •   
  •     
  •       
  • name"headerElement" select"'h2'"/
  •     

7
Iterating across a set of nodes
  • 3
  • 10
  • 4
  • 20
  • 5
  • xmlnsxsl"http//www.w3.org/1999/XSL/Transfor
    m"
  • output

8
Repetition
  • 1
  • -
  • select"howMany - 1"/

9
Testing the recursive template rule
  • Print 1 hyphen
  • Print 20 hyphens
  • Print 0 hyphens

10
Recursive template rule result
  • Print 1 hyphen
  • -
  • Print 20 hyphens
  • --------------------
  • Print 0 hyphens

11
XSLT Extension functions
  • xmlnsxsl"http//www.w3.org/1999/XSL/Transfo
    rm"
  • xmlnssaxon"http//saxon.sf.net/"

12
Testing the extension function
  • Source document
  • test
  • Result
  • /a
  • /a/b1
  • /a/b2
  • /a/b2/c1
  • test
  • /a/b3

13
Extension elements (and attributes)
  • xmlnsxsl"http//www.w3.org/1999/XSL/Transform
    "
  • xmlnssaxon"http//saxon.sf.net/"
  • extension-element-prefixes"saxon"
  • saxonassignable"yes"/
  • The value of i is select"i"/

14
Extension element test output
  • The value of i is 0
  • The value of i is 1
  • The value of i is 2
  • The value of i is 3
  • The value of i is 4
  • The value of i is 5
  • The value of i is 6
  • The value of i is 7
  • The value of i is 8
  • The value of i is 9

15
Checking for extension support (option 1)
  • The value of i is
  • Your XSLT processor doesn't
  • support saxonwhile.

16
Checking for extension support (option 2)
  • "
  • The value of i is select"i"/
  • Your XSLT processor doesn't
    support
  • saxonwhile.
  • checking for extension function support
  • function-available()

17
EXSLT
  • Saxon documentation on its extensions
  • Before using a Saxon extension, check whether
    there is an equivalent EXSLT extension
    available. EXSLT extensions are more likely to be
    portable across XSLT processors.
  • http//www.exslt.org

18
Some EXSLT extensions (pt 1)
  • xmlnsxsl"http//www.w3.org/1999/XSL/Transfo
    rm"
  • xmlnsexsl"http//exslt.org/common"
  • xmlnsmath"http//exslt.org/math"
  • xmlnsdate"http//exslt.org/dates-and-times"
  • exslobject-type(3)
  • exslobject-type('potrzebie')
  • bie')"/
  • exslobject-type(2 gt 1)
  • 1)"/

19
Some EXSLT extensions (pt 2)
  • mathconstant('PI',4)

  • mathconstant('PI',12)
  • mathsqrt(256)
  • datedate-time
  • dateday-name

20
EXSLT demo result
  • exslobject-type(3)
  • number
  • exslobject-type('potrzebie')
  • string
  • exslobject-type(2 gt 1)
  • boolean
  • mathconstant('PI',4)
  • 3.1415
  • mathconstant('PI',12)
  • 3.141592653589
  • mathsqrt(256)
  • 16
  • datedate-time
  • 2005-06-25T112412-0400
  • dateday-name
  • Saturday

21
Declaring and using lookup keys
  • we have
  • yellow
  • blue
  • orange
  • green
  • oxford
    button-down
  • poly blend, straight
    collar
  • monogrammed, tab
    collar
  • we want
  • blue oxford button-down
  • yellow poly blend, straight collar

22
Without keys
  • xmlnsxsl"http//www.w3.org/1999/XSL/Transfo
    rm"
  • select"_at_colorCode"/
  • select"/shirts/colors/color_at_cid
    shirtColorCode"/
  • sltext

23
With keys
  • xmlnsxsl"http//www.w3.org/1999/XSL/Transfo
    rm"
  • use"_at_cid"/
  • ode)"/

24
How key lookup works

25
More lookups (part 1)
  • xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
  • use"_at_cid"/
  • use"."/
  • c4
  • colorKeyble
  • Looking up the color name with the color ID
  • c3's color y','c3')"/
  • c4's color y',testVar)"/
  • c8's color y','c8')"/
  • c7's colors

26
More lookups (part 2)
  • Looking up the color ID with the color name
  • blue's cid 'blue')/_at_cid"/
  • black's cid black')/_at_cid"/
  • gray's cid 'gray')/_at_cid"/

27
More lookups result
  • Looking up the color name with the color ID
  • c3's color red
  • c4's color blue
  • c8's color
  • c7's colors
  • orange green
  • Looking up the color ID with the color name
  • blue's cid c4
  • black's cid c2
  • gray's cid

28
Muenchian Grouping
  • xmlnsxsl"http//www.w3.org/1999/XS
    L/Transform"
  • use"_at_project"/
  • "filecount(. key('files-by-project',
    _at_project)1) 1"
  • /
  • t', _at_project)"
  • ,

  • bytes

29
Muenchian Grouping result
  • jupiter
  • gadabout.pas,685 bytes
  • kwatz.xom,43 bytes
  • potrzebie.dbf,1102 bytes
  • mars
  • schtroumpf.txt,389 bytes
  • swablr.eps,4313 bytes
  • ummagumma.zip,2441 bytes
  • neptune
  • batboy.wks,424 bytes
  • mondegreen.doc,1993 bytes
  • paisley.doc,988 bytes

30
Web Browsers and XSLT
  • W3C Recommendation "Associating Style Sheets
  • with XML documents"
  • (http//www.w3.org/TR/xml-stylesheet) shows how
  • type"text/xsl" ?
  • 2
  • 11
  • 100
  • -5

31
Web Browsers and XSLT (part 1)
  • xmlnsxsl"http//www.w3.org/1999/XSL/Transform
    "
  • Squares
  • body font-family arial,helvetica
  • h1 font-size 14pt
  • p font-size 10pt
  • Squares

32
Web Browsers and XSLT (part 2)
  • The square of
  • is
  • value"/.
  • You could create your HTML like this
  • saxon -o numbers.html numbers.xml
    squareAsHTML.xsl

33
XSLT 2.0
  • Lots more built-in functions
  • Writing your own functions
  • Regular expressions
  • Grouping output by values
  • Tokenizing strings
  • Typing awareness
Write a Comment
User Comments (0)
About PowerShow.com