WebReference.com - Chapter 7 of XML for ASP.NET Developers, from Sams Publishing (2/13) | WebReference

WebReference.com - Chapter 7 of XML for ASP.NET Developers, from Sams Publishing (2/13)

To page 1current pageTo page 3To page 4To page 5To page 6To page 7To page 8To page 9To page 10To page 11To page 12To page 13
[previous] [next]

XML for ASP.NET Developers

The Transformation Process

The process of transforming an XML document into another format, such as HTML or WML, relies on two types of processing engines. First, a parser capable of loading an XML document must be present to load the source document into a DOM tree structure (for more information about the DOM, refer to Chapter 6, "Programming the Document Object Model (DOM) with ASP.NET." Next, the XSLT document must be loaded and a tree structure will be created for it, as well. This tree structure will normally be optimized to accommodate XSLT processing and is specific to the processor being used. An XSLT processor is then needed to take the XML document structure, match up nodes within the document against "templates" found in the XSLT document, and then output the resulting document. The third tree structure (the resulting document) is dynamically created based on information contained in the XSLT document. A simple diagram of this transformation process is shown in Figure 7.1.

Figure 7.1: The XSLT Transformation Process
Figure 7.1: The XSLT transformation process

XSLT Templates

Before looking more closely at how to build XSLT documents, it's important that you understand what the building blocks of these documents are. Although XSLT stands for Extensible Stylesheet Language Transformations, an alternative name for it could potentially be Extensible Template Language Transformations. Why? The answer is because of its reliance on templates to process and create a particular output structure. The W3C provides the following statement about templates:

A stylesheet contains a set of template rules. A template rule has two parts: a pattern which is matched against nodes in the source tree and a template which can be instantiated to form part of the result tree. This allows a stylesheet to be applicable to a wide class of documents that have similar source tree structures.

If you have ever used templates in Excel, Word, or PowerPoint, you know that they provide a basic structure that can be reused for specific purposes. For example, every time you submit an expense report, you may be accustomed to filling out a template in Word that is designed for this purpose. The template likely has specific form fields built in so that every expense report being submitted looks the same. There may be other templates that are used for purchase orders. The point is that the templates are geared to match up with a specific task, such as creating an expense report, a purchase order, or some other activity.

Templates in XSLT function in much the same way, except that they are geared to match up with nodes in an XML document. XSLT templates provide a way to process and structure data contained within elements and attributes in the source XML document. Their basic purpose is to provide a template structure that can be processed when a particular node in the source XML document is discovered.

So how do templates work? The XSLT processor described earlier is provided with two tree structures to walk through. The first is the structure for the source XML document and the second is the XSLT document itself. After these two structures are provided, the XSLT processor attempts to match element or attribute names found in the XML document with templates contained in the XSLT tree structure. This matching process uses XPath expressions that are embedded within the XSLT document. When a node found within the XML document matches a template in the XSLT document, that template is processed.

Processing of templates found within an XSLT document normally starts with a template that matches the root node of the XML document and proceeds down to its children. When a template is processed, the output is added to the third tree structure mentioned earlier that is used in building the output document.

Templates offer an efficient way to process a variety of XML document structures and are very efficient in cases where an XML document contains repetitive items. Each time an element, attribute, text node, and so on is found, it is matched up with the appropriate template via XPath expressions. If a given node does not have a matching template, no processing will occur on it, and the next section of the XML document is processed. In cases where a matching node is found, the template takes care of generating the proper output structure based on data/nodes contained within the node.

So that you can see templates in action, the next section introduces you to a simple XSLT document. The sections that follow describe in greater detail how to use templates and other parts of the XSLT language.


To page 1current pageTo page 3To page 4To page 5To page 6To page 7To page 8To page 9To page 10To page 11To page 12To page 13
[previous] [next]

© Copyright Pearson Education and Created: April 22, 2002
Revised: April 22, 2002

URL: https://webreference.com/authoring/languages/xml/aspnet/chap7/2.html