3.4 Outputting Text

The text output method lets an XSLT processor know that you intend to output plain text to the result. You have already seen simple examples that do this previously in the book. This example shows you how to output programming language text using the text method. If you are not a programmer, this section may be a little tough to follow. You can skip it if programming makes you queasy or if you aren't interested in .NET, although the same approach can be used to generate Java, VisualBasic, COBOL, or the language of your choice.

Now, I'll show you how you can use XSLT to write a program in the C# programming language. The stylesheet csharp.xsl uses the text output method:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>     <xsl:template match="name"> using System; using System.Xml;     class Name {         static void Main(  ) {         XmlTextWriter w = new XmlTextWriter(Console.Out);          w.Formatting = Formatting.Indented;          w.Indentation = 1;          w.WriteStartDocument(  );          w.WriteStartElement("<xsl:value-of select="name(  )"/>");          w.WriteAttributeString("title", "Mr.");           w.WriteElementString("family", "<xsl:value-of select="last"/>");           w.WriteElementString("given", "<xsl:value-of select="first"/>");          w.WriteEndElement(  );         w.Close(  );         }     } </xsl:template>     </xsl:stylesheet>

This stylesheet uses value-of instruction elements to grab string values from the source tree. The first occurrence of value-of uses the XPath function name( ) to grab the name of the element that the template matches. The template actually matches not just the name of an element node, but a node-set, that is, the set of nodes including the element name and its children. The value-of element, however, returns only the string value of the first node of this node-set. The next two occurrences of value-of capture the text node children of the last and first elements in the source tree, respectively. (You'll learn more about nodes and node-sets in Chapter 4.)

When name.xml is processed with this stylesheet, it outputs a C# program. C# is part of the .NET Framework and offers many conveniences for a programmer that must handle XML. You can download .NET for Windows from Microsoft at http://www.microsoft.com/net/. You can also download Ximian's open source implementation of .NET at http://www.go-mono.com/, which runs on Linux and Windows, as well as FreeBSD and Mac OS X.

To transform name.xml with csharp.xsl, use this command to save the program to a file:

xalan -o name.cs name.xml csharp.xsl

After the transformation, the program is saved to the file name.cs:

using System; using System.Xml;     class Name {         static void Main(  ) {         XmlTextWriter w = new XmlTextWriter(Console.Out);          w.Formatting = Formatting.Indented;          w.Indentation = 1;          w.WriteStartDocument(  );          w.WriteStartElement("name");          w.WriteAttributeString("title", "Mr.");           w.WriteElementString("family", "Churchill");           w.WriteElementString("given", "Winston");          w.WriteEndElement(  );         w.Close(  );         }     }

The XmlTextWriter object allows C# programs to write well-formed XML to the console, file, or stream. The output of this particular program is written to the console (standard output), and the output will be indented. This is set by the Formatting and Indentation properties. The document element name is created by the WriteStartElement( ) method of XmlTextWriter, and it has a single attribute, title, created with WriteAttributeString( ). This element also has two children, family and given, produced by a pair of WriteElementString( ) methods.

You can compile and run this program if you have the .NET Framework downloaded and installed. Compile the program with the Microsoft C# compiler by typing the following at a command prompt:

csc name.cs

Or with the Mono compiler using:

mcs name.cs

It should report no errors all you should see are some copyright messages. The output of the compilation is an executable file called name.exe. If you have the Windows implementation, type:

name

If you have the Mono implementation, type:

mono name.exe

Again, this example works only if you have .NET installed. When you successfully run this program on Windows, for example, it produces the following well-formed XML output:

<?xml version="1.0" encoding="IBM437"?> <name title="Mr.">  <family>Churchill</family>  <given>Winston</given> </name>

IBM437 is an IANA-registered character set name for the Windows code page 437. XML processors are not required to support this character set, but they are permitted to support any character sets registered at IANA (which IBM437 is), plus any private character sets (they must be prefixed with x-).



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

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