Tải bản đầy đủ - 0 (trang)
6-12. Perform an XSL Transform

6-12. Perform an XSL Transform

Tải bản đầy đủ - 0trang

CHAPTER 6 ■ XML PROCESSING



















Essentially, every XSLT stylesheet consists of a set of templates. Each template matches some set of

elements in the source document and then describes the contribution that the matched element will

make to the resulting document. To match the template, the XSLT document uses XPath expressions, as

described in recipe 6-6.

The orders.xslt stylesheet contains two template elements (as children of the root stylesheet

element). The first template matches the root Order element. When the XSLT processor finds an Order

element, it outputs the tags necessary to start an HTML table with appropriate column headings and

inserts some data about the client using the value-of command, which outputs the text result of an

XPath expression. In this case, the XPath expressions (Client/@id and Client/Name) match the id attribute

and the Name element.

Next, the apply-templates command branches off and performs processing of any contained Item

elements. This is required because there might be multiple Item elements. Each Item element is matched

using the XPath expression Items/Item. The root Order node is not specified because Order is the current

node. Finally, the initial template writes the tags necessary to end the HTML document.

If you execute this transform on the sample orders.xml file shown in recipe 6-6, you will end up with

the following HTML document:







Order ROS-930252034

for Remarkable Office Supplies































IDNamePrice
1001Electronic Protractor42.99
1002Invisible Ink200.25








293



www.it-ebooks.info



CHAPTER 6 ■ XML PROCESSING



To apply an XSLT stylesheet in .NET, you use the XslCompiledTransform class. The following code

shows a Windows-based application that programmatically applies the transformation and then

displays the transformed file in a window using the WebBrowser control:

using

using

using

using

using

using

using

using

using



System;

System.Collections.Generic;

System.ComponentModel;

System.Data;

System.Drawing;

System.Text;

System.Windows.Forms;

System.Xml.Xsl;

System.Xml;



namespace Apress.VisualCSharpRecipes.Chapter06

{

public partial class TransformXml : Form

{

public TransformXml()

{

InitializeComponent();

}

private void TransformXml_Load(object sender, EventArgs e)

{

XslCompiledTransform transform = new XslCompiledTransform();

// Load the XSTL stylesheet.

transform.Load(@"..\..\orders.xslt");

// Transform orders.xml into orders.html using orders.xslt.

transform.Transform(@"..\..\orders.xml", @"..\..\orders.html");

webBrowser1.Navigate(Application.StartupPath + @"\..\..\orders.html");

}

}

}

Figure 6-2 shows the application.



294



www.it-ebooks.info



CHAPTER 6 ■ XML PROCESSING



Figure 6-2. The stylesheet output for orders.xml

In this example, the code uses the overloaded version of the Transform method that saves the result

document directly to disk, although you could receive it as a stream and process it inside your

application instead. The following code shows an alternate approach that keeps the document content

in memory at all times (with no external results file). The XslCompiledTransform writes the results to an

XmlWriter that wraps a StringBuilder. The content is then copied from the StringBuilder into the

WebBrowser through the handy WebBrowser.DocumentText property. The results are identical.

StringBuilder htmlContent = new StringBuilder();

XmlWriter results = XmlWriter.Create(htmlContent);

transform.Transform(@"..\..\orders.xml", results);

webBrowser1.DocumentText = htmlContent.ToString();



295



www.it-ebooks.info



CHAPTER 6 ■ XML PROCESSING



6-13. Load XML with LINQ

Problem

You need to load an XML tree in order to perform LINQ to XML operations.



Solution

Use the static methods of the System.Xml.Linq.XElement class to handle existing XML.



How It Works

The System.Xml.Linq.XElement class represents an XML element. LINQ represents XML as a tree of

XElements, such that one class is used for every element from the root nodes to the terminating child

nodes. The static XElement.Load method will load and parse an XML document from a number of

sources, returning an instance of XElement representing the root node. The Load method is overloaded to

support a range of data sources, as shown in Table 6-2.

Table 6-2. Overloads of the XElement.Load Method



Method



Description



Load(Stream)



Loads the XML data from a stream



Load(String)



Loads the XML from a file, the name of which is obtained from the string



Load(TextReader)



Loads the XML from a System.IO.TextReader



Load(XMLReader)



Loads the XML from a System.Xml.XmlReader (see recipe 6-7)



You can control some of the load options by using the System.Xml.Linq.LoadOptions enumeration

as an additional argument to the Load method—see the .NET Framework documentation for details.



The Code

The following example demonstrates using the four different data sources accepted by the

XElement.Load method:

using

using

using

using



System;

System.IO;

System.Xml;

System.Xml.Linq;



296



www.it-ebooks.info



CHAPTER 6 ■ XML PROCESSING



namespace Recipe06_13

{

class Recipe06_13

{

static void Main(string[] args)

{

// Define the path to the sample file.

string filename = @"..\..\ProductCatalog.xml";

// Load the XML using the file name.

Console.WriteLine("Loading using file name");

XElement root = XElement.Load(filename);

// Write out the XML.

Console.WriteLine(root);

Console.WriteLine("Press enter to continue");

Console.ReadLine();

// Load via a stream to the file.

Console.WriteLine("Loading using a stream");

FileStream filestream = File.OpenRead(filename);

root = XElement.Load(filestream);

// Write out the XML.

Console.WriteLine(root);

Console.WriteLine("Press enter to continue");

Console.ReadLine();

// Load via a textreader.

Console.WriteLine("Loading using a TextReader");

TextReader reader = new StreamReader(filename);

root = XElement.Load(reader);

// Write out the XML.

Console.WriteLine(root);

Console.WriteLine("Press enter to continue");

Console.ReadLine();

// Load via an xmlreader.

Console.WriteLine("Loading using an XmlReader");

XmlReader xmlreader = new XmlTextReader(new StreamReader(filename));

root = XElement.Load(xmlreader);

// Write out the XML.

Console.WriteLine(root);

Console.WriteLine("Press enter to continue");

Console.ReadLine();

}

}

}



297



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

6-12. Perform an XSL Transform

Tải bản đầy đủ ngay(0 tr)

×