spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / authoring / languages / xml / insidexslt / chap2 / 5 To page 1To page 2To page 3current page
[previous]

Inside XSLT

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


Output Method: Text

The text output method is not just for creating plain text; it's used for any non-XML, non-HTML text-based format. For example, you can use it to create Rich Text Format (RTF) documents. Rich Text Format uses embedded text-based codes to specify the format of documents, and you can place those text-based codes in documents yourself if you use the text output method.

Here's an example stylesheet (that you'll see in Chapter 6) that converts planets.xml into planets.rtf:

Listing 2.6: RTF Stylesheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
<xsl:template match="/PLANETS">{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0
Courier New;}}
\viewkind4\uc1\pard\lang1033\b\f0\fs36 The Planets Table\par
\b0\fs20
Name\tab Mass\tab Rad.\tab Day\par
<xsl:apply-templates/>
\par
}</xsl:template>
<xsl:template match="PLANET">
<xsl:value-of select="NAME"/>
\tab
<xsl:value-of select="MASS"/>
\tab
<xsl:value-of select="RADIUS"/>
\tab
<xsl:value-of select="DAY"/>
\tab
\par
</xsl:template>
</xsl:stylesheet>

You can see the resulting RTF document, planets.rtf, in Figure 2.3 in Microsoft Word 2000.

Note that I've set the output method to text with <xsl:output method="text"/>:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/PLANETS">{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}
\viewkind4\uc1\pard\lang1033\b\f0\fs36 The Planets Table\par
        .
        .
        .

You might also note that I've started the RTF codes immediately after the <xsl:template> element. I've done that because RTF documents must start with RTF codes from the very beginning; if I had begun inserting RTF codes on the next line, like this:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/PLANETS">
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}}
\viewkind4\uc1\pard\lang1033\b\f0\fs36 The Planets Table\par
        .
        .
        .

then the RTF output file would have started with a newline character, which would throw off the RTF application like Microsoft Word. You'll learn more on RTF and other formats in Chapter 6.

Figure 2.3. Planets.rtf in Microsoft Word.
Figure 2.3 Planets.rtf in Microsoft Word.


home / authoring / languages / xml / insidexslt / chap2 / 5 To page 1To page 2To page 3current page
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

Created: October 11, 2001
Revised: October 11, 2001


URL: http://webreference.com/authoring/languages/xml/insidexslt/chap2/5/4.html