XSLT Training
XSLT Training
XSLT Training
Objectives
How XML documents may be rendered in browsers How the XSLT language transforms XML documents d t How XPath is used in XSLT
Using CSS
card name title email phone { { { { { background-color: #cccccc; border: none; width: 300;} display: block; font-size: 20pt; margin-left: 0; } display: block; margin-left: 20pt;} p y y p g p display: block; font-family: monospace; margin-left: 20pt;} display: block; margin-left: 20pt;}
the information cannot be rearranged information encoded in attributes cannot be exploited additional structure cannot be introduced
Using XSLT
<?xml stylesheet type="text/xsl" href businesscard.xsl ?> <?xml-stylesheet type text/xsl href="businesscard.xsl"?> <card xmlns="http://businesscard.org"> <name>John Doe</name> <title>CEO, Widget Inc.</title> <email>john.doe@widget.inc</email> <phone>(202) 555 1414 / h h (202) 555-1414</phone> <logo uri="widget.gif"/> </card>
XSLXSL-FO
XSLT was originally design to target XSL-FO XSL-FO (Formatting Objects) in an XML language for describing physical layout of texts Widely used in the graphics industry Not supported by any browsers yet
Overview
10
XSLT Stylesheets
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> ... </xsl:stylesheet>
XSLT is a domain-specific language for writing XML transformations (compare with e.g. JDOM) An XSLT stylesheet contains template rules Processing starts at the root node of the input document
An Introduction to XML and Web Technologies
11
Template Rules
<xsl:template match="..."> ... </xsl:template>
Find the template rules that match the context node Select the most specific one Evaluate the body (a sequence constructor)
12
13
Evaluation Context
A context item (a node in the source tree or an atomic value) A context position and size A set of variable bindings (mapping variable ) names to values) A function library (including those from XPath) A set of namespace d l ti t f declarations
14
15
16
17
Overview
18
Sequence Constructors
Element and attribute constructors Text constructors Copying nodes Recursive application Repetitions Conditionals Template invocation
19
Literal Constructors
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> xmlns "http://www w3 org/1999/xhtml" <xsl:template match="/"> <html> <head> <title>Hello World</title> </head> <body bgcolor="green"> <b>Hello World</b> b H ll W ld /b </body> </html> </xsl:template> </xsl:stylesheet>
An Introduction to XML and Web Technologies
20
Explicit Constructors
<xsl:stylesheet version= 2.0 version="2 0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:template match= / > match="/"> <xsl:element name="html"> <xsl:element name="head"> <xsl:element name="title"> name= title > Hello World </xsl:element> </xsl:element> <xsl:element name="body"> <xsl:attribute name="bgcolor" select="'green'"/> <xsl:element name="b"> name= b > Hello World </xsl:element> </xsl:element> </xsl:element> </xsl:template> </xsl:stylesheet>
An Introduction to XML and Web Technologies
21
22
23
Text Constructors
Literal text becomes character data in the output
here is some chardata
24
Recursive Application
The apply-templates element
finds some nodes using the select attribute applies the entire stylesheet to those nodes concatenates the resulting sequences
The default select value is child::node() Processing is often (but not necessarily!) a simple recursive traversal down the input XML tree
25
Student Data
<students> <student id 100026 > id="100026"> <name">Joe Average</name> <age>21</age> <major>Biology</major> <results> <result course="Math 101" grade="C-"/> <result course="Biology 101" grade="C+"/> <result course= Statistics 101 grade="D"/> course="Statistics 101" grade= D /> </results> </student> <student id="100078"> <name>Jack Doe</name> <age>18</age> <major>Physics</major> <major>XML Science</major> <results> <result course="Math 101" grade="A"/> <result course="XML 101" grade="A-"/> <result course="Physics 101" grade="B+"/> <result course="XML 102" grade="A"/> </results> </student> </students> An Introduction to XML and Web Technologies
<summary> y <grades id="100026"> <grade>C-</grade> g /g <grade>C+</grade> <grade>D</grade> </grades> g ades d 000 8 <grades id="100078"> <grade>A</grade> <grade>A-</grade> <grade>B+</grade> <grade>A</grade> </grades> </summary>
26
27
28
29
30
Repetitions
<xsl:stylesheet version= 2.0 version="2 0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="students"> <summary> <xsl:apply-templates select="student"/> </summary> </xsl:template> <xsl:template match="student"> <grades> <xsl:attribute name="id" select="@id"/> <xsl:for-each select=".//@grade"> <grade> <xsl:value-of select="."/> </grade> </xsl:for-each> </xsl:for each> </grades> </xsl:template> </xsl:stylesheet>
An Introduction to XML and Web Technologies
31
32
33
34
35
36
Sorting
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match students > match="students"> <enrolled> <xsl:apply-templates select="student"> <xsl:sort select="age" data-type="number" l l " " d " b " order="descending"/> <xsl:sort select="name"/> </xsl:apply-templates> </enrolled> </xsl:template> <xsl:template match="student"> <student name="{name}" age="{age}"/> </xsl:template> </xsl:stylesheet>
An Introduction to XML and Web Technologies
37
Copying Nodes
The copy-of element creates deep copies The copy element creates shallow copies Give top-most HTML lists square bullets:
<xsl:template match="ol|ul"> p <xsl:copy> <xsl:attribute name="style" select="'list-style-type: square;'"/> <xsl:copy-of select="*"/> </xsl:copy> </xsl:template>
38
An Identity Transformation
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/|@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> l l l l "@ | d ()"/ </xsl:copy> </xsl:template> / l t l t </xsl:stylesheet>
39
Overview
40
41
42
43
44
45
46
47
The Output
48
A Different View
<xsl:stylesheet version= 2.0 version="2 0" xmlns:rcp="http://www.brics.dk/ixwt/recipes" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match= rcp:collection > match="rcp:collection"> <nutrition> <xsl:apply-templates select="rcp:recipe"/> </nutrition> </xsl:template> <xsl:template match= rcp:recipe > match="rcp:recipe"> <dish name="{rcp:title/text()}" calories="{rcp:nutrition/@calories}" fat= {rcp:nutrition/@fat} fat="{rcp:nutrition/@fat}" carbohydrates="{rcp:nutrition/@carbohydrates}" protein="{rcp:nutrition/@protein}" alcohol= {if alcohol="{if (rcp:nutrition/@alcohol) then rcp:nutrition/@alcohol else '0%'}"/> </xsl:template> </xsl:stylesheet>
An Introduction to XML and Web Technologies
49
The Output
<nutrition> <dish name="Beef Parmesan with Garlic Angel Hair Pasta" calories="1167" fat="23%" carbohydrates="45%" protein="32%" alcohol="0%"/> <dish name="Ricotta Pie" calories="349" fat="18%" carbohydrates="64%" protein="18%" alcohol="0%"/> <dish name="Linguine Pescadoro" calories="532" fat="12%" carbohydrates="59%" protein="29%" alcohol="0%"/> <dish name="Zuppa Inglese" calories="612" fat="49%" carbohydrates="45%" protein="4%" alcohol="2%"/> <dish name="Cailles en Sarcophages" calories="8892" fat="33%" carbohydrates="28%" protein="39%" alcohol="0%"/> </nutrition>
50
A Further Stylesheet
<xsl:stylesheet version="2.0" xmlns= http://www.w3.org/1999/xhtml xmlns="http://www w3 org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="nutrition"> <html> <head> <title>Nutrition Table</title> </head> <body> <table border="1"> <tr> <th>Dish</th> <th>Calories</th> <th>Fat</th> <th>Carbohydrates</th> <th>Protein</th> </tr> <xsl:apply-templates select="dish"/> </table> </body> </html> </xsl:template> / l t l t <xsl:template match="dish"> <tr> <td><xsl:value-of select="@name"/></td> <td align= right ><xsl:value-of select= @calories /></td> align="right"><xsl:value-of select="@calories"/></td> <td align="right"><xsl:value-of select="@fat"/></td> <td align="right"><xsl:value-of select="@carbohydrates"/></td> <td align="right"><xsl:value-of select="@protein"/></td> </tr> / s te p ate </xsl:template> </xsl:stylesheet>
51
52
53
54
<xsl:template match="/"> <xsl:call-template name="fib"> l ll t l t "fib" <xsl:with-param name="n" select="10"/> / p </xsl:call-template> </xsl:template> </xsl:stylesheet>
55
Grouping
<xsl:stylesheet version="2.0" xmlns:rcp="http://www.brics.dk/ixwt/recipes" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> xmlns:xsl "http://www w3 org/1999/XSL/Transform" <xsl:template match="rcp:collection"> <uses> <xsl:for-each-group select="//rcp:ingredient" group-by="@name"> <use name="{current-grouping-key()}" count="{count(current-group())}"/> </xsl:for-each-group> / l f h </uses> </xsl:template> </xsl:stylesheet>
56
The Output
<uses> <use name="beef cube steak" count="1"/> <use name onion, sliced into thin rings count="1"/> name="onion, rings" count 1 /> <use name="green bell pepper, sliced in rings" count="1"/> <use name="Italian seasoned bread crumbs" count="1"/> <use name="grated Parmesan cheese" count="1"/> " d h " "1"/ <use name="olive oil" count="2"/> p g <use name="spaghetti sauce" count="1"/> <use name="shredded mozzarella cheese" count="1"/> <use name="angel hair pasta" count="1"/> <use name= minced garlic count="3"/> name="minced garlic" count= 3 /> ... </uses>
57
Numbering
<xsl:stylesheet version="2.0" xmlns:rcp="http://www.brics.dk/ixwt/recipes" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> p p g <xsl:template match="rcp:ingredient"> <rcp:ingredient> <xsl:attribute name="level"> p p g <xsl:number level="multiple" count="rcp:ingredient"/> </xsl:attribute> <xsl:apply-templates select="@*|*"/> / p g </rcp:ingredient> </xsl:template> p <xsl:template match="@*"> <xsl:copy/> </xsl:template> <xsl:template match="*"> <xsl:copy><xsl:apply-templates/></xsl:copy> / s te p ate </xsl:template> </xsl:stylesheet>
An Introduction to XML and Web Technologies
58
Functions
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:local http://www.w3.org/2004/07/xquery local functions > xmlns:local="http://www.w3.org/2004/07/xquery-local-functions"> <xsl:function name="local:fib"> <xsl:param name="n"/> <xsl:value-of select="if ($n le 1) l l f l "if ($ l then 1 else local:fib($n -1)+local:fib($n -2)"/> </xsl:function> <xsl:template match= / > match="/"> <xsl:value-of select="local:fib(10)"/> </xsl:template> </xsl:stylesheet>
59
60
61
62
63
64
Including a Stylesheet
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="howabout"> <answer> I don't like <xsl:value-of select="text()"/> </answer> </xsl:template> </xsl:stylesheet> <xsl:stylesheet version="2.0" xmlns:xsl= http://www.w3.org/1999/XSL/Transform > xmlns:xsl "http://www w3 org/1999/XSL/Transform"> <xsl:include href="negative.xsl"/> <xsl:template match="*"> <answer> I'm crazy for <xsl:value-of select="text()"/> </answer> </xsl:template> </xsl:stylesheet>
65
Importing a Stylesheet
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="howabout"> <answer> I don't like <xsl:value-of select="text()"/> </answer> </xsl:template> </xsl:stylesheet> <xsl:stylesheet version="2.0" xmlns:xsl= http://www.w3.org/1999/XSL/Transform > xmlns:xsl "http://www w3 org/1999/XSL/Transform"> <xsl:import href="negative.xsl"/> <xsl:template match="*"> <answer> I'm crazy for <xsl:value-of select="text()"/> </answer> </xsl:template> </xsl:stylesheet>
66
67
68
69
70
71
72
73
74
75
76
77
78