Download presentation
Presentation is loading. Please wait.
Published byKennedy Larabee Modified over 9 years ago
1
©CSW Group Ltd 2005 Intermediate XSLT Bob DuCharme www.snee.com/bob bob@snee.com these slides: www.snee.com/xml 1.0
2
Outline Variables and parameters Named templates and parameters xsl:for-each and “looping” XSLT extensions and EXSLT Using keys for faster lookups XSLT and browsers
3
Variables <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> John Congratulations,, you are a winner! Input document: Output: Congratulations, John, you are a winner!
4
Passing Parameters to Stylesheets <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 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 winnerName=Jane Congratulations, Jane, you are a winner!
5
Named Templates
6
Passing Parameters to Named Templates h4 <xsl:with-param name="headerElement">h1 <xsl:with-param name="headerElement" select="'h2'"/>
7
Iterating across a set of nodes 3 10 4 20 5 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> output: 8 9 10
8
Repetition 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 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/">
12
Testing the extension function Source document: test Result: /a /a/b[1] /a/b[2] /a/b[2]/c[1] test /a/b[3]
13
Extension elements (and attributes) <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/" extension-element-prefixes="saxon"> <xsl:variable name="i" select="0" saxon:assignable="yes"/> The value of i is
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 saxon:while.
16
Checking for extension support (option 2) The value of i is Your XSLT processor doesn't support saxon:while. 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) <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" xmlns:math="http://exslt.org/math" xmlns:date="http://exslt.org/dates-and-times"> exsl:object-type(3): exsl:object-type('potrzebie'): exsl:object-type(2 > 1):
19
Some EXSLT extensions (pt 2) math:constant('PI',4): math:constant('PI',12): math:sqrt(256): date:date-time: date:day-name:
20
EXSLT demo result exsl:object-type(3): number exsl:object-type('potrzebie'): string exsl:object-type(2 > 1): boolean math:constant('PI',4): 3.1415 math:constant('PI',12): 3.141592653589 math:sqrt(256): 16 date:date-time: 2005-06-25T11:24:12-04:00 date:day-name: Saturday same result with Saxon and Xalan Java!
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 white monogrammed, tab collar
22
Without keys <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="shirtColorCode" select="@colorCode"/> <xsl:value-of select="/shirts/colors/color[@cid = $shirtColorCode]"/>
23
With keys <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
24
How key lookup works
25
More lookups (part 1) <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> c4 colorKey Looking up the color name with the color ID: c3's color: c4's color: c8's color: c7's colors:
26
More lookups (part 2) Looking up the color ID with the color name: blue's cid: black's cid: gray's 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 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:for-each select= "file[count(. | key('files-by-project', @project)[1]) = 1]">, 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: 2 11 100 -5
31
Web Browsers and XSLT (part 1) <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- squareAsHTML.xsl: create an HTML document with a statement about the square of each "number" element read from the source tree. --> 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. 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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.