XSLT Tutorial

 

Table of Contents

Analyzing the Program

Introduction

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. Ensure that Line Numbers are enabled in Komodo (View|View Line Numbers). Ensure that the file mailexport2html.xsl is displayed in the Komodo Editor pane.

XSLT Header

Lines 1 to 3 - XML and XSLT Declarations

  • an XSLT program is actually an XML document - thus, the XML version and character set are declared on the first line
  • the namespace declaration on the second line tells the "parser" (the XSLT interpreter) that XSLT elements are prefixed with "xsl:" to prevent confusion with user-defined element names and non-XSLT elements
  • xsl:output controls the appearance of the generated output; for example, the presence of this line will generate a META declaration in the head of the HTML output

Komodo Tip notice that different types of language elements are displayed in different colors. You can 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.

Top

HTML Header

Line 6 - XSLT "template"

  • the "template" statement is the main processing element in an XSLT program
  • the attribute match="/" specifies that the template will be selected when the document element is processed
XSLT 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. In line 6, match="/" selects a "node" in the XML document's hierarchy, rather than a specific item of data.

Lines 7 to 11 - HTML Tags

  • these lines write standard HTML tags to the output document

Line 12 - XSLT "apply-templates"

  • this statement processes each node of the XML document (that is, each sub-section contained beneath the current position in the XML document)
  • for each node, the XSLT "engine" (the internal processor) checks the XSLT program for a matching template
  • the first XML tag with a corresponding template is <EMAIL>

Lines 13 to 15 - HTML Tags

  • after processing all the nodes in the XML document, processing will return to line 13, where the closing tags for the HTML page will be written to the output
  • line 15 closes the XSLT processing routine, completing the program
Top

Format Email Header

Lines 18 to 21 - Select HEADER content

  • when line 18 is processed, content in <HEADER> tags in the XML document are processed
  • lines 19 and 21 write standard HTML formatting around the content generated in line 20
  • on line 20, the "value-of" statement selects content contained in the <SUBJECT> tag and writes it to the output document

Komodo Tip Click on the minus symbol to the left of line 19. The entire section of nested code will be collapsed. This is Code Folding.

Lines 22 to 29 - "call-template"

  • after the "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 23
  • "with-param" indicates that the parameter "address" should be applied to the contents of the <ORIGADDRESS> XML tag
  • the same selection and formatting routine is applied to the contents of the <DESTADDRESS> XML tag on lines 26 to 28

XSLT Pointer Notice the "<BR/>" HTML tag on line 25. XML and XSLT treat all tags as container tags that have both opening and closing elements. However, some HTML tags (like <BR> and <IMG>) stand alone, and do not require a closing tag. They are represented with a closing slash. XSLT tags also use a closing slash if they are not a tag pair (as shown on line 23).


Top

Process Email

Lines 33 to 34 - Process First Message

  • when the "apply-templates" tag in line 12 is encountered, processing jumps to line 33
  • on line 34, the "HEADER" node is selected and processing jumps to line 18

XSLT Pointer comments in XSLT programs are enclosed in the tags <!-- and -->, the same as in HTML.

Lines 36 to 39 - Process Email Body

  • after processing the email header, the XSLT program proceeds to line 36
  • the contents of the "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.


Top

Format Email Addresses

Lines 45 to 52 - Format Email Addresses

  • the routine that starts on line 47 is called from lines 22 and 26
  • the contents of the "address" parameter are determined on lines 23 and 27
  • on line 49, the contents of the "address" parameter are converted to a variable and concatenated with the text that constitutes a valid email address reference in HTML

Top Previous Next