Title: Intermediate XSLT
1- Intermediate XSLT
- Bob DuCharme
- www.snee.com/bob
- bob_at_snee.com
- these slides www.snee.com/xml
- 1.0
2Outline
- Variables and parameters
- Named templates and parameters
- xslfor-each and looping
- XSLT extensions and EXSLT
- Using keys for faster lookups
- XSLT and browsers
3Variables
- Â Â Â Â Â Â xmlnsxsl"http//www.w3.org/1999/XSL/Transf
orm" -
- John
-
- Â Â Congratulations,Â
- Â Â
-   , you are a winner!
- Â Â
-
-
- Input document
- Â Â Â Â
- Output
-     Congratulations, John, you are a winner!
4Passing 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
5Named Templates
6Passing Parameters to Named Templates
- Â Â
- Â Â Â Â h4
- Â Â Â Â
- Â Â Â Â Â Â
- Â Â Â Â
- Â Â
- Â Â
- Â Â Â Â
- Â Â Â Â Â Â
- name"headerElement"h1
- Â Â Â Â
- Â Â
- Â Â
- Â Â Â Â
- Â Â Â Â Â Â
- name"headerElement"Â select"'h2'"/
- Â Â Â Â
7Iterating across a set of nodes
-
- 3
- 10
- 4
- 20
- 5
-
- xmlnsxsl"http//www.w3.org/1999/XSL/Transfor
m" -
-
-
-
-
-
-
- output
8Repetition
9Testing the recursive template rule
-
- Print 1 hyphen
-
- Print 20 hyphens
-
-
-
- Print 0 hyphens
-
-
-
-
10Recursive template rule result
- Print 1 hyphen
- -
- Print 20 hyphens
- --------------------
- Print 0 hyphens
11XSLT Extension functions
- xmlnsxsl"http//www.w3.org/1999/XSL/Transfo
rm" - xmlnssaxon"http//saxon.sf.net/"
-
-
-
-
-
-
-
12Testing the extension function
- Source document
-
-
- test
-
-
- Result
- /a
-
- /a/b1
-
- /a/b2
- /a/b2/c1
- test
- /a/b3
13Extension 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"/
-
-
-
-
14Extension 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
15Checking for extension support (option 1)
-
- The value of i is
-
- Your XSLT processor doesn't
- support saxonwhile.
-
-
-
16Checking 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()
17EXSLT
- 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
18Some 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)"/
19Some EXSLT extensions (pt 2)
- mathconstant('PI',4)
-
- mathconstant('PI',12)
-
- mathsqrt(256)
-
- datedate-time
-
- dateday-name
-
-
-
20EXSLT 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
21Declaring 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
22Without keys
- xmlnsxsl"http//www.w3.org/1999/XSL/Transfo
rm" -
-
-
- select"_at_colorCode"/
-
- select"/shirts/colors/color_at_cid
shirtColorCode"/ - sltext
-
-
-
23With keys
- xmlnsxsl"http//www.w3.org/1999/XSL/Transfo
rm" -
- use"_at_cid"/
-
-
- ode)"/
-
-
24How key lookup works
25More 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
-
26More 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"/
-
-
-
-
27More 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
28Muenchian 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 -
-
-
29Muenchian 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
-
30Web 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
31Web 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
-
32Web 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
33XSLT 2.0
- Lots more built-in functions
- Writing your own functions
- Regular expressions
- Grouping output by values
- Tokenizing strings
- Typing awareness