This tutorial assumes:
In the Perl Tutorial, a Perl program converts a text file containing exported email messages to an XML file. In this tutorial, XSLT converts the XML file to HTML. Note that you do not need to complete the Perl Tutorial before doing the XSLT Tutorial. Each tutorial can be completed independently. In this tutorial you will:
On File menu, click Open|Project and navigate to the xslt_tutorial.kpf project file on the xslt_tutorials subdirectory. The location differs depending on your operating system.
Windows
<komodo-install-directory>\lib\support\samples\xslt_tutorials
Linux
<komodo-install-directory>/lib/support/samples/xslt_tutorials
Mac OS X
<User-home-directory>/Library/Application Support/Komodo/3.x/samples/xslt_tutorials
All files included in the tutorial project are displayed on the Projects tab in the Left Pane.
On the Projects tab, double-click the files mailexport.html, mailexport.xml, and mailexport2html.xsl. These files open in the Editor Pane; a tab at the top of the pane displays each of their names.
In this step, you will analyze the XSLT program on a line-by-line basis. Open the XSLT Tutorial Project and associated files as described in the previous step. Be sure Line Numbers are enabled in Komodo (View|View Line Numbers). Be sure the mailexport2html.xsl file is displayed in the Komodo Editor Pane.
xsl:
to prevent confusion with user-defined
element names and non-XSLT elementsxsl:output
controls the appearance of the
generated output; for example, the presence of this line
generates a META
declaration in the head of the
HTML output
Komodo Tip: Notice that different types of language elements are displayed in different colors. Adjust the display options for language elements in the Preferences dialog box. |
XSLT Pointer: Processing routines in XSLT programs are enclosed in opening and closing tags similar to those in XML. |
template
is the main processing element in an
XSLT programmatch="/"
attribute specifies that the
template is selected when the document element is
processedXSLT Pointer: XSLT commands have (up to) four components: namespace ("xsl"), element ("template"), attribute(s) ("match="), and attribute value(s) ("/"). |
XSLT Pointer: XSLT
uses XPath expressions to select data from XML documents. On
line 6, match="/" selects a "node" in the XML
document's hierarchy, rather than a specific item of
data. |
<EMAIL>
<HEADER>
tags in the XML document are
processedvalue-of
statement selects
content contained in the <SUBJECT>
tag and
writes it to the output document
Komodo Tip: Click the minus symbol to the left of line 19. The entire section of nested code is collapsed. This is called Code Folding. |
From:
text, the
call-template
routine causes the XSLT program to
proceed to the template formatEmail
on line 51;
after completing the formatEmail
routine,
processing returns to line 23with-param
indicates that the parameter
address
should be applied to the contents of the
<ORIGADDRESS>
XML tag<DESTADDRESS>
XML tag on
lines 26 to 28
XSLT Pointer: Notice the
|
apply-templates
tag in line 12 is
encountered, processing jumps to line 33HEADER
node is selected and
processing jumps to line 18
XSLT Pointer: Comments in XSLT programs
are enclosed in the tags |
BODY
tag are placed in the
HTML tags
Komodo Tip: XSLT programs and XML input documents must be "well-formed" in order to perform transformations. Komodo's Background Syntax Checking makes it easy to identify and fix coding errors. |
address
parameter contents are determined on
lines 23 and 27address
parameter are converted to a variable and concatenated with the
text that constitutes a valid email address reference in
HTMLTo start, generate program output by running the program through the debugger without setting any breakpoints.
This section reviews how to add breakpoints to the program and "debug" it. Adding breakpoints lets you to run the program in parts, making it possible to watch variables and view output as they are generated. Before beginning, be sure that line numbering is enabled in Komodo (View|View Line Numbers).
Komodo Tip: Debugger commands can be accessed from the Debug menu, by shortcut keys, or from the Debug Toolbar. For a summary of debugger commands, see the Debugger Command List. |
Komodo Tip: Breakpoints can be set at any time. An enabled breakpoint is a solid red circle. A disabled breakpoint is a white circle with a red outline. Click once in the gray margin to enable a breakpoint. Click an enabled breakpoint once to disable it. |
xsl:apply-templates
), it looked for a template
that matched the top node in the XML document
(<EMAILCOMMENTS>
). When no matching template
was found, it proceeded to the next node in the XML document
(<EMAIL>
) and found a matching template on
line 33.xsl:value-of
statement
selects the contents of the <SUBJECT>
field
on line 7 of the XML file and places it within the HTML tags on
lines 19 and 21.formatEmail
on line 45. Continue to step
in until line 49 is processed. The formatEmail
template is processed with the address
parameter
on line 46. This routine processes the contents of the
<ORIGADDRESS>
node in the XML document. In
order to generate the hyperlink in the output HTML document,
lines 48 and 49 concatenate the string mailto:
with the contents of the <ORIGADDRESS>
node.ASPN, the ActiveState Programmer Network, hosts the XSLT Cookbook, a collaborative library of XSLT code.
The W3C (World Wide Web Consortium) specifications are available online:
There are many XSLT tutorials and beginner XSLT sites on the Internet, including: