introduction to xslt
DESCRIPTION
Introduction to XSLT. Cheng-Chia Chen. XSL. XSL Processor. XML. HTML (or XML or text). What is XSLT ?. XSLT is a language for transforming XML documents into other [XML] documents . XML syntax Possible output formats: XML HTML TEXT. Outlines. - PowerPoint PPT PresentationTRANSCRIPT
Introduction to XSLT
Transparency No. 2
What is XSLT ?
XSLT is a language for transforming XML documents into other [XML] documents. XML syntax
Possible output formats: XML HTML TEXT
XSL Processor
XSL
XML HTML (or XML or text)
Introduction to XSLT
Transparency No. 3
Outlines
Transform XML Documents into HTML Documents
Transform XML Documents into other XML Documents
Transform XML Documents into Texts.
Note: Most slide materials adapted from Roger L. Costello
Introduction to XSLT
Transparency No. 5
HTML Generation
Use XSL to generate HTML documents.XSLT viewed as a tool to fill in the content of an
HTML document with data extracted from an XML Document. XSLT provides elements (tags) for extracting the
XML data
Introduction to XSLT
Transparency No. 6
Enhancing HTML Documents with XML Data
XML Document
HTML Document
(with embeddedXSL elements)
XSL element
XML data
Introduction to XSLT
Transparency No. 7
Enhancing HTML Documents with the Following XML Data
<?xml version="1.0"?><FitnessCenter> <Member level="platinum"> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member></FitnessCenter>
FitnessCenter.xml
Introduction to XSLT
Transparency No. 8
HTML Document in an XSL Template
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/>
<xsl:template match="/"> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY> Welcome! </BODY> </HTML> </xsl:template>
</xsl:stylesheet>
Literal result elements are
elements in templates
not from xsl namespace.
htmlEx1
Introduction to XSLT
Transparency No. 9
Note
The HTML is embedded within an XSL template, which is an XML document. Consequently, the embedded HTML must be well formed, e.g., every start tag must have an end tag
We can add XSL elements to the embedded HTML, which allows us to extract data out of XML documents.
Introduction to XSLT
Transparency No. 10
Extracting the Member Name
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/>
<xsl:template match="/"> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY> Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>! </BODY> </HTML> </xsl:template>
</xsl:stylesheet>htmlEx2
Introduction to XSLT
Transparency No. 11
Extracting a Value from an XML Document,Navigating the XML Document
Extracting values: use the <xsl:value-of select="…"/> XSL element
Navigating: … is an XPath expression with matched node as context n
ode. Recall what the location path : /FitnessCenter/Member/Name means: "Start from the top of the XML document, go to the FitnessCenter element, from th
ere go to the Member element, and from there go to the Name element."
Introduction to XSLT
Transparency No. 12
The tree view of fitnessCenter.xml
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementPhone
ElementPhone
ElementFavoriteColor
... ...
TextJeff
Text555-1234
Text555-4321
Textlightgrey
...
Introduction to XSLT
Transparency No. 13
Extract the FavoriteColor and use it as the bgcolor
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/>
<xsl:template match="/"> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD>
<BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}"> Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>! </BODY> </HTML> </xsl:template>
</xsl:stylesheet>htmlEx3
Introduction to XSLT
Transparency No. 14
Notes
Attribute values cannot contain "<" nor ">“ The following is NOT valid: <Body bgcolor="<xsl:value-of select='/FitnessCenter/Member/FavoriteColor'/>">
To extract the value of an XML element and use it as an attribute value you must use curly braces:
<Body bgcolor="{/FitnessCenter/Member/FavoriteColor}">
1. Evaluate the expression within the curly braces.
2. Assign the value to the attribute.
3. The place of an attribute value where we can put xpath expr within curly braces is called an attribute value template(avt).
Introduction to XSLT
Transparency No. 15
Extract the Home Phone Number
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/>
<xsl:template match="/"> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}"> Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>! <BR/> Your home phone number is:<xsl:value-of select="/FitnessCenter/Member/Phone[@type='home']"/
> </BODY> </HTML> </xsl:template>
</xsl:stylesheet> htmlEx4
Introduction to XSLT
Transparency No. 16
Note
In this example we want "the Phone element where the valueof its type attribute equals 'home' ":
<xsl:value-of select="/FitnessCenter/Member/Phone[@type='home']"/>
The expression within […] is called a "predicate". Its purposeis to filter.
Note the use of the single quotes within the double quotes. select=" … ' …' …"
Introduction to XSLT
Transparency No. 17
Review - HTML Table
<table border=“1” width=“100%”>
</table>
<tr>
<tr>
<tr> </tr>
</tr>
</tr><th> </th>
<th> </th>
<th> </th>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
This will create a table with 3 rows - the first row contains a headerfor each column. The next two rows contains the table data.
Introduction to XSLT
Transparency No. 18
<table border=“1” width=“75%”> <tr> <th>Fruit</th> <th>Color</th> </tr> <tr> <td>Apple</td> <td>Red</td> </tr> <tr> <td>Banana</td> <td>Yellow</td> </tr></table>
Fruit Color
Apple Red
Banana Yellow
Introduction to XSLT
Transparency No. 19
Create a Table of Phone Numbers
Suppose that a Member has an arbitrary number of phone numbers (home, work, cell, etc).
Create an HTML table comprised of the phone numbers. On each row of the table put the type (home, work, cell, etc) in one column and the actual phone number in the next column.
Introduction to XSLT
Transparency No. 20
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}"> Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>! <BR/> Your phone numbers are: <TABLE border="1" width="25%"> <TR><TH>Type</TH><TH>Number</TH></TR> <xsl:for-each select="/FitnessCenter/Member/Phone"> <TR> <TD><xsl:value-of select="@type"/></TD> <TD><xsl:value-of select="."/></TD> </TR> </xsl:for-each> </TABLE> </BODY> </HTML> </xsl:template> </xsl:stylesheet>
context node is the
currently processed
phone node
htmlEx5
Introduction to XSLT
Transparency No. 21
Iterating through XML Elements
<xsl:for-each select="/FitnessCenter/Member/Phone">
<!- - Within here we are at one of the Phone elements. Thus, in <xsl:value-of select="path", the value for path is relative to where we are in the XML document. The "." refers to the Phone element that we are currently positioned at. - ->
</xsl:for-each>
Introduction to XSLT
Transparency No. 22
Special Offer to Platinum Members
Enhance the example to provide a special offer to "platinum" members.
We need to check if the "level" attribute on the Member element equals "platinum".
Introduction to XSLT
Transparency No. 23
<HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}"> Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>! <BR/>
<xsl:if test="/FitnessCenter/Member/@level='platinum'"> Our special offer to platinum members today is ... <BR/> </xsl:if> Your phone numbers are: <TABLE border="1" width="25%"> <TR><TH>Type</TH><TH>Number</TH></TR> <xsl:for-each select="/FitnessCenter/Member/Phone"> <TR> <TD><xsl:value-of select="@type"/></TD> <TD><xsl:value-of select="."/></TD> </TR> </xsl:for-each> </TABLE></BODY> </HTML>
htmlEx6
Introduction to XSLT
Transparency No. 24
Conditional Processing
Use the <xsl:if test="…“> IfTruePart <…/> element to perform conditional processing.
Note: It is needless to add in the value of test attribute extra { and }, which must appear in
<Body bgcolor="{/FitnessCenter/Member/FavoriteColor}">
Introduction to XSLT
Transparency No. 25
Accessing Multiple Parts of the XML Document
Enhance the table to contain three columns: the name of the Member, the type of the phone (home, work, cell, etc), and the actual phone number.
Introduction to XSLT
Transparency No. 26
<HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}"> Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>! <BR/> <xsl:if test="/FitnessCenter/Member/@level='platinum'"> Our special offer to platinum members today is ... <BR/> </xsl:if> Your phone numbers are: <TABLE border="1" width="25%"> <TR><TH>Name</TH><TH>Type</TH><TH>Number</TH></TR> <xsl:for-each select="/FitnessCenter/Member/Phone"> <TR>
<TD><xsl:value-of select="../Name"/></TD> <TD><xsl:value-of select="@type"/></TD> <TD><xsl:value-of select="."/></TD> </TR> </xsl:for-each> </TABLE> </BODY> </HTML>
htmlEx7
Introduction to XSLT
Transparency No. 27
Getting the Name when accessing the Phone
Member
Phone
555-1234
Phone
555-4321
Name
Jeff
Notice how we accessthe Name with respect to the Phone elementVia the location path../Name
•We can access elements in other parts of the XML tree via the “../” operator.
..Name
Introduction to XSLT
Transparency No. 28
Other ways to Access the XML Data
<xsl:value-of select="/FitnessCenter/Member[1]/Name"/>"Select the Name of the first Member"
<xsl:value-of select="/FitnessCenter/Member[position()=1]/Name"/>"Select the Name of the first Member"
<xsl:value-of select="/FitnessCenter/Member[last()]/Name"/>"Select the Name of the last Member"
<xsl:for-each select=" FitnessCenter/Member[not(last())]"> <!- - Process all Members but the last - -></xsl:for-each>
Note: Assume that there are multiple Members
Introduction to XSLT
Transparency No. 29
Other ways to Access the XML Data (cont.)
<xsl:for-each select="/FitnessCenter/Member[position() >1]"> <!- - Process all Members but the first - -></xsl:for-each>
<xsl:for-each select="/FitnessCenter//Name"> <!- - Process all Name elements which have FitnessCenter as an ancestor - -></xsl:for-each>
Introduction to XSLT
Transparency No. 30
Enhanced XML Document<?xml version="1.0"?><FitnessCenter> <Member id="1" level="platinum"> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member> <Member id="2" level="gold"> <Name>David</Name> <Phone type="home">383-1234</Phone> <Phone type="work">383-4321</Phone> <FavoriteColor>lightblue</FavoriteColor> </Member> <Member id="3" level="platinum"> <Name>Roger</Name> <Phone type="home">888-1234</Phone> <Phone type="work">888-4321</Phone> <FavoriteColor>lightyellow</FavoriteColor> </Member></FitnessCenter>
Note that each Membernow has a unique id(the id attribute)
fitnessCenter-2.xml
Introduction to XSLT
Transparency No. 31
Review - HTML Hyperlinking
<A name="Anchor2" />
…
<A href="#Anchor2">Click Here</A>...
This creates an internal hyperlink (the source anchor links tothe target anchor).
Introduction to XSLT
Transparency No. 32
Hyperlink Name to Home Phone
Problem: 1. create an HTML document that has two tables:
- a Member Name table, and
- a Member home Phone number table.
2. Hyperlink the Member's Name to his/her Phone.
Introduction to XSLT
Transparency No. 33
<TABLE border="1" width="25%"><TR><TH>Name</TH></TR> <xsl:for-each select="/FitnessCenter/Member"> <TR> <TD> <A href="#{@id}"> <xsl:value-of select="Name"/> </A> </TD> </TR> </xsl:for-each> </TABLE> <BR/><BR/><BR/><BR/><BR/> <TABLE border="1" width="25%"> <TR><TH>Home Phone Number</TH></TR> <xsl:for-each select="/FitnessCenter/Member"> <TR> <TD> <A name="{@id}"> <xsl:value-of select="Phone[@type='home']"/> </A> </TD> </TR> </xsl:for-each> </TABLE> htmlEx8
Introduction to XSLT
Transparency No. 34
Numbering
There is an XSL element that returns a number corresponding to the element's position in the set of selected nodes
<xsl:for-each select="/FitnessCenter/Member"> <xsl:number value="position()" format="1"/> <xsl:text>.</xsl:text> <xsl:value-of select="Name"/> <BR/></xsl:for-each>
Output:1. Jeff2. David3. Roger
htmlEx9
Introduction to XSLT
Transparency No. 35
Start Numbering from 0
How would you start the numbering from zero, rather than one?
<xsl:number value="position() - 1" format="1">
Introduction to XSLT
Transparency No. 36
format attribute of xsl:number
With the format attribute we can specify the format of the generated number, i.e., 1, 2, 3 or I, II, III, or A, B, C, or A.1, A.2, A.3, or … format=“1” generates the sequence: 1, 2, 3, … format=“01” generates: 01, 02, 03, … format=“A” generates: A, B, C, … format=“a” generates: a, b, c, … format=“I” generates: I, II, III, … format=“i” generates: i, ii, iii, ...
Introduction to XSLT
Transparency No. 37
format attribute of xsl:number
<xsl:for-each select="/FitnessCenter/Member"> <xsl:number value="position()" format="A"/> <xsl:text>. </xsl:text> <xsl:value-of select="Name"/> <BR/></xsl:for-each>
Output:A. JeffB. DavidC. Roger
Introduction to XSLT
Transparency No. 38
Sorting
There is an XSL element that sorts the elements that you extract from the XML document
<xsl:for-each select="/FitnessCenter/Member"> <xsl:sort select="Name" order="ascending"/> <xsl:value-of select="Name"/> <BR/></xsl:for-each>
"For each Member, sort the Name elements" Output:DavidJeffRoger htmlEx10
Introduction to XSLT
Transparency No. 39
Sorting
Note:1. Sort occurs before iteration.2. I.e. The set of Member elements selected by xsl:for-each issorted using the Name child element. This occurs priorto the first iteration of the loop.. After the set of Memberelements are sorted then the looping begins
<xsl:for-each select="/FitnessCenter/Member"> <xsl:sort select="Name" order="ascending"/> <xsl:value-of select="Name"/> <BR/></xsl:for-each>
Introduction to XSLT
Transparency No. 40
concat() function
concat(destination string, strings to add)
Note: if you want to concatenate more than one string to the destination string then simply add more arguments
Introduction to XSLT
Transparency No. 41
<xsl:for-each select="/FitnessCenter/Member"> <xsl:value-of select="concat('Welcome ', Name, '!')"/> <BR/></xsl:for-each>
Output:Welcome Jeff!Welcome David!Welcome Roger!
htmlEx11
Introduction to XSLT
Transparency No. 42
xsl:variable
This XSL element allows you to create a variable to hold a value (which could be a string or a subtree of the XML document).
The variable is referenced by $variable-name
<xsl:variable name=“hello” select=“'Hello World'”/>
This creates a variable called hello, that has a value which is the literal string, ‘Hello World’.
We could use this variable as follows:
Value = <xsl:value-of select=“$hello”/>
This will output:
Value = Hello World
Introduction to XSLT
Transparency No. 43
Member's Phone Numbers: <TABLE border="1" width="25%"> <TR><TH>Name</TH><TH>Type</TH><TH>Number</TH></TR> <xsl:for-each select="/FitnessCenter/Member"> <xsl:variable name="name" select="Name"/> <xsl:for-each select="Phone"> <TR> <TD><xsl:value-of select="$name"/></TD> <TD><xsl:value-of select="@type"/></TD> <TD><xsl:value-of select="."/></TD> </TR> </xsl:for-each> </xsl:for-each></TABLE>
htmlEx12
Introduction to XSLT
Transparency No. 44
xsl:variable
<xsl:variable name=“member” select=“Member[1]”/>
This creates a variable called member, that has a value which is a subtree.
We could use this variable as follows:
Name = <xsl:value-of select=“$member/Name”/>Home Phone = <xsl:value-of select=“$member/Phone[@type='home']”/>
This will result in generating:
Name = JeffHome Phone = 555-1234
Member
...Name Phone Phone
Jeff 555-1234 555-4321
Introduction to XSLT
Transparency No. 45
xsl:variable
A variable is “write once, read many”. That is, you can assign a variable a value only once,
but then you can retrieve the value of the variable many times.
A variable declaration is visible to the following siblings and its descendants. This region is the scope of the variable binding.
Introduction to XSLT
Transparency No. 46
The name variable's scope
Member's Phone Numbers: <TABLE border="1" width="25%"> <TR><TD>Name</TD><TD>Type</TD><TD>Number</TD></TR> <xsl:for-each select="/FitnessCenter/Member"> … <xsl:variable name="name" select="Name"/> <xsl:for-each select="Phone"> <TR> <TD><xsl:value-of select="$name"/></TD> <TD><xsl:value-of select="@type"/></TD> <TD><xsl:value-of select="."/></TD> </TR> </xsl:for-each> </xsl:for-each></TABLE>
Introduction to XSLT
Transparency No. 47
Global Variables
Are variables declared outside of template elements <xsl:template match="/">.
<?xml version="1.0"?><xsl:stylesheet mlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:variable name="pi" select="'3.142857142857'"/> <xsl:template match="/" > <HTML><HEAD><TITLE>Value of Pi</TITLE> </HEAD> <BODY> The value of pi = <xsl:value-of select="$pi"/> </BODY> </HTML> </xsl:template> …</xsl:stylesheet>
Introduction to XSLT
Transparency No. 48
Problem
Create a variable, names, to contain a list of the Member Names, with each name separated by a slash.
How would you create such a variable?
First attempt:
Member's Names:<xsl:variable name="names" select="/FitnessCenter/Member[1]/Name"/><xsl:for-each select="/FitnessCenter/Member[position() > 1]"> <xsl:variable name="names" select="concat($names, '/')"/> <xsl:variable name="names" select="concat($names, Name)"/> </xsl:for-each><xsl:value-of select="$names"/>
Output:
A parameter or variable with the same
name already exists in the current scope. htmlEx13
Introduction to XSLT
Transparency No. 49
Problem - Solution
In all previous examples of creating a variable we declared the name of the variable and then had a select attribute which gave the variable its value. We can omit the select attribute:
<xsl:variable name=“names”> - Do stuff in here. All output will go into the names “box”.</xsl:variable>
Introduction to XSLT
Transparency No. 50
Problem - Solution
Member's Names:<xsl:variable name="names"> <xsl:value-of select="/FitnessCenter/Member[1]/Name"/> <xsl:for-each select="/FitnessCenter/Member[position() > 1]"> <xsl:text>/</xsl:text> <xsl:value-of select="Name"/> </xsl:for-each></xsl:variable><xsl:value-of select="$names"/>
Output:
Member's Names: Jeff/David/Roger htmlEx15
Introduction to XSLT
Transparency No. 51
contains() function
contains(string to be tested, test string) returns true if string to be tested contains test string
<xsl:if test=“contains($greeting, ‘welcome’)”> $greeting contains ‘welcome’</xsl:if>
Introduction to XSLT
Transparency No. 52
xsl:choose
xsl:choose allows you to elegantly express multiple conditional tests. Here’s the structure:
<xsl:choose> <xsl:when test='something’> [action] </xsl:when> <xsl:when test='something'> [action] </xsl:when> <xsl:otherwise> [action] </xsl:otherwise></xsl:choose>
The first xsl:when statement thatevaluates to true is executed. Ifnone evaluates to true then thexsl:otherwise statement is executed.
Introduction to XSLT
Transparency No. 53
substring-before() String Function
Here’s the form of this string function:
substring-before(string, pattern)
Example:
<xsl:variable name="phone" select="Phone"/><xsl:value-of select="substring-before($phone, '-')"/>
“Get the contents of Phone and put it into the variablecalled ‘phone’. Then extract from the content of ‘phone’ the stringbefore the '-' (i.e., the telephone exchange)”.
555-1234substring-before($phone, ‘-’)
555
phone
Introduction to XSLT
Transparency No. 54
substring-after() String Function
Here’s the form of this string function:
substring-after(string, pattern)
Example:
<xsl:variable name="phone" select="Phone"/><xsl:value-of select="substring-after($phone, '-')"/>
“Get the contents of Phone and put it into the variablecalled ‘phone’. Then extract from the content of ‘phone’ the stringafter the '-'”.
555-1234substring-after($phone, ‘-’)
1234
phone
Introduction to XSLT
Transparency No. 55
starts-with() String Function
Here’s the form of this string function:
starts-with(string, pattern)
Example:
<xsl:if test="starts-with(Phone, '555')"> [action]</xsl:if>
“If the Phone starts with the string, ‘555’ then do [action]”.
Introduction to XSLT
Transparency No. 56
substring() function
substring(string, i, len?) returns the substring of string that starts at the ith position and has length, len.
The length argument (len) is optional. If not present then this function returns the substring starting at the ith position all the way to the end of the string.
Note: the first character is at position 1 (not 0 as with some languages)
substring(‘1234567890’, 2, 5) returns ‘23456’
Introduction to XSLT
Transparency No. 57
string-length() function
string-length(string) returns the length of the string
string-length(‘1234567890’) returns 10
Introduction to XSLT
Transparency No. 58
translate() function
translate(string, from-pattern, to-pattern)
Example. translate(“Hello”, “ABCDEFGHIJKLMNOPQRSTUVWXYZ”, “abcdefghijklmnopqrstuvwxyz”);
this will convert Hello to hello (i.e., convert to lower case)
A better approach to the above problem is:
<xsl:variable name="upperCaseChars" select=" 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' "/><xsl:variable name="lowerCaseChars" select=" 'abcdefghijklmnopqrstuvwxyz' "/>
translate(“Hello”, $upperCaseChars, $lowerCaseChars) Note: need to put the string within (single) quotes, otherwisethe XSL Processor will try to interpret it as an XML element.
Introduction to XSLT
Transparency No. 59
Boolean and Relational Operators
Boolean operators: not, and, orRelational operators: <, >, =, <=, >=, !=The less than < and greater than > signs are reserved
symbols, so they need to be escaped when you use them. Thus, the relational operators will appear in your XSL code like this:
< > = <= >= !=< > = <= >= !=
Want this:Use this:
Introduction to XSLT
Transparency No. 60
Arithmetic
The arithmetic operators available: +, -, *, div, mod (remainder from doing a division) Note: recall that an XML element can have a dash in the
name. So, if you want to indicate subtraction, be sure to surround “-” with blank spaces.
Introduction to XSLT
Transparency No. 61
Arithmetic functions
sum(node-set) this function sums up all the values in the set of nodes
floor(number) returns the largest integer that is not greater than number Example. floor(2.5) returns 2
ceiling(number) returns the smallest integer that is not less than number Example. Ceiling(2.5) returns 3
round(number) returns the integer closest to number Example. round(2.3) returns 2
Introduction to XSLT
Transparency No. 62
Enhanced XML Document
<?xml version="1.0"?><FitnessCenter> <Member id="1" level="platinum"> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> <MembershipFee>340</MembershipFee> </Member> <Member id="2" level="gold"> <Name>David</Name> <Phone type="home">383-1234</Phone> <Phone type="work">383-4321</Phone> <FavoriteColor>lightblue</FavoriteColor> <MembershipFee>500</MembershipFee> </Member> <Member id="3" level="platinum"> <Name>Roger</Name> <Phone type="home">888-1234</Phone> <Phone type="work">888-4321</Phone> <FavoriteColor>lightyellow</FavoriteColor> <MembershipFee>340</MembershipFee> </Member></FitnessCenter>
Note that each Membernow has MembershipFeeelement
Introduction to XSLT
Transparency No. 63
Compute Membership Revenue
Membership Fee Revenue:
<xsl:value-of select="sum(/FitnessCenter/Member/MembershipFee)"/>
htmlEx16
Introduction to XSLT
Transparency No. 64
xsl:attribute
This XSL element is used by nesting it within an output element. It enables you to create an attribute for the output element.
Introduction to XSLT
Transparency No. 65
Coloring alternate rows
Member Names:<TABLE border="1" width="25%"> <xsl:for-each select="/FitnessCenter/Member"> <TR> <xsl:if test="position() mod 2 = 0"> <xsl:attribute name="bgcolor">yellow</xsl:attribute> </xsl:if> <TD><xsl:value-of select="Name"/></TD> </TR> </xsl:for-each></TABLE>
For each even row of the table, the TR value will be: <TR bgcolor="yellow">
htmlEx17
Introduction to XSLT
Transparency No. 66
count() function
count(set of node) returns an integer representing the numberof nodes (i.e., XML elements) in the set.
Example.
Number of members = <xsl:value-of select="count(/FitnessCenter/Member)"/>
Output:Number of members = 5
htmlEx18
Introduction to XSLT
Transparency No. 67
Selecting all Elements/Attributes
<xsl:for-each select="/FitnessCenter/Member"> <xsl:for-each select="@*"> ... </xsl:for-each> <xsl:for-each select="*"> ... </xsl:for-each> </xsl:for-each>
For each attribute do ...
For each child elementdo ...
Introduction to XSLT
Transparency No. 68
Getting the Name of the Element/Attribute using the name() Function
<xsl:for-each select="/FitnessCenter/Member"> <xsl:for-each select="@*"> Attribute = <xsl:value-of select="name(.)"/> </xsl:for-each> <xsl:for-each select="*"> Element = <xsl:value-of select="name(.)"/> </xsl:for-each> </xsl:for-each>
name(node) returns the name of "node"
htmlEx19
Introduction to XSLT
Transparency No. 69
When to use Curly Braces?
“When I assign an attribute a value, when do I use curly braces and when do I not use them?”
Use curly braces for these attributes: - the attribute of a literal result element (where you literally type what should be output)
Example: <a href=“#{@id}”> - the name attribute of xsl:attribute
Example: <xsl:attribute name =“{@value}”> - the name attribute of xsl:pi
Example: <xsl:pi name =“{@value}”> - the name attribute of xsl:element
Example: <xsl:element name =“{@value}”> - the optional attributes of xsl:sort:
Example: <xsl:sort order ={@value} lang =“{@value}”data-type ={@value} case-order =“{@value}”>
Introduction to XSLT
Transparency No. 70
document( ) Function
This function enables you to access other XML documents (besides the XML document that you specify when you invoke the XSL Processor).
The format for using the document() function is: document(url), where url is a URL to another XML document
Introduction to XSLT
Transparency No. 71
Fitness Centers Merger
Another fitness center has just merged with us. They have an xml document (FitnessCenter2.xml) containing their Members.
You are to create an XSL-enhanced HTML document that creates a single table comprised of all the Members from both fitness clubs.
Introduction to XSLT
Transparency No. 72
<TABLE border="1" width="75%"> <TR><TH>Name</TH><TH>Phone(home)</TH>...</TR> <xsl:for-each select="/FitnessCenter/Member"> <TR> <TD><xsl:value-of select="Name"/></TD> <TD><xsl:value-of select="Phone[@type='home']"/></TD> <TD><xsl:value-of select="Phone[@type='work']"/></TD> <TD><xsl:value-of select="FavoriteColor"/></TD> </TR> </xsl:for-each> <xsl:variable name="FitnessCenter2" select="document('FitnessCenter2.xml')"/> <xsl:for-each select="$fitnessCenter2/FitnessCenter/Member"> <TR> <TD><xsl:value-of select="Name"/></TD> <TD><xsl:value-of select="Phone[@type='home']"/></TD> <TD><xsl:value-of select="Phone[@type='work']"/></TD> <TD><xsl:value-of select="FavoriteColor"/></TD> </TR> </xsl:for-each></TABLE> htmlEx20
Introduction to XSLT
Transparency No. 73
Parameterized Processing
You can create a subroutine (called a named template), and you can pass to it parameters.
Introduction to XSLT
Transparency No. 74
<xsl:template match="/"> <HTML> <HEAD> <TITLE>Fitness Center</TITLE> </HEAD> <BODY> <xsl:call-template name="displayNameWithFont"> <xsl:with-param name="fontFace" select="‘Arial'"/> <xsl:with-param name="name" select="/FitnessCenter/Member[1]/Name"/> </xsl:call-template> <BR/> ... </BODY> </HTML> </xsl:template>
<xsl:template name="displayNameWithFont"> <xsl:param name="fontFace" select="‘Times New Roman'"/> <!-- default font --> <xsl:param name="name"/> <FONT face="{$fontFace}"> <xsl:value-of select="$name"/> </FONT> </xsl:template> htmlEx21
Introduction to XSLT
Transparency No. 75
Call by Reference
How do we create a named template that returns a value?
Example: create a named template which, when passed a number,
returns the number div 2.
Introduction to XSLT
Transparency No. 76
<xsl:template match="/"> <HTML> <HEAD><TITLE>Fitness Center</TITLE></HEAD> <BODY> 16 / 2 =
<xsl:variable name="result"> <xsl:call-template name="NumDiv2"> <xsl:with-param name="N" select="16"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="$result"/> </BODY> </HTML> </xsl:template>
<xsl:template name="NumDiv2"> <xsl:param name="N"/> <xsl:value-of select="$N div 2"/> </xsl:template>
htmlEx22
Introduction to XSLT
Transparency No. 77
generate-id()
Use this function to generate a unique string for a node in the source document tree.
Example.
generate-id(/FitnessCenter/Member[1]) will return a unique id for the first Member
Note: for the same node e, generate-id(e) will always produce the same id.
Introduction to XSLT
Transparency No. 78
Using generate-id() to Uniquely Identify Elements
In previous example we created two tables – a table containing the Members Names, and a separate table containing home Phone numbers. Each Name was hyperlinked to his/her home Phone. We used the id attribute on each Member element to
link the two tables together.Suppose there is no id attribute. We can use
generate-id() to create a unique identifier.
Introduction to XSLT
Transparency No. 79
<TABLE border="1" width="25%"> <TR><TH>Name</TH></TR> <xsl:for-each select="/FitnessCenter/Member"> <TR> <TD> <A href="#{generate-id(.)}"> <xsl:value-of select="Name"/> </A> </TD> </TR> </xsl:for-each> </TABLE> <BR/><BR/><BR/><BR/><BR/> <TABLE border="1" width="25%"> <TR><TH>Home Phone Number</TH></TR> <xsl:for-each select="/FitnessCenter/Member"> <TR><TD> <A name="{generate-id(.)}"> <xsl:value-of select="Phone[@type='home']"/> </A> </TD> </TR> </xsl:for-each> </TABLE>
htmlEx23
Introduction to XSLT
Transparency No. 80
Debugging your Stylesheets using xsl:message
xsl:message is used to display a message, and (optionally) terminate execution of the stylesheet.
The message is sent to the screen, not to the output file.This provides a very nice way to monitor the flow of your
stylesheet, without impacting the output file.
Introduction to XSLT
Transparency No. 81
Example using xsl:message
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/>
<xsl:template match="/"> <HTML> <BODY> <xsl:for-each select="FitnessCenter/Member"> <xsl:if test="MembershipFee < 0"> <xsl:message terminate="yes"> <xsl:text>Invalid MembershipFee</xsl:text> </xsl:message> </xsl:if> </xsl:for-each> <xsl:text>All the MembershipFee elements are valid</xsl:text> </BODY> </HTML> </xsl:template>
</xsl:stylesheet>
Two possible values forterminate - yes, or no.terminate="yes" means that youwant the message output to thescreen and then the programstopped.terminate="no" means that youwant the message output to thescreen and the program to continue executing.
htmlEx26
Introduction to XSLT
Transparency No. 82
system-property() function
The system-property(property name) function enables you to obtain information about the XSL Processor that you are using: xsl:vendor - if you specify this as the value for property na
me then it will return the name of the XSL Processor vendor
xsl:vendor-url - this provides the URL to the vendor's web site
xsl:version - this indicates what version of the XSL spec is implemented.
Introduction to XSLT
Transparency No. 83
Example
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/>
<xsl:template match="/"> <xsl:message> XSLT Processor: <xsl:value-of select="system-property('xsl:vendor')"/> </xsl:message> <HTML> <HEAD> <TITLE>Welcome</TITLE> </HEAD> <BODY> Welcome! </BODY> </HTML> </xsl:template>
</xsl:stylesheet>
htmlEx27
Introduction to XSLT
Transparency No. 84
Embedded Stylesheets
You can embed a stylesheet within an XML document.<?xml version="1.0"?><!DOCTYPE FitnessCenter [<!ATTLIST xsl:stylesheet id ID #REQUIRED>]><?xml-stylesheet type="text/xml" href="#embed"?><FitnessCenter> <Member level="platinum"> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member> ... <xsl:stylesheet id="embed“ xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version="1.0"> ...
</xsl:stylesheet>
</FitnessCenter>
Stylesheet embedded withinthe XML document
Introduction to XSLT
Transparency No. 85
Embedded Stylesheets
<?xml version="1.0"?><!DOCTYPE FitnessCenter [<!ATTLIST xsl:stylesheet id ID #REQUIRED>]><?xml-stylesheet type="text/xml" href="#embed"?><FitnessCenter> <Member level="platinum"> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member> ... <xsl:stylesheet id="embed" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> ...
</xsl:stylesheet>
</FitnessCenter>
Add an id attribute to thexsl:stylesheet element.
The stylesheet PI referencesthe embeded stylesheet (asindicated by the"#" sign)
You must indicate that the id attributeis of type ID.
htmlEx28
Introduction to XSLT
Transparency No. 87
Note
All the xsl functionality that we have learned at previous slides are applicable in transforming XML documents
Introduction to XSLT
Transparency No. 88
Transformation Language
XSL may be used as a transformation language it may be used to transform an XML document into another
XML document (perhaps the new one is the same, minus company sensitive data)
Transformation Engine(XSLT Processor)
XSL
XML XML
Introduction to XSLT
Transparency No. 89
Example: Filter Gold Members
<?xml version="1.0"?><FitnessCenter> <Member id="1" level="platinum"> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member> <Member id="2" level="gold"> <Name>David</Name> <Phone type="home">383-1234</Phone> <Phone type="work">383-4321</Phone> <FavoriteColor>lightblue</FavoriteColor> </Member> <Member id="3" level="platinum"> <Name>Roger</Name> <Phone type="home">888-1234</Phone> <Phone type="work">888-4321</Phone> <FavoriteColor>lightyellow</FavoriteColor> </Member></FitnessCenter>
<?xml version="1.0"?><FitnessCenter> <Member id="1" level="platinum"> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member> <Member id="3" level="platinum"> <Name>Roger</Name> <Phone type="home">888-1234</Phone> <Phone type="work">888-4321</Phone> <FavoriteColor>lightyellow</FavoriteColor> </Member></FitnessCenter>
Introduction to XSLT
Transparency No. 90
A pseudo code for XSLT Process model:
process(Node: node): tree-fragment rules = match(node); // find rules matching this node rule = rules.best(); // find the best among all rules result = rule.appliedTo(node) // this may recursively call process() return result.
main() { return process( root-node ) }.
Introduction to XSLT
Transparency No. 91
XML Transformations - all about (Template) “Rules”
Each template rule has two parts: A pattern or matching part, that
- identifies the XML node in the source document to which the action part is to be applied. Matching information is contained in an attribute.
An action part that
- details the transformation of the node
Introduction to XSLT
Transparency No. 92
XSLT Document Structure
<?xml version=“1.0”?><xsl:stylesheet> <xsl:template match=“/”> [action] </xsl:template> <xsl:template match=“FitnessCenter”> [action] </xsl:template> <xsl:template match=“Member”> [action] </xsl:template> ...</xsl:stylesheet>
Introduction to XSLT
Transparency No. 93
Template Rules
Template rules take the following general form:
<xsl:template match=“pattern”> [ action ]
</xsl:template>
Introduction to XSLT
Transparency No. 94
Template Rules (Example)
<xsl:template match=“Member”> <xsl:apply-templates/></xsl:template>
“Use this rule when the processor parse through the XML documentand get to a <Member> element.”
“Go to each of my children (the Member children) and apply the template rules to them.”
<xsl:template match=“Member”>
<xsl:apply-templates/>
Introduction to XSLT
Transparency No. 95
Terminology
In FitnessCenter.xml we have (snippet):
<FitnessCenter> <Member> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member> ...</FitnessCenter>
“Member is a child element of the FitnessCenter element. Name, Phone,Phone, and FavoriteColor are children elements of the Member element.Member is a parent of Name. FitnessCenter and Member are ancestors of Name.”
Introduction to XSLT
Transparency No. 96
xsl:element
Used to compute/create output elements. xsl:element is used to create elements
<xsl:element name=“element-name”> [contents of the new element]</xsl:element>
<element-name> [contents of the new element] </element-name>creates
Introduction to XSLT
Transparency No. 97
Identity Transformation
Create a stylesheet which simply creates an XML document that is a copy of the input XML document
Introduction to XSLT
Transparency No. 98
Document/
PI<?xml version=“1.0”?>
ElementFitnessCenter
ElementMember
ElementMember
ElementMember
ElementName
ElementPhone
ElementPhone
ElementFavoriteColor
... ...
TextJeff
Text555-1234
Text555-4321
Textlightgrey
...
Introduction to XSLT
Transparency No. 99
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/> <!– output method -->
<xsl:template match="/"> <!-- main template --> <xsl:apply-templates/> </xsl:template>
<xsl:template match="FitnessCenter"> <xsl:element name="FitnessCenter"> <xsl:apply-templates select=“./node()” /> </xsl:element> </xsl:template>
<xsl:template match="Member"> <xsl:element name="Member"> <xsl:apply-templates/> </xsl:element> </xsl:template>
default selection
Introduction to XSLT
Transparency No. 100
<xsl:template match="Name"> <xsl:element name="Name"><xsl:apply-templates/> </xsl:element> </xsl:template>
<xsl:template match="Phone"> <xsl:element name="Phone"> <xsl:apply-templates/> </xsl:element> </xsl:template>
<xsl:template match="FavoriteColor"> <xsl:element name="FavoriteColor"> <xsl:apply-templates/> </xsl:element> </xsl:template>
<xsl:template match="text()"> <!– rule for text node--><xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Introduction to XSLT
Transparency No. 101
<?xml version="1.0" encoding="UTF-8"?><FitnessCenter> <Member> <Name>Jeff</Name> <Phone>555-1234</Phone> <Phone>555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member> <Member> <Name>David</Name> <Phone>383-1234</Phone> <Phone>383-4321</Phone> <FavoriteColor>lightblue</FavoriteColor> </Member> <Member> <Name>Roger</Name> <Phone>888-1234</Phone> <Phone>888-4321</Phone> <FavoriteColor>lightyellow</FavoriteColor> </Member></FitnessCenter>
Note that we've lost the attribute on the Member element
The output
Introduction to XSLT
Transparency No. 102
<xsl:template match="Member"> <xsl:element name="Member"> <xsl:for-each select="@*"> <xsl:attribute name="{name(.)}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:for-each> <xsl:apply-templates/> </xsl:element> </xsl:template>
For each attribute Add an attribute to the element being output. The name of the attribute is the name of the current attribute being processed. The value of the attribute is the value of the current attribute being processed.
Getting Member’s Attribute:
Introduction to XSLT
Transparency No. 103
<?xml version="1.0" encoding="UTF-8"?><FitnessCenter> <Member level=“platinum”> <Name>Jeff</Name> <Phone type="home">555-1234</Phone> <Phone type="work">555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member> <Member level=“gold”> <Name>David</Name> <Phone type="home">383-1234</Phone> <Phone type="work">383-4321</Phone> <FavoriteColor>lightblue</FavoriteColor> </Member> <Member level=“platinum”> <Name>Roger</Name> <Phone type="home">888-1234</Phone> <Phone type="work">888-4321</Phone> <FavoriteColor>lightyellow</FavoriteColor> </Member></FitnessCenter>
Introduction to XSLT
Transparency No. 104
Generalize
previous identity stylesheet only work for FitnessCenter XML documents.
Make a stylesheet which does an identity transformation on any XML document.
Introduction to XSLT
Transparency No. 105
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/>
<xsl:template match="/"><xsl:apply-templates/></xsl:template>
<xsl:template match="*"> <!– foreach element … --> <xsl:element name="{name(.)}"> <xsl:for-each select="@*"> <!– foreach attribute … --> <xsl:attribute name="{name(.)}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:for-each> <!– foreach child node … --> <xsl:apply-templates/> </xsl:element> </xsl:template>
<xsl:template match="text()"><xsl:value-of select="."/>
</xsl:template> </xsl:stylesheet>
Introduction to XSLT
Transparency No. 106
Default Template Rules
Every xsl document has two default template rulesApplied when the XSL Processor cannot find a templ
ate rule to use in your stylesheet.Here are the two default template rules:
<xsl:template match=“/ | *”><xsl:apply-templates/>
</xsl:template>
<xsl:template match=“text()”><xsl:value-of select=“.”/>
</xsl:template>
“Match on the document orany element. The action is to goto the children and execute theirtemplate rules.”
“Match on a text node. The actionis to output the value of the textnode.”
Introduction to XSLT
Transparency No. 107
Multiple Applicable Rules
Suppose we are finding rules to be applied to a <member/> node. But now there are two rules matched:
• <xsl:template match=“Member”>...• <xsl:template match=“/ | *”>... // the default rule
Which one will the processor choose to apply?
Answer: given two rules that apply, the more specific rule wins.--> Clearly, “*” is much more general than “Member”.
“*” matches on any element. “Member” just matches on the Member element.
Introduction to XSLT
Transparency No. 108
Smallest Identity Transformation Stylesheet
Now that we know about the default template rules, we can further reduce the size of the stylesheet.
Introduction to XSLT
Transparency No. 109
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/>
<xsl:template match="*"> <xsl:element name="{name(.)}"> <xsl:for-each select="@*"> <xsl:attribute name="{name(.)}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:for-each> <xsl:apply-templates/> </xsl:element> </xsl:template>
</xsl:stylesheet>
Introduction to XSLT
Transparency No. 110
<xsl:apply-templates select=“pattern”>
The xsl:apply-templates element (without the select attribute) tells the XSL Processor to apply the template rules to all children (in document order)
The xsl: apply-templates element can have a select attribute that tells the XSL Processor which nodes are to be processed instead of just the child nodes. we can also use <xsl:order/> to specify the order in which sele
cted nodes are processed
Introduction to XSLT
Transparency No. 111
<xsl:apply-templates select=“pattern”>
<xsl:template match="Member"> <xsl:apply-templates select="Name"/> <xsl:apply-templates select="Phone[@type='work']"/></xsl:template>
"Go to the template rule for my Name child element. Thengo to the template rule for the work Phone child element."
<xsl:template match="Member"> <xsl:apply-templates select="*"/></xsl:template>
"Go to all the child element nodes (not to any child text nodes)."
Introduction to XSLT
Transparency No. 112
mode Attribute
Allows you to create multiple template rules for the same element. Each template rule can process the element differently.
So, you can have multiple template rules for the same element. Just give each template rule a different mode
<xsl:template match="Name" mode="Normal">
<xsl:template match="Name" mode="footnote">
Introduction to XSLT
Transparency No. 113
Problem
Identity transform the FitnessCenter.xml document. However, after you have copied all the Members, follow up with a (new) GoldMembers section, containing the name of each gold member (within stars)
The next slide shows what the output XML file should look like
Introduction to XSLT
Transparency No. 114
<?xml version="1.0" encoding="UTF-8"?><FitnessCenter> <Member level="platinum"> <Name>Jeff</Name> <Phone>555-1234</Phone> <Phone>555-4321</Phone> <FavoriteColor>lightgrey</FavoriteColor> </Member> <Member level="gold"> <Name>David</Name> <Phone>383-1234</Phone> <Phone>383-4321</Phone> <FavoriteColor>lightblue</FavoriteColor> </Member> <Member level="platinum"> <Name>Roger</Name> <Phone>888-1234</Phone> <Phone>888-4321</Phone> <FavoriteColor>lightyellow</FavoriteColor> </Member> <GoldMembers> <Name>***David***</Name> </GoldMembers></FitnessCenter>
Note that thenames hereare processeddifferentlythan the namein the GoldMemberssection
Introduction to XSLT
Transparency No. 115
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform version="1.0"> <xsl:output method="xml"/> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template>
<xsl:template match="FitnessCenter"> <xsl:element name="FitnessCenter"> <xsl:apply-templates/> <xsl:element name="GoldMembers"> <xsl:for-each select="Member[@level='gold']"> <xsl:apply-templates select="Name" mode="footnote"/> </xsl:for-each> </xsl:element> </xsl:element> </xsl:template>
<xsl:template match="Member"> <xsl:element name="Member"> <xsl:for-each select="@*"> <xsl:attribute name="{name(.)}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:for-each> <xsl:apply-templates mode="Normal"/> </xsl:element> </xsl:template>
Introduction to XSLT
Transparency No. 116
<xsl:template match="Name" mode="Normal"> <xsl:element name="Name"> <xsl:apply-templates/> </xsl:element> </xsl:template>
<xsl:template match="Name" mode="footnote"> <xsl:element name="Name"> <xsl:text>***</xsl:text> <xsl:apply-templates/> <xsl:text>***</xsl:text> </xsl:element> </xsl:template>
<xsl:template match="Phone" mode="Normal"> <xsl:element name="Phone"> <xsl:apply-templates/> </xsl:element> </xsl:template>
<xsl:template match="FavoriteColor" mode="Normal"> <xsl:element name="FavoriteColor"> <xsl:apply-templates/> </xsl:element> </xsl:template></xsl:stylesheet>
Introduction to XSLT
Transparency No. 117
Stylesheet Reuse via xsl:include and xsl:import
The elements xsl:include and xsl:import enable you to reuse other stylesheets.
These elements are “top-level elements”. This means that they must be immediate children of the xsl:stylesheet element (i.e., they cannot be within a template rule)
The xsl:include element is basically a macro substitution - the element is replaced by the contents of stylesheet it references
like #include in C/C++
Introduction to XSLT
Transparency No. 118
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="lcase" select="'abcdefghijklmnopqrstuvwxyz'"/><xsl:variable name="ucase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:template match="*"> <xsl:apply-templates select="@* | * | text() | comment() | processing-instruction()"/></xsl:template>
<xsl:template match="@*"> <xsl:value-of select="translate(.,$lcase, $ucase)"/></xsl:template>
<xsl:template match="text()"> <xsl:value-of select="translate(.,$lcase, $ucase)"/></xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:include href="toUpperCase.xsl"/>
<xsl:template match="FitnessCenter"> ... </xsl:template> ...</xsl:stylesheet>
Replace the xsl:includeelement with the contentsof the referenced stylesheet(i.e., all the children ofxsl:stylesheet)
toUpperCase.xsl
Introduction to XSLT
Transparency No. 119
xsl:import
xsl:import acts just like xsl:include - the stylesheet that it references is macro-substituted. However, there is a difference: With xsl:include the stuff that is macro-substituted in
to the stylesheet has the same precedence as the rest of the stylesheet. It is as though you had one stylesheet.
With xsl:import the stuff that is macro-substituted into the stylesheet has lower precedence than the rest of the stylesheet. Also, all xsl:import elements must come first in the stylesheet.
like super/parent calss in OOP.
Introduction to XSLT
Transparency No. 120
Import precedence
import tree given as the figure
=> order of precedence:
D < B < E < C < A.
Principles:
1. postorder traversal of the tree. I.e.,
2. later imported rules or definitions. > earlier rules or def.
3. importing stylesheet > imported stylesheet.
D
E
AB
C
Introduction to XSLT
Transparency No. 122
Problem
Create a stylesheet that creates a text file containing each member's data. One line per member Member data separated by a slash delimiter
Jeff/555-1234/555-4321/lightgreyDavid/383-1234/383-4321/lightblueRoger/888-1234/888-4321/lightyellow
Introduction to XSLT
Transparency No. 123
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text"/>
<xsl:variable name="delimiter" select="'/'"/>
<xsl:template match="FitnessCenter"> <xsl:for-each select="Member"> <xsl:apply-templates select="."/> <xsl:text>
</xsl:text> <!-- Hex value for carriage return --> </xsl:for-each> </xsl:template>
<xsl:template match="Member"> <xsl:for-each select="*[position() < last()]"> <xsl:value-of select="."/> <xsl:value-of select="$delimiter"/> </xsl:for-each> <xsl:value-of select="*[last()]"/> </xsl:template> </xsl:stylesheet>
Introduction to XSLT
Transparency No. 125
Examples
transform some data represented in XML using three different XSLT stylesheets to produce three different representations of the data, HTML, SVG and VRML.
The input data:<sales> <division id="North"> <revenue>10</revenue> <growth>9</growth> <bonus>7</bonus></division> <division id="South"> <revenue>4</revenue> <growth>3</growth> <bonus>4</bonus></division> <division id="West"><revenue>6</revenue> <growth>-1.5</growth><bonus>2</bonus></division> </sales>
Introduction to XSLT
Transparency No. 126
transforms the data into HTML using simplified syntax:
<html xsl:version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
lang="en">
<head><title>Sales Results By Division</title></head>
<body>
<table border="1"> <tr><th>Division</th><th>Revenue</th><th>Growth</th>
<th>Bonus</th></tr>
Introduction to XSLT
Transparency No. 127
<xsl:for-each select="sales/division"> <!-- order the result by revenue --> <xsl:sort select="revenue" data-type="number" order="descending"/> <tr><td><em><xsl:value-of select="@id"/></em></td> <td><xsl:value-of select="revenue"/></td> <td><!-- highlight negative growth in red --> <xsl:if test="growth < 0"> <xsl:attribute name="style"> <xsl:text>color:red</xsl:text> </xsl:attribute></xsl:if> <xsl:value-of select="growth"/></td> <td><xsl:value-of select="bonus"/></td></tr> </xsl:for-each></table></body></html>
Introduction to XSLT
Transparency No. 128
The HTML output
<html lang="en">
<head><meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>Sales Results By Division</title></head>
<body>
<table border="1">
<tr><th>Division</th><th>Revenue</th>
<th>Growth</th><th>Bonus</th></tr>
<tr><td><em>North</em></td><td>10</td><td>9</td><td>7</td></tr>
<tr><td><em>West</em></td><td>6</td>
<td style="color:red">-1.5</td><td>2</td></tr>
<tr><td><em>South</em></td><td>4</td><td>3</td><td>4</td></tr>
</table></body></html>
Introduction to XSLT
Transparency No. 129
Transform the data into SVG:
<xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/Graphics/SVG/SVG-19990812.dtd"><xsl:output method="xml" indent="yes" media-type="image/svg"/>
<xsl:template match="/"> <svg width = "3in" height="3in"> <g style = "stroke: #000000"> <!-- draw the axes --> <line x1="0" x2="150" y1="150" y2="150"/> <line x1="0" x2="0" y1="0" y2="150"/> <text x="0" y="10">Revenue</text> <text x="150" y="165">Division</text> <xsl:for-each select="sales/division"> <!-- define some useful variables --> <!-- the bar's x position -->
Introduction to XSLT
Transparency No. 130
<xsl:variable name="pos" select="(position()*40)-30"/>
<!-- the bar's height -->
<xsl:variable name="height" select="revenue*10"/>
<!-- the rectangle -->
<rect x="{$pos}" y="{150-$height}"
width="20" height="{$height}"/>
<!-- the text label -->
<text x="{$pos}" y="165"><xsl:value-of select="@id"/></text>
<!-- the bar value -->
<text x="{$pos}" y="{145-$height}">
<xsl:value-of select="revenue"/></text>
</xsl:for-each>
</g></svg></xsl:template>
</xsl:stylesheet>
Introduction to XSLT
Transparency No. 131
The SVG output
<svg width="3in" height="3in" xmlns="http://www.w3.org/Graphics/SVG/svg-19990412.dtd"> <g style="stroke: #000000"> <line x1="0" x2="150" y1="150" y2="150"/> <line x1="0" x2="0" y1="0" y2="150"/> <text x="0" y="10">Revenue</text> <text x="150" y="165">Division</text> <rect x="10" y="50" width="20" height="100"/> <text x="10" y="165">North</text> <text x="10" y="45">10</text> <rect x="50" y="110" width="20" height="40"/> <text x="50" y="165">South</text> <text x="50" y="105">4</text> <rect x="90" y="90" width="20" height="60"/> <text x="90" y="165">West</text> <text x="90" y="85">6</text> </g></svg>
Introduction to XSLT
Transparency No. 132
Transforms the data into VRML
<xsl:stylesheet version="1.0“
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- generate text output as mime type model/vrml, using default charset -->
<xsl:output method="text" encoding="UTF-8"
media-type="model/vrml"/>
<xsl:template match="/">#VRML V2.0 utf8
# externproto definition of a single bar element
EXTERNPROTO bar [
field SFInt32 x
field SFInt32 y
field SFInt32 z
field SFString name ]
"http://www.vrml.org/WorkingGroups/dbwork/barProto.wrl"
Introduction to XSLT
Transparency No. 133
# inline containing the graph axes
Inline { url "http://www.vrml.org/WorkingGroups/dbwork/barAxes.wrl"
}
<xsl:for-each select="sales/division">
bar { x <xsl:value-of select="revenue"/>
y <xsl:value-of select="growth"/>
z <xsl:value-of select="bonus"/>
name "<xsl:value-of select="@id"/>" }
</xsl:for-each></xsl:template> </xsl:stylesheet>
Introduction to XSLT
Transparency No. 134
The VRML Output
#VRML V2.0 utf8
# externproto definition of a single bar element
EXTERNPROTO bar [ field SFInt32 x field SFInt32 y
field SFInt32 z field SFString name ]
"http://www.vrml.org/WorkingGroups/dbwork/barProto.wrl"
# inline containing the graph axes
Inline {url
"http://www.vrml.org/WorkingGroups/dbwork/barAxes.wrl" }
bar {x 10 y 9 z 7 name "North" }
bar {x 4 y 3 z 4 name "South" }
bar {x 6 y -1.5 z 2 name "West" }