Skip to content

XSLT <xsl:text>

How to use the XSLT <xsl:text> element to output literal text, control character escaping, and format XML transformations with precise text handling.

XSLT <xsl:text>

The <xsl:text> element is used to write literal text to the output.

Syntax

<xsl:text disable-output-escaping="yes|no">    
	<!-- Content:#PCDATA -->
</xsl:text>

Attributes

Attribute Value Required/Optional Description
disable-output-escaping yes | no Optional "yes" indicates that special characters (like <) should be output as is. "no" indicates that special characters (like <) should be output as &lt;. Default is "no".

This attribute is not supported by Netscape 6

Example

Consider the XML document about a bookstore shown below:

books.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>

<bookstore>
	<book category="children">
		<title lang="en">Harry Potter</title>
		<author>J K. Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
	<book category="web">
		<title lang="en">XQuery Tips</title>
		<author>Tom Nolan</author>
		<year>2022</year>
		<price>39.99</price>
	</book>
	<book category="web">
		<title lang="en">Learn XML</title>
		<author>Tutorial Reference</author>
		<year>2022</year>
		<price>9.99</price>
	</book>
</bookstore>

We want to display the title of each book. Inserts a ", " between each book title if it is not the last book or the last but one. If it is the last book, it will add a "!" behind the title. If it is the last but one, add a ", and " behind the title:

books.xslxml
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <h2>The bookstore</h2>
    <p>Titles:
    <xsl:for-each select="bookstore/book">
      <xsl:value-of select="title"/>
      <xsl:if test="position() < last()-1">
        <xsl:text>, </xsl:text>
      </xsl:if>
      <xsl:if test="position()=last()-1">
        <xsl:text>, and </xsl:text>
      </xsl:if>
      <xsl:if test="position()=last()">
        <xsl:text>!</xsl:text>
      </xsl:if>
    </xsl:for-each>
    </p>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
Navigation