XSLT - XSLT <xsl:text>: Generating Text Nodes
The <xsl:text> element in XSLT is used to create a text node explicitly in the output document. It is especially useful when you need precise control over text, whitespace, line breaks, indentation, or special characters in the generated output.
Unlike ordinary text written directly inside an XSLT template, <xsl:text> tells the XSLT processor that the content should be treated exactly as text and copied to the result.
1. Basic Syntax
The basic syntax is:
<xsl:text>Text to be generated</xsl:text>
For example:
<xsl:template match="/">
<message>
<xsl:text>Welcome to XSLT</xsl:text>
</message>
</xsl:template>
The output will be:
<message>Welcome to XSLT</message>
Here, Welcome to XSLT is explicitly created as a text node.
2. Why Use <xsl:text>?
XSLT stylesheets contain both instructions and literal text. In some situations, whitespace in the stylesheet can be interpreted or stripped by the XSLT processor. <xsl:text> provides a reliable way to specify exactly what text should appear in the output.
For example:
<xsl:text>Hello </xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>!</xsl:text>
If the source XML contains:
<student name="Rahul"/>
The result can be:
Hello Rahul!
The <xsl:text> elements provide the fixed portions of the output, while <xsl:value-of> retrieves dynamic content from the source XML.
3. Combining <xsl:text> with <xsl:value-of>
One of the most common uses of <xsl:text> is to combine fixed text with values obtained from the source XML.
Consider this XML:
<student>
<name>Anita</name>
<course>Computer Science</course>
</student>
An XSLT stylesheet could contain:
<xsl:template match="student">
<p>
<xsl:text>Student Name: </xsl:text>
<xsl:value-of select="name"/>
<xsl:text>, Course: </xsl:text>
<xsl:value-of select="course"/>
</p>
</xsl:template>
The output is:
<p>Student Name: Anita, Course: Computer Science</p>
Here:
-
Student Name:is fixed text. -
nameis retrieved from the source XML. -
, Course:is fixed text. -
courseis retrieved from the source XML.
This makes <xsl:text> particularly useful for creating readable sentences from XML data.
4. Controlling Whitespace
Whitespace is an important reason for using <xsl:text>.
Consider:
<xsl:text> </xsl:text>
This explicitly creates a single space.
For example:
<xsl:value-of select="firstName"/>
<xsl:text> </xsl:text>
<xsl:value-of select="lastName"/>
If the source contains:
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
</person>
The result is:
John Smith
Without explicitly controlling the space, the output might not have the exact formatting you intended.
5. Creating Line Breaks
<xsl:text> can also be used when a stylesheet needs to generate specific whitespace or line-break characters.
For example:
<xsl:text>
</xsl:text>
can be used to create a newline in appropriate output contexts.
Another approach is to use character references when precise control is required.
For example:
<xsl:text> </xsl:text>
The character reference represents a line-feed character.
This can be useful when generating text-based output such as CSV or plain-text reports.
6. Using Special Characters
<xsl:text> can contain XML character references for characters that need special representation.
For example:
<xsl:text><student></xsl:text>
produces:
<student>
Here:
-
<represents< -
>represents>
Similarly:
<xsl:text>&</xsl:text>
produces:
&
This is important because < and & have special meanings in XML syntax.
7. Generating Symbols and Punctuation
<xsl:text> is also useful for adding punctuation or symbols to generated content.
For example:
<xsl:text>Name: </xsl:text>
<xsl:value-of select="name"/>
<xsl:text> | Age: </xsl:text>
<xsl:value-of select="age"/>
If the source contains:
<person>
<name>David</name>
<age>25</age>
</person>
The result is:
Name: David | Age: 25
The fixed labels and punctuation are generated using <xsl:text>.
8. <xsl:text> Versus Literal Text
XSLT allows ordinary text to be written directly inside a template.
For example:
<xsl:template match="/">
Hello World
</xsl:template>
This can produce:
Hello World
However, when whitespace needs to be controlled carefully, <xsl:text> is preferable:
<xsl:template match="/">
<xsl:text>Hello World</xsl:text>
</xsl:template>
The second approach explicitly identifies the content as text and gives the stylesheet author better control over whitespace.
9. Using <xsl:text> in HTML Generation
Suppose an XSLT stylesheet generates an HTML page:
<xsl:template match="student">
<p>
<xsl:text>Welcome, </xsl:text>
<xsl:value-of select="name"/>
<xsl:text>!</xsl:text>
</p>
</xsl:template>
For:
<student>
<name>Priya</name>
</student>
The generated HTML is:
<p>Welcome, Priya!</p>
This technique is commonly used when fixed phrases need to be combined with values from XML.
10. Using <xsl:text> for CSV Output
<xsl:text> becomes particularly useful when generating CSV or other text-based formats.
For example:
<xsl:value-of select="name"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="age"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="city"/>
If the source contains:
<person>
<name>Rahul</name>
<age>28</age>
<city>Bengaluru</city>
</person>
The result can be:
Rahul,28,Bengaluru
Here, the commas are explicitly generated using <xsl:text>.
11. Using <xsl:text> for Separators
Separators are frequently required when displaying lists.
For example:
<xsl:for-each select="students/student">
<xsl:value-of select="name"/>
<xsl:text>, </xsl:text>
</xsl:for-each>
If the XML contains three students, the result could be:
Anita, Rahul, Priya,
However, this example also demonstrates an important issue: a separator is produced after every item, including the last one.
A better approach is:
<xsl:for-each select="students/student">
<xsl:value-of select="name"/>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
The result becomes:
Anita, Rahul, Priya
Here, <xsl:text> creates the comma and space only between items.
12. <xsl:text> and Whitespace Preservation
One important characteristic of <xsl:text> is that whitespace within it is significant.
For example:
<xsl:text> Hello World </xsl:text>
contains spaces before and after the text. Those spaces are part of the text node.
This makes <xsl:text> useful when exact output formatting is important.
For example:
<xsl:text>First Name: </xsl:text>
<xsl:value-of select="firstName"/>
The space after the colon is deliberately included so that the output becomes:
First Name: Anita
instead of:
First Name:Anita
13. <xsl:text> and Text-Based Reports
XSLT is not limited to generating XML and HTML. It can also generate plain-text documents.
For example:
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:text>Student Report</xsl:text>
<xsl:text> </xsl:text>
<xsl:text>----------------</xsl:text>
<xsl:text> </xsl:text>
<xsl:text>Name: </xsl:text>
<xsl:value-of select="student/name"/>
<xsl:text> </xsl:text>
<xsl:text>Course: </xsl:text>
<xsl:value-of select="student/course"/>
</xsl:template>
The output could be:
Student Report
----------------
Name: Anita
Course: Computer Science
In this example, <xsl:text> controls the labels, separators, spaces, and line breaks.
14. Important Difference Between <xsl:text> and <xsl:value-of>
These two instructions have different purposes.
<xsl:text> provides text that is already known when the stylesheet is written.
<xsl:text>Student Name: </xsl:text>
<xsl:value-of> retrieves a value from the source XML or evaluates an expression.
<xsl:value-of select="student/name"/>
For example:
<xsl:text>Student Name: </xsl:text>
<xsl:value-of select="student/name"/>
produces something like:
Student Name: Anita
The first part is static, while the second part is dynamic.
15. Advantages of <xsl:text>
The main advantages are:
-
Precise text control
It allows the stylesheet author to explicitly specify output text. -
Better whitespace control
Spaces and other whitespace can be deliberately included. -
Useful for text-based output
It is especially helpful when generating plain-text reports and CSV files. -
Works well with dynamic values
It can be combined with<xsl:value-of>and other XSLT instructions. -
Improves stylesheet clarity
It makes it clear that a particular piece of content is intended to be output as text. -
Useful for punctuation and separators
Commas, spaces, colons, hyphens, and other fixed characters can be generated explicitly.
16. Example Combining Multiple XSLT Instructions
Consider the following XML:
<employee>
<firstName>Ravi</firstName>
<lastName>Kumar</lastName>
<department>Finance</department>
</employee>
The XSLT can be:
<xsl:template match="employee">
<report>
<xsl:text>Employee: </xsl:text>
<xsl:value-of select="firstName"/>
<xsl:text> </xsl:text>
<xsl:value-of select="lastName"/>
<xsl:text> </xsl:text>
<xsl:text>Department: </xsl:text>
<xsl:value-of select="department"/>
</report>
</xsl:template>
The resulting content contains:
Employee: Ravi Kumar
Department: Finance
This demonstrates the primary role of <xsl:text>: it provides the fixed textual portions while other XSLT instructions supply dynamic information.
Conclusion
<xsl:text> is an XSLT instruction used to create explicit text nodes in the transformation output. It is particularly valuable when exact control over spaces, punctuation, separators, line breaks, and other fixed text is required. It is frequently combined with <xsl:value-of> to construct meaningful sentences, reports, HTML content, CSV data, and plain-text output from XML.
For simple text, literal text in a template may be sufficient. However, when precise whitespace and text control are important, <xsl:text> provides a clear and reliable solution.