Title: Whats New in XSLT 2.0
1Whats New in XSLT 2.0?
- Steve Heckler
- Accelebrate
- http//www.accelebrate.com
- steveheckler_at_accelebrate.com
(404) 849-1850 phone (404) 420-2491 fax
2Acknowledgement
- Bob DuCharme (bob_at_snee.com), who generously gave
me permission to use code from his XSLT 2.0
article series on xml.com (published by OReilly
Associates) - Bob is author of XSLT Quickly by Manning (see
http//www.snee.com/bob/xsltquickly) - 1,000 new copies are being printed in February,
so be persistent if you look for it! - Bobs articles are all linked from
http//www.xml.com/pub/au/42
3What Well Cover
- Role of XSLT
- XSLT 2.0 status/overview
- New features of XSLT 2.0
- Grouping
- Functions
- Data type checking
- String handling regular expressions
- Transclusion
- Temporary Trees
- Conclusion
4Role of XSLT
- XSLT allows you to transform XML documents
- The results can be
- Same XML document type with filtered data
- Another XML document type
- Other text formats
- HTML
- Delimited text
- etc., etc.
- Within XSLT stylesheets, XPath is the language
used to select the elements to operate on
5XSLT inputs and outputs
6XSLT 2.0 Status (as of late Jan 2004)
- XSLT 2.0 and XPath 2.0 are still working drafts
- Latest drafts of both were released on 12
November 2003. - Final Recommendation status is anticipated (but
not guaranteed) for this year - Only Saxon (http//saxon.sourceforge.net)
implements XSLT 2.0 - Saxon 7.8 runs on J2SE 1.4 and later
- Other implementations are expected to rapidly
emerge once XSLT 2.0 and XPath 2.0 achieve Final
Recommendation status
7Grouping
- XSLT 1.0 allowed sorting, but not SQL-style
grouping - ltxslfor-each-group /gt iterates through a set of
groups. Attributes - select XPath expression for which elements to
select - group-by Node by which to group the results
- group-adjacent Leaves elements in their original
order and groups elements with the same key value
together - group-starting-with Specifies a node that will
be treated as the beginning of a new group - group-ending-with Specifies a node that will be
treated as the end of the group
8Grouping
- Functions to know
- current-grouping-key() returns the current value
of the grouping key shared by members of the
group - current-group() returns a reference to the
current group - The code Ive shown and a related article are
available at http//www.xml.com/pub/a/2003/11/05/t
r.html
9Functions
- XSLT is really a programming language, but in
version 1.0 lacked the ability to define new
functions - XSLT 2.0 rectifies this
- You can define new functions
- These functions can take 0 or more parameters
- These functions can return a value
10Functions
- ltxslfunctiongtlt/xslfunctiongt wraps the
function. Attributes - name The name of the function
- as Data type returned by the function (as an
XML Schema data type) - override Whether this function should override
previous definitions with the same name and arity
(number of xslparam elements in the function
definition)
11Functions
- Content of the function
- 0 or more ltxslparam /gt elements that define
incoming parameters - Code to execute final value can be returned via
ltxslvalue-of /gt, ltxslsequence /gt, or another
expression - The code Ive shown and a related article are
available at http//www.xml.com/pub/a/2003/09/03/t
rxml.html
12Data Type Checking
- XSLT 2.0 supports the XML Schema data typing
system - XPath 2.0s castable as keyword tests whether a
value can be converted to a certain type and
returns a boolean with the result - The code Ive shown and a related article are
available at http//www.xml.com/pub/a/2003/10/01/t
r.html
13String Handling
- String comparison
- Strings can now be compared (in ASCII order) via
the compare() function, which takes 2 strings as
parameters - -1 means the string on the left is greater
- 0 means the strings are equal
- 1 means the string on the right is greater
14String Handling
- Find/Replace
- translate(), in XSLT 1.0, only let you replace
individual characters with other characters - Similar to tr// in Perl
- replace(), introduced in XSLT 2.0, lets you
replace strings of characters. Parameters - The string to search (required)
- The substring to search for (required)
- The string with which this substring should be
replaced (required) - Flags (optional)
- m multiline match mode
- i ignore case
15String Handling
- Concatenation
- string-join() joins a sequence of strings into a
single string. Arguments - The sequence whose elements are to be joined
- The delimiting character(s) that currently
separate the elements in the sequence - tokenize() does the reverse, splitting a string
into a sequence. Parameters - The string to tokenize
- The string or pattern to use as a delimiter
between tokens
16String Handling
- The code from this section and related articles
can be found at - http//www.xml.com/pub/a/2003/05/07/tr.html
- http//www.xml.com/pub/a/2003/08/06/tr.html
17Regular Expressions
- Regular expressions, now supported by most modern
programming languages, allow you to search for
patterns within strings - They can be used in XML Schemas to pattern
restrict elements and attributes whose type is
xsstring
18Regular Expressions
- Within XSLT 2.0, there are three key functions
that use regular expressions - tokenize() discussed earlier Lets you split a
string into pieces at each point where a
delimiter or pattern occurs - matches() Returns a boolean with whether the
string in the first argument contains the pattern
in the second argument - replace() Replaces the string or pattern in the
first argument with the string in the second
argument
19Regular Expressions
- Retrieving back references
- When a string matches a pattern, you can retrieve
the pieces of the string that matched specific
sections of the pattern - Each of these sections should be wrapped in ()
within the pattern - From left to right, the values of these sections
will be assigned to 1, 2, 3, etc.
20Basic Frequency Operators
- ? 0 or 1
- 0 or more
- 1 or more
- So, 3 would match 3, 33, and 3333, but not 45 or
678.
21Wildcard Operator .
- . matches any non-newline character
- Unless multiline mode has been turned on for the
pattern - Examples
- A. would match a capital A followed by any
character. - A. would match a capital A followed by one or
more non-newline characters - \.htm.? would match ".htm" followed by an
optional non-newline character - The backslash is used to escape characters that
have reserved meanings in regular expressions
22Enclosing Alternatives with
- The square brackets allow you to specify a list
of alternate values. Used in conjunction with
the operator, you can even specify character
ranges. - Cc Capital or lowercase c
- A-Z Any capital letter A through Z
- A-Za-z Any capital or lowercase letter
- 0-9 Any digit 0 through 9
- A-Za-z0-9 Any letter or digit
23Controlling ExpressionFrequency with
- The operators allow you to control the
frequency of the preceding expression. The
expression takes one of these two forms - precise_number_of_occurrences
- min_occurrences, max_occurences
24Controlling ExpressionFrequency with
- A-Za-z3
- A capital letter followed by 3 lowercase letters
- A-Za-z2,4
- A capital letter followed by at least 2 but not
more than 4 lowercase letters - 0-94
- 4 digits
- .3,8\..3
- 3 to 8 non-newline characters, followed by a dot
and 3 non-newline characters (presuming multiline
match mode is off)
25Convenience Expressions
- \d
- Any digit
- \D
- Any non-digit
- \s
- Any whitespace character (such as a space or tab)
- \S
- Any character other than a whitespace character
- \w
- Any number or letter
- \W
- Any character other than a number or letter
26Regular ExpressionsSample XML Code
- ltaddressesgt
- ltaddressgt
- ltnamegtRichard Muttlt/namegt
- ltaddrLine1gt30 Main St.lt/addrLine1gt
- ltaddrLine2gtNew Haven, CT 06460lt/addrLine2gt
- lt/addressgt
- ltaddressgt
- ltnamegtNanker Phelgelt/namegt
- ltaddrLine1gt1432 Broad St.lt/addrLine1gt
- ltaddrLine2gtPhoenix Arizonalt/addrLine2gt
- lt/addressgt
- ltaddressgt
- ltnamegtBilly Shearslt/namegt
- ltaddrLine1gt1 Grand View Crestlt/addrLine1gt
- ltaddrLine2gt Lansing, MI 22934-2234lt/addrLin
e2gt - lt/addressgt
- lt/addressesgt
27Regular Expressions SampleXSLT Code
- ltxsltemplate match"addrLine2"gt
- ltxslvariable name"elValue" select"."/gt
- ltxslanalyze-string select"elValue"
- regex"\s(.)\s,\s(A-Z2)\s(\d5
(\-\d4)?)\s"gt - ltxslmatching-substringgt
- ltcitygtltxslvalue-of select"regex-group(1)"/
gtlt/citygt - ltstategtltxslvalue-of select"regex-group(2)"
/gtlt/stategt - ltzipgtltxslvalue-of select"regex-group(3)"/gt
lt/zipgt - lt/xslmatching-substringgt
28Regular Expressions SampleXSLT Code
- ltxslnon-matching-substringgt
- ltaddrLine2gt
- ltxslvalue-of select"elValue"/gt
- lt/addrLine2gt
- lt/xslnon-matching-substringgt
- lt/xslanalyze-stringgt
- lt/xsltemplategt
29Regular Expression XSLT Tags
- To analyze a string, create an ltxslanalyze-string
/gt element. Attributes - select The string to analyze
- regex The pattern to which the string is
compared
30Regular Expression XSLT Tags
- ltxslmatching-substring /gt allows you to retrieve
and display substrings that match parenthesized
expressions within the pattern - Within this block, you can use the regex-group()
function to retrieve the value of a parenthesized
group - This function takes a single argument, the
backreference number
31Regular Expression XSLT Tags
- ltxslnon-matching-substring /gt contains code that
operates on the portions of the string that did
not match the parenthesized expressions in the
pattern
32Resource Used for Regular Expressions in XSLT 2.0
- Bob DuCharmes article at http//www.xml.com/pub/a
/2003/06/04/tr.html was the source for the code
shown
33Transclusion
- Transclusion is the act of dynamically including
data from another resource into the current
resource - Analogous to a server-side include in Web
development - Sample code using the document() function
- ltchapter xslversion"2.0"
- xmlnsxsl"http//www.w3.org/1999/XSL/Transf
orm"gt - lttitlegtdoc1.xmllt/titlegt
- ltpgtFirst paragraphlt/pgt
- ltxslcopy-of select"document('doc2.xml')/doc/p_at_
id'p2'"/gt - ltpgtLast paragraphlt/pgt
- lt/chaptergt
34Transclusion
- This code and a related article can be found at
http//www.xml.com/pub/a/2003/07/09/xslt.html
35Temporary Trees
- In XSLT 2.0, xslvariable, xslparam, and
xslwith-param can now be used to store temporary
XML fragments. Example - ltxslvariable name"genData"gt
- ltssheetMetadatagt
- ltfilenamegtprepdata.xsllt/filenamegt
- ltauthorgtBDlt/authorgt
- ltreleaseHistgt
- ltversion date"2003-10-12T1452"
fileSize"1543"gt1.2lt/versiongt - ltversion date"2003-09-11T1012"
fileSize"1322"gt1.1lt/versiongt - ltversion date"2003-07-24T0803"
fileSize"1134"gt1.0lt/versiongt - lt/releaseHistgt
- lt/ssheetMetadatagt
- lt/xslvariablegt
36Temporary Trees
- These variables can then be used in XPath
expressions - ltxsltemplate match"/"gt
- ltxslcall-template name"outputMetadata"gt
- ltxslwith-param name"revData"
select"genData"/gt - lt/xslcall-templategt
- ltxslapply-templates/gt
- lt/xsltemplategt
37Temporary Trees
- ltxsltemplate name"outputMetadata"gt
- ltxslparam name"revData"/gt
- ltxslcommentgt
- File name ltxslvalue-of select"revData/ss
heetMetadata/filename"/gt - Revision History
- ltxslfor-each select"revData/ssheetMetadat
a/releaseHist/version"gt - release ltxslvalue-of select"."/gt
- ltxsltextgt lt/xsltextgtltxslvalue-of
select"_at_date"/gt - lt/xslfor-eachgt
- average file size ltxslvalue-of
select"myavgFileSize(revData)"/gt - lt/xslcommentgt
- lt/xsltemplategt
38Temporary Trees
- ltxslfunction name"myavgFileSize"gt
- ltxslparam name"fileData"/gt
- ltxslvalue-of
- select"avg(fileData/ssheetMetadata/releas
eHist/version/_at_fileSize)"/gt - lt/xslfunctiongt
- ltxsltemplate match"_at_node()"gt
- ltxslcopygt
- ltxslapply-templates select"_at_node()"/gt
- lt/xslcopygt
- lt/xsltemplategt
39Temporary Trees
- The code for this section and a related article
covering temporary trees in more detail can be
found at http//www.xml.com/pub/a/2003/12/03/tr.ht
ml
40Recommended Resources
- xml.com A fabulous resource for all things XML,
including XSLT - The XPath 2.0 working draft http//www.w3.org/TR/
xpath20/ - The XSLT 2.0 working draft http//www.w3.org/TR/x
path20/ - Saxon http//saxon.sourceforge.net/
41Thanks for Joining Me!
- Please email questions to steveheckler_at_accelebrate
.com - Please visit http//www.accelebrate.com/present/
for a copy of this presentation and links to the
source code - Good luck with XSLT 2.0! ?