Disabling Output Escaping

Disabling Output Escaping

You can use <xsl:text> when you want characters such as < and & to appear in your output document, rather than &lt; and &amp;. To do that, set the <xsl:text> elements disable-output-escaping attribute to yes (the default is no). Heres an example where I write the text "<PLANET/>" to the output document directly, using <xsl:text> :

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:template match="PLANETS">      <HTML>          <HEAD>              <TITLE>                  Planets              </TITLE>          </HEAD>          <BODY>              <xsl:apply-templates select="PLANET"/>          </BODY>      </HTML>  </xsl:template>  <xsl:template match="PLANET">      <xsl:text disable-output-escaping = "yes">          &lt;PLANET/&gt;      </xsl:text>  </xsl:template>  </xsl:stylesheet> 

Here is the result:

 <HTML>      <HEAD>          <TITLE>              Planets          </TITLE>      </HEAD>      <BODY>        <PLANET/>        <PLANET/>        <PLANET/>    </BODY>  </HTML> 

It wasnt necessary to output <PLANET/> using <xsl:text> , of course; I could have placed that element directly into a literal result element. But what about cases where the XSLT processor wont recognize an element you need in your output as a true element? For example, in transitional XHTML documents, you need the element <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> , but XSLT processors complain that this is not well- formed XML. How can you place this element into the output?

You could try placing this element into a <!CDATA[ ]> section, as youll see in Chapter 6, and try to treat it as simple character data, but XSLT processors still invariably escape the < as &lt; and > as &gt;.

The proper way to add a <!DOCTYPE> element to the output is actually with the doctype-public attribute of the <xsl:output> element as youll see in Chapter 6, but as an example for demonstration purposes, Ill disable output escaping in <xsl:text> here to do the same thing (this is not the recommended way of creating <!DOCTYPE> elements in output documents). Heres how it looks:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:output method="xml"/>      <xsl:template match="/PLANETS">          <xsl:text disable-output-escaping="yes">              &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"&gt;          </xsl:text>          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <TABLE BORDER="2">                      <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>            <TD><xsl:apply-templates select="DAY"/></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> 

And heres the result:

 <?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">      <HTML>          <HEAD>              <TITLE>                  The Planets Table              </TITLE>          </HEAD>          <BODY>              <H1>                  The Planets Table                  .                  .                  . 

Youll see other uses for <xsl:text> throughout this book, including during the discussion of whitespace later in this chapter.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net