The xsl:text Element

The <xsl:text> Element

Handling spaces is always something of an involved topic in XSLT, and Ill spend a little time on it in this chapter. Inserting a single space, , isnt difficult if you use the <xsl:text> element, which you use to insert literal text directly into the output tree. This element only has one attribute:

  • disable-output-escaping . Set to yes to make sure characters such as < and > are output literally rather than as &lt; and &gt;. The default is no.

This element can contain only a text node.

You create text nodes with the <xsl:text> element, allowing you to do things such as replace whole elements with text on the fly. One reason to use <xsl:text> is to preserve whitespace, as in the following example, where Ill use <xsl:text> to insert spaces:

Listing 3.4 Adding Spaces in a Stylesheet
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="">      <xsl:template match="/PLANETS">          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <TABLE>                      <TD>Name</TD>                      <TD>Mass</TD>                      <TD>Radius</TD>                      <TD>Day</TD>                      <xsl:apply-templates/>                  </TABLE>              </BODY>          </HTML>      </xsl:template>      <xsl:template match="PLANET">         <TR>          <TD><xsl:value-of select="NAME"/></TD>          <TD><xsl:apply-templates select="MASS"/></TD>          <TD><xsl:apply-templates select="RADIUS"/></TD>        </TR>     </xsl:template>      <xsl:template match="MASS">        <xsl:value-of select="."/>        <xsl:text> </xsl:text>        <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="RADIUS">        <xsl:value-of select="."/>        <xsl:text> </xsl:text>        <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="DAY">        <xsl:value-of select="."/>        <xsl:text> </xsl:text>        <xsl:value-of select="@UNITS"/>      </xsl:template>  </xsl:stylesheet> 

You can see the new result in Figure 3.2, where you can see the spaces inserted between the numeric values and their units.

Figure 3.2. Displaying attribute values, second attempt.

As you can see, the <xsl:text> element is a useful one. However, theres one thing you should know: By default, <xsl:text> elements escape characters that could be part of markup. For example, <xsl:text>Here is a greater-than sign:></xsl:text> gets written as Here is a greater-than sign: &gt;, not Here is a greater-than sign: >. And if you try to use a < inside an <xsl:text> element, XSLT processors think that youre trying to enclose an element inside an <xsl:text> element, which is illegal. So how do you send sensitive characters such as < and > to the output if you really need to? You can do that by disabling output escaping .

Upcoming in XSLT 2.0

One of the issues that XSLT 2.0 is supposed to address is how to make it easier to import unparsed text of the kind youve seen here from other files.

Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
Year: 2005
Pages: 196

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: