perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
You are here: home Free Library Wrox Press » Professional ASP.NET 1.0 XML with C# Saturday, 17 November 2007

Page: 1   |   2   |   3   |   4   |   5   |   6        

Professional ASP.NET 1.0 XML with C#Professional ASP.NET 1.0 XML with C#

This book concentrates on describing how XML can be effectively used within ASP.NET applications. Coverage includes discussion of where and when to use XML, detailed discussion of the System.XML namespace, ADO.NET as it relates to ASP.NET, SQL Server 2000 and SQLXML managed classes, and XSLT. Furthermore the book specifically spends time highlighting new developments in XML related standards and technologies, and performance issues that the advanced ASP.NET developer should be aware of.

This book is aimed at the experienced web developer who already has a grasp of ASP.NET and a basic familiarity with XML and related technologies. The book is written in C# and aims to augment the skill set of those seeking to progress their .NET experience.

Read Chapter 1: Introduction to XML Technologies from this book

Read Chapter 5: Transformations from this book

Print this article

Buy this book!

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 >>>        

  Contact Us | E-mail Us | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy