0672320398
- Chapter 7: Transforming XML with XSLT and ASP.NET
- The Transformation Process
- Getting Your Feet Wet with XSLT
- The XSLT Language
- XSLT Functions
- .NET Classes Involved in Transforming XML
- Creating a Reusable XSLT Class
- Summary
Chapter 7: Transforming XML with XSLT and ASP.NET
In This Chapter
- What Is XSLT
- The Transformation Process
- Getting Your Feet Wet with XSLT
- The XSLT Language
- XSLT Functions
- .NET Classes Involved in Transforming XML
- Creating a Reusable XSLT Class
What Is XSLT?
During the development of the XML specification, the W3C working group realized that for XML to reach its full potential, a method of transforming XML documents into different formats needed to exist. At some time or another, an application that has the capability to work with XML documents will need to display or structure the data in a different format than specified in the document. If the only method for accomplishing this task necessitates programmatically transforming the XML document into the appropriate format by using an XML parser paired with a programming language, the power of having a cross-platform and language-independent XML language would be lost. Some method of transforming XML documents into different formats such as HTML, flat files, Wireless Markup Language (WML), and even other forms of XML needed to be devised so that it could be used on any platform and with any language.
To accommodate this transformation process, Extensible Stylesheet Language Transformations (XSLT) was created. Version 1.0 of the XSLT specification reached recommended status at the W3C in November of 1999 (http://www.w3.org/TR/1999/REC-xslt-19991116) and many XML parsers now provide full XSLT support. The .NET framework provides 100% compliance with the XSLT version 1.0 specification.
What exactly is XSLT useful for and why would you, as an ASP.NET developer, want to learn about it? The answer boils down to the capability of XSLT to transform XML documents into different formats that can be consumed by a variety of devices, including browsers, Personal Digital Assistants (PDAs), Web-enabled phones, and other devices that will appear in the near future.
Transformations can also be useful in situations where an XML document's structure does not match up well with an application that will accept the data within the document. An XML document may contain the appropriate data to be imported into a database, for example, but may not be structured in a way that the application performing the import expects. For example, the application may be better prepared to handle element-based XML documents rather than ones with a lot of attributes, as shown in the following document:
<?xml version="1.0"?> <root> <row id="1" fname="Dan" lname="Wahlin"/> <row id="2" fname="Heedy" lname="Wahlin"/> <row id="3" fname="Danny" lname="Wahlin"/> <row id="4" fname="Jeffery" lname="Wahlin"/> </root>
Using XSLT, this document can be transformed into a structure that the application is better suited to work with:
<?xml version="1.0"?> <root> <row> <id>1</id> <fname>Dan</fname> <lname>Wahlin</lname> </row> <row> <id>2</id> <fname>Heedy</fname> <lname>Wahlin</lname> </row> <row> <id>3</id> <fname>Danny</fname> <lname>Wahlin</lname> </row> <row> <id>4</id> <fname>Jeffery</fname> <lname>Wahlin</lname> </row> </root>
This chapter teaches you how to perform this type of transformationas well as many othersby covering the following topics:
The transformation process
The XSLT language
.NET classes involved in transforming XML