(Optional) XSLT with Class XslCompiledTransform

Recall from Section 19.7 that XSLT provides elements that define rules for converting one type of XML document to another type of XML document. We showed how to transform XML documents into XHTML documents and displayed these in Internet Explorer. The XSLT processor included in Internet Explorer (i.e., MSXML) performed the transformations.

Performing an XSL Transformation in C# Using the .NET Framework

Figure 19.30 applies the style sheet sports.xsl (Fig. 19.17) to sports.xml (Fig. 19.26) programmatically. The result of the transformation is written to an XHTML file on disk and displayed in a text box. Figure 19.30(c) shows the resulting XHTML document (sports.html) when it is viewed in Internet Explorer.

Figure 19.30. XSLT style sheet applied to an XML document.

1 // Fig. 19.30: TransformTest.cs 2 // Applying an XSLT style sheet to an XML document. 3 using System; 4 using System.Windows.Forms; 5 using System.Xml.Xsl; // contains class XslCompiledTransform 6 7 namespace TransformTest 8 { 9 public partial class TransformTestForm : Form 10 { 11 public TransformTestForm() 12 { 13 InitializeComponent(); 14 } // end constructor 15 16 // applies the transformation 17 private XslCompiledTransform transformer; 18 19 // initialize variables 20 private void TransformTestForm_Load( object sender, EventArgs e ) 21 { 22 transformer = new XslCompiledTransform(); // create transformer 23 24 // load and compile the style sheet 25 transformer.Load( "sports.xsl" ); 26 } // end method TransformTestForm_Load 27 28 // transform XML data on transformButton_Click event 29 private void transformButton_Click( object sender, EventArgs e ) 30 { 31 // perform the transformation and store the result in new file 32 transformer.Transform( "sports.xml", "sports.html" ); 33 34 // read and display the XHTML document's text in a Textbox 35 consoleTextBox.Text = 36 System.IO.File.ReadAllText( "sports.html" ); 37 } // end method transformButton_Click 38 } // end class TransformTestForm 39 } // end namespace TransformTest  

(a)

(b)

(c)

Line 5 is a using declaration for the System.Xml.Xsl namespace, which contains class XslCompiledTransform for applying XSLT style sheets to XML documents. Line 17 declares XslCompiledTransform reference transformer. An object of this type serves as an XSLT processor (like MSXML in earlier examples) to transform XML data from one format to another.

In event handler transformTestForm_Load (lines 2026), line 22 creates a new XslCompiledTransform object. Then line 25 calls the XslCompiledTransform object's Load method, which parses and loads the style sheet that this application uses. This method takes an argument specifying the name and location of the style sheetsports.xsl (Fig. 19.17) located in the current directory.

Event handler TRansformButton_Click (lines 2937) calls method transform of class XslCompiledTransform to apply the style sheet (sports.xsl) to sports.xml (line 32). This method takes two string argumentsthe first specifies the XML file to which the style sheet should be applied, and the second specifies the file in which the result of the transformation should be stored on disk. Thus the transform method call in line 32 transforms sports.xml to XHTML and writes the result to disk as the file sports.html. Figure 19.30(c) shows the new XHTML document rendered in Internet Explorer. Note that the output is identical to that of Fig. 19.16in the current example, though, the XHTML is stored on disk rather than generated dynamically by MSXML.

After applying the transformation, the program displays the content of the newly created sports.html file in the txtConsole TextBox, as shown in Fig. 19.30(b). Lines 3536 obtain the text of the file by passing its name to method ReadAllText of the System.IO.File class provided by the FCL to simplify file-processing tasks on the local system.

Категории