XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (5 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
4.5Mb size Format: txt, pdf, ePub

Errata

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, such as a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save another reader hours of frustration and at the same time you will be helping us provide even higher-quality information.

To find the errata page for this book, go to
http://www.wrox.com
and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that have been submitted for this book and posted by Wrox editors. A complete book list, including links to each book's errata, is also available at
www.wrox.com/misc-pages/booklist.shtml
.

If you don't spot “your” error on the Book Errata page, go to
www.wrox.com/contact/techsupport.shtml
and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

p2p.wrox.com

For author and peer discussion, join the P2P forums at
p2p.wrox.com
. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At
http://p2p.wrox.com
you will find a number of different forums that will help you not only as you read this book but also as you develop your own applications. To join the forums, just follow these steps:

1.
Go to
p2p.wrox.com
and click the Register link.

2.
Read the terms of use and click Agree.

3.
Complete the required information to join as well as any optional information you wish to provide, and click Submit.

4.
You will receive an e-mail with information describing how to verify your account and complete the joining process.

You can read messages in the forums without joining P2P but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

Here are some tips for writing a question if you want a good answer:

1.
Choose your subject line carefully. Not just “XSLT question”.

2.
Don't use text shorthand. Not everyone has English as their first language, but if you take care over writing your question, it's much more likely that someone will take care over answering it.

3.
Show a complete source document, a complete example of your required output, and if you want to know why your code doesn't work, your complete code—but only after paring the problem down to its essentials. Don't ask people to debug code that they can't see.

4.
If you tried something and it didn't work, say exactly what you tried and exactly how it failed (including details of what products you are using).

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

List of Examples

This list includes all the worked examples in the book: that is, the examples consisting of entire stylesheets, for which working code can be downloaded from
http://www.wrox.com/
. It does not include the many examples that are provided as incomplete snippets.

The purpose of this list is to help you out when you know that you've seen an example somewhere that is relevant to your current problem, but you can't remember where you saw it.

Chapter 1

Description
Page
A Hello World Stylesheet
This stylesheet creates an HTML containing a greeting (such as “Hello, World!”) read from the source XML document.
11
Tabulating Word Frequencies
Given any XML document, this stylesheet produces a list of the words that appear in its text, giving the number of times each word appears, together with its frequency. This illustrates features in XSLT 2.0 for analyzing text using regular expressions, and for grouping based on common values.
19
Displaying a Poem
This stylesheet shows how to use template rules to render the text of a poem in HTML.
35

Chapter 2

Description
Page
An XML Tree
This example shows how an XML document is represented as a tree in the data model.
46
Push Processing
This stylesheet shows the use of template rules to display a list of books in HTML, using one template rule for each different kind of node.
74
Controlling the Sequence of Processing
This example shows a refinement of the previous stylesheet in which the
select
attribute of

is used to control the sequence of different parts of the output.
77
Selecting Nodes Explicitly
This shows a further refinement of the book list stylesheet in which some aspects of the output are generated using

instructions.
77

Chapter 3

Description
Page
Using
This example shows the use of

to split a stylesheet into three modules performing distinct tasks: one to format the current date, one to construct a copyright statement, and one to control the rest of the processing.
91
Using
This stylesheet extends the previous example, showing how

allows some of the declarations in one module to be overridden in the importing module.
95
Embedded Stylesheets
This example shows the use of a stylesheet embedded within the source document that it is designed to display.
102
A Simplified Stylesheet
This is an example of a simplified stylesheet module, that is, a stylesheet whose outermost element is a literal result element rather than an

element.
125
Using An Extension Instruction
This stylesheet uses Saxon's

element to illustrate how a stylesheet can call extension instructions provided by a vendor or third party.
139

Chapter 4

Description
Page
Validating the Source Document
In this example a stylesheet takes advantage of the fact that the source document can be validated against a schema before the transformation starts.
67
Validating the Result Document
This stylesheet invokes validation of the result document using the

option of the

instruction.
71

Chapter 6

Description
Page
Using Modes
The example uses a mode to display a list of characters appearing in a play.
247
Checking for Cycles in a Graph
This example provides a generic procedure to look for cycles in a graph, and then applies this procedure to a data file to see if the ID/IDREF links are cyclic. It illustrates the use of

to simulate a higher-order function.
251
Generating an Attribute Conditionally
This example shows the use of

to generate an attribute only when certain conditions are true.
263
Deciding the Attribute Name at Runtime
This example shows the use of

to generate an attribute whose name is not known until execution time.
264
Using an Attribute Set for Numbering
This stylesheet shows an unusual way of using attribute sets, to illustrate that the attributes generated by an attribute set do not need to have fixed values.
270
Using Recursion to Process a Sequence of Nodes
This example illustrates how to use a recursive named template to process a sequence of nodes. It uses this technique to find the longest speech in a play.
275
Using Recursion to Process a Sequence of Strings
This example uses a recursive named template to process a sequence of strings, obtained by splitting a line of text into its constituent words. It uses this technique to find phrases of the form “A and B”, where A and B are both the names of characters in a play.
278
Using for Repeated Output
This stylesheet uses

to generate a standard table heading that appears repeatedly in the HTML output document (a listing of soccer matches).
294
Converting Attributes to Child Elements
This example illustrates how

can be used to create element nodes whose names and content are taken from the names and values of attributes in the source document.
315
Showing the Ancestors of a Node
This example stylesheet can be applied to any XML document. For each element it processes all its ancestor elements, in reverse document order (that is, starting with the parent node and ending with the document element), and outputs their names to a comment that shows the position of the current node.
325
Single-Level Grouping by Value
This stylesheet uses

to group a set of employees according to the department in which they work.
331
Multilevel Grouping by Value
This example groups employees by department, and groups the departments by location.
333
Grouping Consecutive Elements by Name
This example shows how the
group-adjacent
attribute of

can be used to group adjacent elements having the same element name. It applies this technique to the adjacent

and

elements in a Shakespeare play.
336
Handling Repeating Groups of Adjacent Elements
This takes the previous example and makes the problem more difficult, by removing another layer of markup from the source document, so that it is necessary to infer multiple levels of hierarchic structure from the pattern of leaf elements in the tree.
338
Handling Flat XHTML Documents
This example shows how to use the
group-starting-with
attribute of

to process the implicit structure of an XHTML source document, in which an

element is followed by a number of


elements that are logically subordinate to the

element, but actually appear as its siblings.

340
Calculating Annual Leave
This is a simple illustration of the use of stylesheet functions to structure a computation in a stylesheet. It uses two user-defined functions, a function to calculate the annual leave entitlement of an employee, which in turn calls a function to calculate the employee's length of service given a starting and ending date.
347
Looking for Cycles Among Attribute Sets
This example illustrates the use of recursive stylesheet functions to analyze a graph structure. Specifically, it analyzes an XSLT stylesheet as its source document, to determine whether there are any cyclic dependencies among the attribute set definitions in the stylesheet.
352
Formatting a List of Names
This example shows the use of

to produce punctuation between the items in a list, where the punctuation depends on the position of the item within the list.
356
Precedence of Variables
This example shows how an importing stylesheet module can declare global variables with the same names as variables within the imported module, and how the variables in the importing module take precedence.
364
Precedence of Template Rules
This example shows how an importing stylesheet module can declare template rules that override template rules within the imported module. It also shows the use of

in an overriding template rule, to invoke the functionality of the template rule that was overridden.
365
Using with Named Attribute Sets
This example illustrates the use of

to incorporate declarations (in this case, attribute set declarations) from one stylesheet module into another.
375
Multivalued non-Unique Keys
This example shows the use of an

definition in which several elements can have the same key value, and a single element can have multiple key values. Specifically, this situation arises when books are indexed by the names of their authors.
381
Generating a Stylesheet using
This example shows the way in which

can be used when writing a stylesheet whose task is to generate (or modify) another XSLT stylesheet.
398
Identifying Location of Text within a Document
This example is adapted from the stylesheet used to produce the errata for the XSLT and XPath specifications. It searches the published document for a given phrase, and then identifies all the places where the phrase occurs, using

to generate a description of the location of the text using a phrase such as “first numbered list, second item, third paragraph”.
413
Numbering the Lines of a Poem
This stylesheet uses

to selectively number the lines of a poem.
417
Using with a Default Value
This is an example of a named template that defines a default value for an optional template parameter. The stylesheet can be run against any source document, and displays the depth of nesting of the nodes in that document.
432
Tunnel Parameters
This example shows how tunnel parameters can be useful when customizing an existing stylesheet, for reducing the number of rules in the existing stylesheet that need to be modified. The specific example shows how to produce a modified rendition of a Shakespeare play in which the lines for each actor are highlighted.
433
Creating Multiple Output Files
This stylesheet uses the

instruction to split a source document into multiple result documents. In this case, each stanza of a poem ends up in a separate document.
451
Sorting on the Result of a Calculation
This example uses

to sort items on a value that does not appear explicitly in either the source or result documents, but is computed. (It produces a sales report for different flavors of jam).
463
Template Rules
This is an illustration of the classic use of template rules to control the rendition of narrative text, in this case, reviews of classical concerts.
488
Using Modes
This stylesheet modifies the previous example showing how modes can be used to process the same source data in more than one way, using different template rules. It illustrates this by including at the end of each concert review a summary of the works performed in the concert.
490
Using a Variable for Context-Sensitive Values
This stylesheet (used to publish a series of opera performances) shows how a variable can be used to hold on to information that depends on the context, for use when the context has changed.
508
A Multiphase Transformation
An example of a stylesheet that uses variables to capture the result of one phase of processing, so that further processing can be carried out before delivering the final result. In this case the first phase calculates the results of a soccer tournament, and the second phase displays these results as HTML.
511

Chapter 12

Description
Page
Using the key() Pattern to Format a Specific Node
This stylesheet shows how a match pattern for a template rule can use the
key()
function to apply distinctive formatting to one selected node in the source document.
706

Other books

No Greater Love by Eris Field
Fuck Valentine's Day by C. M. Stunich
Coming Home by Priscilla Glenn
30 - It Came from Beneath the Sink by R.L. Stine - (ebook by Undead)
Dead Peasants by Larry D. Thompson