Chungnam National UniversityDataBase System Lab.
임형준 ([email protected])충남대학교 컴퓨터공학과
데이터베이스 시스템 연구실23-4-10 월요일
What’s New in XSLT 2.0
Chungnam National UniversityDataBase System Lab.
Contents XSLT Status XSLT Overview New Feature of XSLT 2.0
Schema Support Grouping Functions Types Regular Expressions Temporary Trees Sequences Character Maps
Conclusion References
2
Chungnam National UniversityDataBase System Lab.
XSLT Status
XSLT 1.0 XSL Transformations (XSLT) Version 1.0 W3C, http://www.w3.org/TR/1999/REC-xslt-19991116 Published W3C Recommendation : 1999-11-16
XSLT 2.0 XSL Transformations (XSLT) Version 2.0 W3C,
http://www.w3.org/TR/2007/REC-xslt20-20070123/ Published W3C Recommendation : 2007-01-23
3
Chungnam National UniversityDataBase System Lab.
XSLT Overview
A language for transforming XML documents
The results can be: Same XML document type with filtered data Another XML document type Other text formats
HTML WML Delimited text, etc.
XSL contains the languages: XPath: expressions for selecting elements in a
document XSLT (XSL Transformations) Other languages e.g. XLink, Xpointer
4
Chungnam National UniversityDataBase System Lab.
Features of XPath
Identifies particular parts in XML documents
Expressions along the tree structure of XML documents
Examples: child::chapter
Selects all subordinated elements of chapter child::*[self::chapter]
Selects all chapter elements of the context element id(´a23´)/child::chapter[position()=5]
Selects the fifth child-element with the name chapter of the element with the unified ID a23
5
Chungnam National UniversityDataBase System Lab.
Features of XSLT
Tree-oriented transformation language based on XML
Uses XPath expressions for: Conditional parsing: if / while / test / ... Selecting parts of the document: match / select /
value-of / template / ... Manipulation of strings and numbers
Vocabulary for specifying formating: e.g. sorting
Variable output formats: e.g. xml, html, plain text, ...
6
Chungnam National UniversityDataBase System Lab.
XSL Transformation
7
XSLT(eXtensible Stylesheet
Language Transformations)
Input
XML Document
Converting Rules:Stylesheet(XSL)
XPath
XML Document
HTML
WML
Plain Text
Output
…
Chungnam National UniversityDataBase System Lab.
Example of a XSLT
8
<?xml version="1.0" ?>
<!-- hello.xml -->
<?xml:stylesheet type="text/xsl" href="hello.xsl"?>
<myMessage> <message>Hello XSLT!</message></myMessage>
<html><body><h1>Hello XSLT!</h1></body></html>
<?xml version="1.0" ?>
<!-- hello.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="myMessage"> <html><body> <h1><xsl:value-of select="message"/></h1> </body></html> </xsl:template>
</xsl:stylesheet>
Chungnam National UniversityDataBase System Lab.
Schema Support Grouping Functions Types Regular Expressions Temporary Trees Sequences Character Maps
New Feature of XSTL 2.0
Chungnam National UniversityDataBase System Lab.
Schema Support Need to import schemas to use:
user-defined types substitution groups
Import with <xsl:import-schema> namespace identifies target namespace schema-location locates schema
In order to refer to additional types, you must import the schema that defines them:
Must specify at least the namespace or the schema location, if not both
10
<xsl:import-schemanamespace="http://nwalsh.com/xmlns/extreme2004/recipes/"schema-location="recipes.xsd"/>
Chungnam National UniversityDataBase System Lab.
Implications for XSLT 2.0 Use
Well-designed schemas become a useful tool Substitution groups and appropriate types help
reduce number of templates Check whether the result conforms to the schema
while generating it
11
Chungnam National UniversityDataBase System Lab.
Grouping
XSLT 1.0 allowed sorting, but not SQL-style grouping XSLT 2.0 provides a new, flexible grouping instruction
for grouping <xsl:for-each-group /> 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
12
Chungnam National UniversityDataBase System Lab.
Grouping
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 Grouping by key(Example)
13
<cities><city name="Milano" country="Italia"/><city name="Paris" country="France"/><city name="Munchen" country="Deutschland"/><city name="Lyon" country="France"/><city name="Venezia" country="Italia"/></cities>
<xsl:for-each-group select="cities/city"group-by="@country"><tr><td><xsl:value-of select="position()"/></td><td><xsl:value-of select="@country"/></td><td><xsl:value-of select="current-group()/@name"separator=", "/></td></tr></xsl:for-each-group>
<tr><td>1</td> <td>Italia</td><td>Milano, Venezia</td></tr><tr><td>2</td> <td>France</td><td>Paris, Lyon</td></tr>
Chungnam National UniversityDataBase System Lab.
Implications for XSLT 2.0 Use
Much easier to convert from flat to hierarchical structures Processing XHTML to DocBook (or XHTML 2.0) Easier to create indexes Easier to create summaries/roll-ups Easier to create paginated documents
14
Chungnam National UniversityDataBase System Lab.
Functions
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
15
Chungnam National UniversityDataBase System Lab.
Functions
<xsl:function>…</xsl:function> 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 xsl:param elements in the function definition)
Content of the function: 0 or more <xsl:param /> elements that define incoming
parameters Code to execute – final value can be returned via
<xsl:value-of />, <xsl:sequence />, or another expression
16
Chungnam National UniversityDataBase System Lab.
Function Example
17
<xsl:function name="str:reverse" as="xs:string"><xsl:param name="sentence" as="xs:string"/><xsl:sequence select=“
if (contains($sentence, ' '))then concat(str:reverse(
substring-after($sentence, ' ')), ' ',substring-before($sentence, ' '))
else $sentence"/></xsl:function>
<xsl:function name="str:align"><xsl:param name="string" /><xsl:sequence select="str:reverse(‘Database System')" />
</xsl:function>
Chungnam National UniversityDataBase System Lab.
Implications for XSLT 2.0 Use
General replacement for named templates
Particular use where XSLT code can't be used: creating a value to sort by using <xsl:sort> creating a value to index by using <xsl:key> creating a value to group by using <xsl:for each-
group> carrying out complex tests on nodes, for use in
match patterns in templates or keys
18
<xsl:template match=“\S.*"> ...</xsl:template>
Chungnam National UniversityDataBase System Lab.
Types
Strong Typing in XSLT 2.0 The type of a sort key determines how values are
sorted If the wrong type of value is passed as a parameter
to a template, you will get an error XSLT 2.0 allows you to declare:
The type of variables The return type of templates The type of sequences (constructed with
xsl:sequence) The return type of (user-declared) functions Both the type and required type of parameters
19
Chungnam National UniversityDataBase System Lab.
Declaring Types of Variables
Declare the type of variables and parameters with as attribute
20
<xsl:variable name="i" select="1"/>is the integer value 1
<xsl:variable name="fp" select="1“ as="xs:double"/>is the double value 1.0
<xsl:variable name="date” select="'2003-11-20'">is the string “2003-11-20”
<xsl:variable name="date” select="xs:date('2003-11-20')"/>is the date November 20, 2003
<xsl:variable name="date" as="xs:date” select="'2003-11-20'"/>is an error
Chungnam National UniversityDataBase System Lab.
Implications for XSLT 2.0 Use
Easy to get errors from a stylesheet unless you're rigorous in keeping track of types Declare types of variables and parameters Cast elements/attributes to particular types
21
Chungnam National UniversityDataBase System Lab.
Regular Expressions
Within XSLT 2.0, there are three key functions that use regular expressions: Tokenize(string, regex, flags?): Lets you split a
string into pieces at each point where a delimiter or pattern occurs
matches(string, regex, flags?): Returns a boolean with whether the string in the first argument contains the pattern in the second argument
Replace(string, regex, replacement, flags?): Replaces the string or pattern in the first argument with the string in the second argument
22
Chungnam National UniversityDataBase System Lab.
Regular Expression Instructions
XSLT 2.0 has <xsl:analyze-string> instruction Uses regular expressions to apply markup to a string select attribute selects string regex The pattern to which the string is compared string split into a sequence of matching and non-
matching substrings processed in turn by:
<xsl:matching-substring> for matching <xsl:non-matching-substring> for non-matching
23
Chungnam National UniversityDataBase System Lab.
Example String Analysis
24
<poem> Mary had a little lamb, Its fleece was white as snow; And everywhere that Mary went The lamb was sure to go.</poem>
<xsl:template match="poem"> <poem> <xsl:analyze-string select="." regex="\S.*" flags="m"> <xsl:matching-substring> <line><xsl:value-of select="." /></line> </xsl:matching-substring> </xsl:analyze-string> </poem></xsl:template><poem>
<line>Mary had a little lamb,</line> <line>Its fleece was white as snow;</line> <line>And everywhere that Mary went</line> <line>The lamb was sure to go.</line></poem>
Chungnam National UniversityDataBase System Lab.
Implications for XSLT 2.0 Use
Potential to process any text format comma-delimited and fixed-format files CSS files C#, Java code?
These are hard because of matching tags/braces Addition of regular expressions and XSD type support,
it now becomes possible for an XSLT 2.0 transformer to read something like a Java or C# header
Possible to parse non-comformant HTML files and convert them into valid XHTML.
25
Chungnam National UniversityDataBase System Lab.
Transclusion
Transclusion is the act of dynamically including data from another resource into the current resource
Sample code using the document() function:
26
<chapter xsl:version="2.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<title>doc1.xml</title><p>First paragraph</p><xsl:copy-of
select="document('doc2.xml')/doc/p[@id='p2']"/><p>Last paragraph</p>
</chapter>
Chungnam National UniversityDataBase System Lab.
Temporary Trees
XSLT 1.0 had Result Tree Fragments Result trees are read-only. Created when use content of <xsl:variable> Tree that couldn't be accessed with location path Most processors have xxx:node-set() extension
function Convert result tree fragment to node tree
In XSLT 2.0, have temporary trees Can copy in same way as RTFs Can access without using extension function Provides a <xsl:result-document> instruction to
create multiple result documents Provides support for validation
27
Chungnam National UniversityDataBase System Lab.
Example Temporary Tress
28
<xsl:variable name="menus"> <menu name="File"> <menuItem name="New..." shortcut="Ctrl-N" /> <menuItem name="Open..." shortcut="Ctrl-O" /> <menuItem name="Save..." shortcut="Ctrl-S" /> ... </menu> ...</xsl:variable>
<xsl:value-of select="exsl:node-set($menus)/menu /menuItem[@shortcut = $shortcut]/@name" />
<xsl:value-of select="$menus/menu /menuItem[@shortcut = $shortcut]/@name" />
XSLT 1.0
XSLT 2.0
Chungnam National UniversityDataBase System Lab.
Implications for XSLT 2.0 Use
Break up complex transformations into several steps Filter, sort, annotate nodes in early steps Later steps are easier to write
Create lookup tables Translate from codes or numbers to labels Similar to arrays or matrices
Iteratively process a document until it fulfils some test Add content until a document is valid
29
Chungnam National UniversityDataBase System Lab.
Sequences
Every sequence of instructions creates a sequence of items
When a sequence is added to a node: Atomic values are converted to text nodes
Spaces added between atomic values Nodes are copied to create children
30
sequence of anyitems
sequence of new nodes
children of node
Chungnam National UniversityDataBase System Lab.
Sequences
The <xsl:sequence> instruction is used to construct sequences of nodes or atomic values (or both) Adds existing nodes or new atomic values to a
sequence <xsl:sequence select='(1,2,3,4)'/>
returns a sequence of integers. <xsl:sequence select='(1,2,3,4)‘ as="xs:double"/>
returns a sequence of doubles.
31
Chungnam National UniversityDataBase System Lab.
Using Sequences in XSLT 2.0
Iterate over a sequence
Create a text node from a sequence
32
<xsl:for-each select="1 to 3"> <tr><td colspan="4" /></tr></xsl:for-each>
<tr><td colspan="4" /></tr><tr><td colspan="4" /></tr><tr><td colspan="4" /></tr>
<xsl:value-of select="author/surname" separator=", " />
Thompson, Tobin
Chungnam National UniversityDataBase System Lab.
Implications for XSLT 2.0 Use
Less need for recursive templates Use integer sequences to iterate a number of times Use <xsl:sequence> to build node sequences by
iteration rather than recursion Less need for temporary elements
Atomic values don't need to be wrapped in an element in order to be passed around in a list
33
Chungnam National UniversityDataBase System Lab.
Character Maps
Character maps give you greater control over serialization. They map a Unicode character to any string in the serialized document For XML and HTML output methods, the resulting
character stream does not have to be well-formed The mapping occurs only at serialization: it is not
present in result tree fragments This facility can be used instead of “disabled output
escaping” in most cases
34
Chungnam National UniversityDataBase System Lab.
Conclusion
XSLT 2.0 introduces a lot of new features Many stylesheets can be simpler:
multi-step processing with temporary trees Grouping using <xsl:for-each-group> user-defined functions for repetitive code
Stylesheet applications can be simpler: Multiple result documents should reduce need for
client-side scripting XSLT 2.0 expands into text parsing Using schemas/types well will make
things easier; not using them will make things harder
35
Chungnam National UniversityDataBase System Lab.
References
W3C, “XSL Transformations (XSLT)Version 1.0” http://www.w3.org/TR/1999/REC-xslt-19991116, 16, November. 1999
W3C, “XSL Transformations (XSLT)Version 2.0” http://www.w3.org/TR/2007/REC-xslt20-20070123/, 12, January. 2007
Steve Heckler, XSLT20.ppt, Accelebrate Kurt Cagle, “Reevaluating XSLT 2.0”, 9, March.
2007
36