XSLT - Using <xsl:text> for Controlled Text Output in XSLT
The <xsl:text> element in XSLT is used to create and output a specific piece of text exactly as required in the transformation result. It is particularly useful when the stylesheet needs to generate fixed text, spaces, punctuation, line breaks, or other characters that should appear exactly in the output. Unlike dynamically selected values, the content inside <xsl:text> is treated as literal text by the XSLT processor.
Purpose of <xsl:text>
When an XSLT stylesheet transforms XML into HTML, XML, or plain text, developers often need to insert text that does not come directly from the source XML document. For example, a stylesheet may need to add labels such as "Name:", "Address:", or "Total:". The <xsl:text> instruction allows these characters to be explicitly placed in the output.
The basic syntax is:
<xsl:text>Text to be displayed</xsl:text>
For example:
<xsl:template match="/">
<html>
<body>
<xsl:text>Welcome to the XML Report</xsl:text>
</body>
</html>
</xsl:template>
The resulting output contains:
Welcome to the XML Report
The text is not obtained from the source XML. It is directly supplied by the XSLT stylesheet.
Why <xsl:text> Is Useful
Whitespace can sometimes be handled differently by XML and XSLT processors. Formatting the stylesheet with indentation and line breaks does not necessarily mean that those spaces and line breaks should become part of the transformation output. <xsl:text> provides explicit control over which text characters are produced.
Consider:
<xsl:text>Name: </xsl:text>
<xsl:value-of select="name"/>
If the source XML contains:
<student>
<name>Rahul</name>
</student>
the output becomes:
Name: Rahul
Here, <xsl:text> produces the fixed text Name: , while <xsl:value-of> retrieves the student's name from the source document.
Controlling Spaces
One important use of <xsl:text> is controlling spaces that need to be present in the output.
For example:
<xsl:text>Hello </xsl:text>
<xsl:value-of select="name"/>
The space after Hello is deliberately included inside <xsl:text>. If the name is Anita, the result is:
Hello Anita
This is useful when constructing sentences or combining several pieces of output.
Adding Punctuation
<xsl:text> can also be used for punctuation marks.
<xsl:value-of select="firstName"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="lastName"/>
If the source contains:
<firstName>Arun</firstName>
<lastName>Kumar</lastName>
the result is:
Arun, Kumar
The comma and space are explicitly generated by <xsl:text>.
Adding Line Breaks
When producing text-based output, <xsl:text> can be used to create line breaks.
For example:
<xsl:text>Student Details:
</xsl:text>
<xsl:value-of select="name"/>
The output can appear as:
Student Details:
Arun
For more precise control in text-oriented transformations, the stylesheet can also use character references where appropriate.
<xsl:text> with Dynamic Content
<xsl:text> is intended for literal text. It does not evaluate XPath expressions inside its content.
For example:
<xsl:text>Name: </xsl:text>
<xsl:value-of select="name"/>
is correct because the fixed label is generated with <xsl:text> and the dynamic value is obtained using <xsl:value-of>.
It would not be appropriate to write:
<xsl:text>Name: <xsl:value-of select="name"/></xsl:text>
because <xsl:text> is designed for text content rather than containing other XSLT instructions.
Difference Between <xsl:text> and <xsl:value-of>
These two instructions serve different purposes.
<xsl:text> produces literal text specified in the stylesheet.
<xsl:text>Student Name: </xsl:text>
Output:
Student Name:
<xsl:value-of> produces a value obtained from the source document or an XPath expression.
<xsl:value-of select="student/name"/>
If the source contains <name>Priya</name>, the output is:
Priya
They are commonly used together:
<xsl:text>Student Name: </xsl:text>
<xsl:value-of select="student/name"/>
Result:
Student Name: Priya
Example with XML Transformation
Suppose the source XML is:
<student>
<name>Ravi</name>
<course>Computer Science</course>
</student>
An XSLT stylesheet could contain:
<xsl:template match="/">
<result>
<xsl:text>Student Name: </xsl:text>
<xsl:value-of select="student/name"/>
<xsl:text> | Course: </xsl:text>
<xsl:value-of select="student/course"/>
</result>
</xsl:template>
The generated result is:
<result>Student Name: Ravi | Course: Computer Science</result>
The labels, spaces, colon, and vertical bar are literal text created with <xsl:text>, whereas the student's actual information comes from the source XML.
Important Characteristics
The <xsl:text> element has several important characteristics:
-
It creates literal text in the transformation result.
-
Its content is not treated as an XPath expression.
-
It provides explicit control over whitespace.
-
It can be used for labels, punctuation, spaces, and fixed messages.
-
It is commonly combined with
<xsl:value-of>. -
It is useful when generating HTML, XML, or text output.
-
It makes the intention to generate particular text clear within the stylesheet.
Conclusion
<xsl:text> is a simple but useful XSLT instruction for producing controlled literal text. Its main advantage is that it clearly distinguishes fixed output from values obtained dynamically from the source XML. It is especially valuable when precise spaces, punctuation, labels, or line breaks are required. By combining <xsl:text> with instructions such as <xsl:value-of>, an XSLT stylesheet can produce well-structured and predictable transformation results.