WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 5 (4/4)
[previous] |
Inside XSLT
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="https://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="https://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="https://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.
[previous] |
Created: October 11, 2001
Revised: October 11, 2001
URL: https://webreference.com/authoring/languages/xml/insidexslt/chap2/5/4.html