Chapter 2. Building New Documents with XSLT

In the first chapter of this book, you got acquainted with the basics of how XSLT works. This chapter will take you a few steps further by showing you how to add text and markup to your result tree with XSLT templates.

First, you’ll add literal text to your output. Then you’ll work with literal result elements, that is, elements that are represented literally in templates. You’ll also learn how to add content with the text, element, attribute, attribute-set, comment, and processing-instruction elements. In addition, you’ll get your first encounter with attribute value templates, which provide a way to define templates inside attribute values.

Outputting Text

You can put plain, literal text into an XSLT template, and it will be written to a result tree when the template containing the text is processed. You saw this work in the very first example in the book (msg.xsl in Chapter 1). I’ll go into more detail about adding literal text in this section.

Look at the single-element document text.xml in examples/ch02 (this directory is where all example files mentioned in this chapter can be found):

<?xml version="1.0"?>
  
<message>You can easily add text to your output.</message>

With text.xml in mind, consider the stylesheet txt.xsl:

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
<output method="text"/>
  
<template match="/">Message: <apply-templates/></template>

</stylesheet>

When applied to text.xml, here is what generally happens, ...

Get Learning XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.