Transformations
For the development community, one of the most useful
features when working with XML is the ability to transform XML content to
another format more appropriate for a given situation. To harness this power in
ASP.NET, we first of all need to understand when and why we should use
transformations in a web application. Once we've made that decision, we must
understand the syntax necessary to create our transformation and how to execute
the transformation in the managed environment of .NET.
By the end of this chapter, we will have covered the
following subjects:
§
What is XSLT?
§
Structure of an XSLT document
§
Applying XSLT Style Sheets to XML documents
§
Controlling document output
§
Using transformations for the presentation layer
§
Using transformations for B2B
We will learn how to harness the XPath skills acquired from
the previous chapter, and examine how to use transformations effectively in
ASP.NET.
What is
XSLT?
Extensible Stylesheet Language Transformations (XSLT) is a
declarative programming language, with its origins in the early Extensible
Stylesheet Language (XSL). XSLT v1.0 was endorsed by the Director of W3C as a
Recommendation in November 1999, and more information can be found at http://www.w3.org/TR/xslt. We will be
using version 1.0 in this chapter, as supported by the .NET Framework, although
there are other versions in working draft at the time of writing.
Transforming
XML Documents
XSLT is the language which instructs an XSLT processor how
to convert information in an XML source document to a format of our preference
– be it an XML document (including WML for example), an HTML document, or just
plain text. Note that different XSLT engines will adhere to the standard to
differing levels, but in this chapter, we will naturally concentrate on the
behavior of .NET.
From
XML to XML
There are many situations where there is a need to transform
an XML document to one in a completely different XML dialect. For example,
consider the following document extract:
<Item>
<ID>ITM-1001</ID>
<LineOfProduct>1</LineOfProduct>
<ListPrice>123.45</ListPrice>
<QTY>1</QTY>
<Description>Gadget
XYZ</Description>
</Item>
We may prefer to have this information in a different form,
perhaps for a component we have already developed which handles data in this
form:
<item
id="ITM-1001" productLine="1">
<quantity>1</quantity>
<list-price>123.45</list-price>
<description>Gadget
XYZ</description>
</item>
Notice how the first extract is element-centric; it is
devoid of any attributes. Also the first uses a different nomenclature for node
names, like <QTY> vs. <quantity>,
and we can't forget that XML documents are case-sensitive – thus <Description> is different from <description>.
So how can we transform one to the other? We could load the
first XML document into an XmlDocument object, traverse each node and
programmatically generate a second XmlDocument object. This would work, but
what if we needed to make changes to the transformation? It could be quite a
challenge to locate and change the code to create the new transformation. Also,
the programmatic route requires recompiling the code after any such changes.
The preferred method would be to use XSLT style sheets.
After all, the language is designed specifically for this purpose. Secondly, it
is fairly easy to locate the template rules that perform certain aspects of a
transformation (discussed later) and add, update, or delete parts to create new
transformations. Finally, it is not necessary to recompile and redeploy the
code which references an XSLT style sheet that has been changed.
From
XML to HTML
In ASP.NET applications, it is quite common to encounter a
need to present data provided as XML to the user in HTML. A typical example of
this would be a symmetrical XML document that quite easily lends itself to a
table format. For instance, we may be interested in taking the following XML
structure:
<items>
<item
id="ITM-1001" productLine="1">
<quantity>1</quantity>
<list-price>123.45</list-price>
<description>Gadget
XYZ</description>
</item>
<item
id="ITM-1002" productLine="1">
<quantity>3</quantity>
<list-price>4.00</list-price>
<description>XYZ
Accessory</description>
</item>
<item
id="ITM-1003" productLine="2">
<quantity>1</quantity>
<list-price>15.00</list-price>
<description>Gizmo
Part</description>
</item>
<item
id="ITM-1004" productLine="3">
<quantity>1</quantity>
<list-price>44.00</list-price>
<description>Widget
X</description>
</item>
</items>
and presenting it to the user like this:

The markup to create the table above would be enclosed
within an HTML <table> tag. By using
XSLT, we can take any XML structure and convert it to HTML. We will see how to
do this a little later.
From
XML to Plain Text
From time to time, we may need to generate plain text.
Typically, this is done to support legacy applications that consume text
documents with either fixed length or comma delimited columns. Thus the
<items> element and all of its children from the previous XML file could
be transformed to a comma delimited text file like this:
ITM-1001, 1, 1, 123.45, Gadget ZYZ
ITM-1002, 1, 3, 4.00,
XYZ Accessory
ITM-1003, 2, 1, 15.00, Gizmo Part
ITM-1004, 3, 1, 44.00, Widget X
Page:
1 |
2 |
3 |
4 |
5 |
6
Next Page >>>