Professional Documents
Culture Documents
XML
Tutorial Six:
Functional Programming with XSLT and XPath 1.0
A Guide to this Instructor’s Manual:
We have designed this Instructor’s Manual to supplement and enhance your teaching
experience through classroom activities and a cohesive chapter summary.
This document is organized chronologically, using the same heading in blue that you see in the
textbook.Under each heading you will find (in order): Lecture Notes that summarize the
section, Figures and Boxes found in the section (if any), Teacher TIPs, Classroom Activities, and
Lab Activities.Pay special attention to teaching tips, and activities geared towards quizzing your
students, enhancing their critical thinking skills, and encouraging experimentation within the
software.
In addition to this Instructor’s Manual, our Instructor’s Resources also include PowerPoint
Presentations, Test Banks, and other supplements to aid in your teaching experience.
Table of Contents
Tutorial Objectives 2
XML 326: Using XSLT Variables 2
XML 331: Copying Nodes 3
XML 335: Retrieving Data from Multiple Files 3
XML 339: Accessing an External Style Sheet 4
XML 344: Creating a Lookup Table in XSLT 5
XML 347: Working with Numeric Functions 6
XML 349: Applying Mathematical Operators 7
XML 351: Formatting Numeric Values 7
XML 355: Working with Text Strings 8
XML 364: Introducing Parameters 8
XML 368: Using Named Templates 9
XML 369: Introducing Functional Programming 10
© 2015 Cengage Learning
New Perspectives on XML Comprehensive, 3rd Edition, Instructor’s Manual 2 of 12
End of Tutorial Material 11
Glossary of Key Terms 12
Tutorial Objectives
Students will have mastered the material in Tutorial Six when they can:
Session 6.1 ⚫ Apply a number format
⚫ Create and apply XSLT variables ⚫ Extract and combine text strings
⚫ Copy nodes into the result document
⚫ Retrieve data from XML documents Session 6.3
⚫ Access external style sheets ⚫ Create global and local parameters
⚫ Explore the principals of functional
Session 6.2 programming
⚫ Create a lookup table ⚫ Create a recursive template
⚫ Use XPath 1.0 numeric functions and
operators
BOXES
• TIP: A variable name cannot start with a number, and variable names are case sensitive. (XML 326)
• TIP: Declare your variables directly after the opening tag for the element that contains them so
there is no confusion about the variables' scope. (XML 327)
• Reference: Declaring and Referencing Variables in XSLT 1.0 (XML 329)
FIGURES
• Figure 6-1, Figure 6-2, Figure 6-3, Figure 6-4, Figure 6-5
TEACHER TIP
Stress to students that an XSLT variable can hold more than just a single value. An XSLT variable can also
contain more complex elements such as a complete node set from an XML document or an entire section of
code.
CLASSROOM ACTIVITIES
1. Class Discussion: Prepare a list of valid and invalid variable names. Present them to the class and ask
them to determine if each is valid or invalid.
2. Quick Quiz:
• True/False: If a variable references a node set, you can use the variable in place of the node-set
reference. (Answer: True)
• True/False: You can assign the same name to a global variable and a local variable. (Answer:
True)
BOXES
• InSight: Using the Identity Template (XML 332)
• Reference: Creating a Copy of a Node Set (XML 333)
• InSight: Copying Nodes in XSLT 2.0 (XML 335)
FIGURES
• Figure 6-6, Figure 6-7
CLASSROOM ACTIVITIES
1. Class Discussion: Ask students to propose situations when you might prefer to use the copy element
to create a shallow copy of a node and situations when use of the copy-of element to create a deep
copy of a node set would be appropriate. Are there any common differences between these examples?
2. Quick Quiz:
• True/False: Many XSLT processors will not write markup tags even if they are enclosed within a
CDATA section. (Answer: True)
• The __________ element creates a shallow copy limited to only the active node and does not
include the children, descendants, or attributes of that node. (Answer: copy)
• Explain that both the document() and doc() functions return the root node of the
specifieddocument, and this root node you can append an XPath expression just as you would with
location paths in the source document
• Describe the unparsed-text() function supported by XSLT 2.0, which can be used to retrieve
non-XML data, including HTML files
BOXES
• TIP: You must enter the URI for a path on the local machine prefaced with file:///, and any
spacesor special characters must be replaced with escape codes. (XML 336)
• TIP: Use the document() function when you are writing an XSLT 1.0-compliant style sheet. Use
the doc() function for XSLT 2.0 style sheets or when you are using XPath for non-XSLT applications
such as database queries. (XML 336)
• ProSkills: Problem Solving: Checking for the Existence of an External Document (XML 338)
FIGURES
• Figure 6-8, Figure 6-9
TEACHER TIP
Emphasize to students the value of checking for the existence of an external document, as described in the
ProSkills box on page XML 338. It can be frustrating to spend considerable effort writing code to access data
from an external file, only to have the transformation fail due to a missing file. Building in such a check
adds an extra level of robustness to an application when executed.
CLASSROOM ACTIVITIES
1. Group Discussion:
2. Quick Quiz:
• True/False: The data retrieved using the unparsed-text() function is read as a long text string.
(Answer: True)
• True/False: Files accessed with the document() and doc() functions do not have to be well-
formed XML documents. (Answer: False)
LAB ACTIVITY
Provide students with a sample XML file and a sample text file, and have them practice creating XSLT
style sheets to retrieve data from each type of file. Students should share their results with the class, and
discuss the differences in the code used and results as displayed when using the different source
documents.
• Review the use of the import element as a method of accessing an external style sheet, and contrast
its use with that of the include element
BOXES
• TIP: If you’re concerned about name conflicts and want the active style sheet to always have
precedence over the external sheet, always use the import element. (XML 339)
• Reference: Including and Importing Style Sheets (XML 339)
• Review: Session 6.1 Quick Check (XML 341)
FIGURES
• Figure 6-10, Figure 6-11
TEACHER TIP
Emphasize to students the process used by a processor to determine priority of two conflicting style rules
when an external style sheet is used. When the processor has to resolve conflicts between two style
components, it picks the one that is defined last in the style sheet. Thus, if you place the include element
at the end of your style sheet it will have precedence over the active style sheet; on the other hand, when
the include element is placed at the top of the style sheet, any components in the active style sheet will
have precedence.
CLASSROOM ACTIVITIES
1. Group Activity: Provide students with a sample XML document and XSLT style sheet. Ask them to
create a new style sheet that will use styles from the external style sheet. In one copy, they should
include the external style sheet, and in another copy they should import the file. As a class, review
any differences or unexpected results that occur.
2. Quick Quiz:
• True/False: The import element can be placed anywhere within the style sheet as long as it is a
child of the stylesheet element. (Answer: False)
• True/False: Including a style sheet has the same effect as inserting the style sheet code at the
location where the include element is placed. (Answer: True)
FIGURES
• Figure 6-12, Figure 6-13, Figure 6-14, Figure 6-15
CLASSROOM ACTIVITIES
1.Class Discussion: Ask students to suggest real world scenarios where a lookup table would be a
valuable resource for an XSLT application. What are some useful and unique features of a result
document you could implement through the use of lookup tables?
2.Quick Quiz:
• True/False: The current()function can be used to look up values from a data source. (Answer:
True)
BOXES
• TIP: If XPath is unable to calculate a value because of an error in the style sheet, it will returnthe
text string "NaN" (Not a Number). (XML 347)
• InSight: Using the number Element (XML 349)
FIGURES
• Figure 6-16, Figure 6-17,Figure 6-18
CLASSROOM ACTIVITIES
1. Critical Thinking: Explore as a group the contrast in function between the position()function and
the number element. The position() function returns the position of the node as it is displayed
within theresult document, not necessarily the position of the node in the source document. The
number element counts nodes based on the source document, even if you sort the nodes in the result
document, the value returned by the number element will still reflect the original order from the
source file. Ask students to propose situations when each approach would be appropriate.
2. Quick Quiz:
• True/False: The number element returns the position of the node as it is displayed within the
result document, not necessarily the position of the node in the source document. (Answer:
False)
• True/False: The ceiling(number) function rounds a number up to the next integer. (Answer:
True)
LAB ACTIVITY
Provide students with a sample XML source document containing a variety of numeric data. Ask them
to create an XSLT style sheet that uses each of the eight XPath 1.0 numeric functions listed in Figure 6-
16 to manipulate the sample data. Ask students to share their results, and highlight unique uses or
combinations of the functions.
FIGURES
• Figure 6-19, Figure 6-20, Figure 6-21, Figure 6-22
TEACHER TIP
Remind students that mathematical division can only be done with the divkeyword and not the / symbol
becausethat symbol is reserved by XPath for use in location paths.
CLASSROOM ACTIVITIES
1. Group Activity: Provide students with a sample XML source document containing a variety of
numeric data. Ask them to create an XSLT style sheet that uses each of the six XPath 1.0 mathematical
operators listed in Figure 6-19 to manipulate the sample data. Ask students to share their results, and
highlight unique uses or combinations of the operators.
2. Quick Quiz:
• True/False: XPath 2.0 does not support the XPath 1.0 numeric functions. (Answer: False)
• True/False: Mathematical division can only be done with the div keyword and not the / symbol.
(Answer: True)
BOXES
• Reference: Formatting Numeric Values (XML 353)
FIGURES
• Figure 6-23, Figure 6-24, Figure 6-25
CLASSROOM ACTIVITIES
1. Group Activity: Provide students with a sample XML source document containing a variety of
numeric data. Ask them to create an XSLT style sheet that uses each of the eight number format
symbols listed in Figure 6-23 to manipulate the sample data. Ask students to share their results, and
highlight unique uses or combinations of the format symbols.
2. Quick Quiz:
• True/False: The decimal-format element can be placed within a template. (Answer: False)
• True/False: XPath 1.0 and XSLT 1.0 do not support date formats. (Answer: True)
BOXES
• TIP: Be sure to include white space characters or text separators within your concatenation or else
the concatenated text strings will run into each other. (XML 356)
• TIP: Do not use the entity even if you are generating HTML code because that entity will
not be recognized by the XSLT processor. (XML 360)
• ProSkills: Written Communication: Removing Extraneous White Space (XML 360)
• Review: Session 6.2 Quick Check (XML 361)
FIGURES
• Figure 6-26, Figure 6-27, Figure 6-28, Figure 6-29, Figure 6-30
CLASSROOM ACTIVITIES
1. Class Discussion: The text illustrates the use of text string manipulation with the example of
formatting a date string. As a group, ask students to suggest other real world scenarios where it would
be useful to extract, combine, and/or format text string data from an XML source document. Consider
the brainstorming code examples that could be used to achieve the proposed scenarios.
2. Quick Quiz:
• True/False: If you want to insure that white space nodes are not deleted, you can apply the
normalize-space element as a direct child of the stylesheet element. (Answer: False,
preserve-space)
• True/False: XPath 2.0 includes several new functions for manipulating text strings and working
with dates and durations. (Answer: True)
LAB ACTIVITY
Provide students with a sample XML source document containing a variety of text string data. Ask them
to create an XSLT style sheet that uses at least four of the eight XPath 1.0 test string functions listed in
Figure 6-26 to manipulate the sample data. Ask students to share their results, and highlight unique uses
or combinations of the text string functions.
LECTURE NOTES
• Introduce the use of parameters as a way to overcome the limitation of XSLT variables that
mandates they only be defined once and only from within the style sheet
• Describe parameters as similar to variables except that their values can be passed to them from
outside their scope
• Differentiate between global and local parameters
• Introduce the role and syntax of the param element used in defining parameters
• Explain that because parameters have their values passed to them from outside their scope, global
parameters have their values set by the processor itself (and the exact method can vary by
processor)
• Review template parameters, and explain that, as with local variables, the scope of a template
parameter is limited to the template in which it is created
BOXES
• TIP: Parameters created in XSLT 2.0 also support the as attribute to define the data type of
theparameter value. (XML 364)
• Reference: Creating and Using Parameters (XML 364)
• TIP: Make sure that the parameter name matches the name of the parameter. If you mistype the
name, an XSLT processor does not pass the value but it also does not return an error message
indicating that a mistake was made. (XML 366)
• Reference: Passing a Value to a Template Parameter (XML 368)
FIGURES
• Figure 6-31, Figure 6-32, Figure 6-33
TEACHER TIP
The command code used to transform a document while setting a parameter value differs from the
command students have been using to this point. Reinforce with students the syntax of this command, and
provide examples of its use to reduce confusion.
CLASSROOM ACTIVITIES
1. Group Activity: Ask students to write code examples to define a local, global, and template
parameter. Share these examples, and discuss differences in the code used to define each and in the
scope of each parameter type. Ask students to suggest scenarios when the use of each type of
parameter might be indicated.
2. Quick Quiz:
• True/False: The parameter cannot be passed to a template parameter from outside of the
template. (Answer: False)
• True/False: The XSLT processors built into web browsers do not allow users to set parameter
values directly at this time. (Answer: True)
• Explain that because a named template is not matched to a node set, it has to be called rather than
applied, using the call-template element
BOXES
• Reference: Creating and Calling Named Templates (XML 369)
TEACHER TIP
Remind students that named templates have much in common with the root and element templates they
already have experience with. All must be separately defined and then called or applied, and all have a
similar basic foundational structure.
CLASSROOM ACTIVITIES
1. Critical Thinking Activity: The text illustrates the use of named templates with a function to insert an
image file into the result document. Ask students to suggest other possible practical uses for a named
template. If possible, have them try to write sample code to implement these suggestions.
2. Quick Quiz:
• True/False: The only element that the call-template element can contain is the with-param
element. (Answer: True)
• A(n) __________ template is a template that is not matched to a node set but instead acts like a
function to display a calculated value or perform an operation. (Answer: named)
BOXES
• ProSkills: Problem Solving: Choosing Functional Programming (XML 370)
• InSight: Returning Variables Values with Named Templates (XML 377)
• Review: Session 6.3 Quick Check
FIGURES
• Figure 6-34, Figure 6-35, Figure 6-36, Figure 6-37, Figure 6-38, Figure 6-39, Figure 6-40, Figure 6-
41, Figure 6-42, Figure 6-43, Figure 6-44, Figure 6-45
CLASSROOM ACTIVITIES
1. Class Discussion: Most students with some programming experience are likely more familiar with
procedural programming languages than functional programming. Review the key features of
functional programming introduced in this section, and ask students to suggest similarities and
differences to programming languages they have worked with in the past. Ask them to propose
applications that would be more suited to one approach or the other.
2. Quick Quiz:
• True/False: Looping is the process by which a function calls itself. (Answer: False, Recursion)
• True/False: Each template can be thought of as a function, with the input being the specified
node set and the output being the result text generated by XSLT. (Answer: True)
situations that also involve software application skills. ProSkills exercises are offered at various
points throughout a text, encompassing the concepts and skills presented in a standalone tutorial or
a group of related tutorials.
Top of Document