XSLT Support in the .NET Framework
by Jayram Tallamraju
Overview
XSLT is an excellent technology
that adds more power to XML. XSLT is a language by itself like most other
programming languages. It is the language to transform source data structure to
destination data structure and may not be suitable to implement business
logic. This means that existing programming languages go hand in hand with XSLT and
XSLT adds more power to them. It is possible to get away from XSLT
using XML DOM to some extent, but this solution is usually not as elegant in most
data/document conversions when compared to using XSLT.
This article only focuses on .net
support for XSLT and using XSLT features from .net Framework. I would like to
encourage everyone to consider XSLT as an option when it comes to data
conversion/transformation. If any application uses XML heavily, there is high
possibility that XSLT might solve few key issues in more elegant/efficient way.
This article does not cover
complete details of XSLT and there is enough material available on the Internet
for this purpose. Please see the reference section for details of books and
useful URLs.
XSLT support in .net:
System.Xml.Xsl is the namespace for XSLT supported classes in .net Framework. XslTranform
class is .net implementation of XSLT version
1.0 recommendation. XslTransform is the XSLT
processor/engine in .net. I believe XslTransform is
complete .net framework implementation and does not use MSXML.
At least I did not see any reference of MSXML, in XslTransform
or .net framework documentation.
XSLT and XSL
XSL started with a goal of
providing presentation and formatting support to XML.
It has evolved into transformation and
formatting objects. They are grouped under XSLT (XSL Transformations) and
XSL-FO (XSL Formatting objects). Majority of applications that use web pages
for presentation can get away with XSLT alone. There so much that can be done
just with XSLT. This article does not cover XSL-FO.
XSLT is not just for presentation:
XSLT is not just another style
sheet language (like CSS) and used for presentation of XML over web only. There
are many uses of XSLT in real world. Obviously showing XML document as HTML
pages on web is one of the uses of XSLT. Most content management applications
use this approach, to separate presentation and data.
There are numerous occasions
where XSLT can come to help. Mainly applications that merge multiple XML
documents to single document (OR) convert different input data formats to a
uniform data format that a specific application can process.
In today's world where XML is the
data standard and with many applications producing XML data in different
format, there is always a requirement to transform these different formats.
This is where XSLT is handy.
Samples of XSLT in .net:
Following are 3 simple examples show using XSLT in .net and
ASP.NET. You can look into the reference section for more details. There is no
sample code provided separately for download, as the samples are too small and
code is already presented with in each sample.
1. Simple XSLT:
1.a. Sample XML file: Let us
consider a simple XML file with employee names. We would like to list all
employee names from this XML file on the web and would like to do this using
XSLT.
Following is sample XML file:
<PrakashFinancial>
<Employee ID="1" Name="Sid" />
<Employee ID="2" Name="Bob" />
<Employee ID="3" Name="Rao" />
<Employee ID="4" Name="Jack" />
<Employee ID="5" Name="David" />
</PrakashFinancial>
1.b. Sample XSLT file: Following sample XSL
file will list all employee names from above XML file and put a title "List of employees"
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="utf-8" />
<xsl:template match="/PrakashFinancial">
<xsl:text>
List of Employees :
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/PrakashFinancial/Employee">
<xsl:value-of select="@Name"/>
</xsl:template>
</xsl:stylesheet>
1.c. Apply transformation:
// Simplified C# code - Showing only code that is important
using System.Xml.Xsl;
XslTransform oTf = new XslTransform();
oTf.Load("specify XSL file");
oTf.Transform("Input XML file","Output File");
1.d. Output file generated:
Following HTML output will be generated in this case:
List of Employees : Sid Bob Rao Jack David
NOTE: Obviously above sample is similar to "Hello world" sample. It only gives quick
view of what is involved using XSLT in .net.
2. Calling .net Code from XSL documents:
Using <msxsl:script> tag in XSLT, it is possible to write code
using C#, VB.NET or any other .net Framework languages. XslTransform
class will convert this scripting language to intermediate language.
Alternately it is possible to simply call
existing business components from the script block instead of keeping business
logic in side XSL document.
For example, if we want to
display today's date in above output of Sample 1. Following XSL file shows what
changes we need to call script functions defined in XSL. Changes are in bold
for readability.
At high-level, we need to use "urn:schemas-microsoft-com:xslt"
namespace where "msxsl:script" is defined.
All user defined scripts are under name "Testing" prefix.
Please see reference section for more details on using <msxsl:script>.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:Testing="urn:my-scripts">
<msxsl:script language="C#" implements-prefix="Testing">
<![CDATA[
public string getDate(){
return (DateTime.Today.ToShortDateString());
}
]]>
</msxsl:script>
<xsl:output method="html" encoding="utf-8" />
<xsl:template match="/PrakashFinancial">
<xsl:text>
List of Employees :
</xsl:text>
<xsl:value-of select="user:getDate()"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/PrakashFinancial/Employee">
<xsl:value-of select="@Name"/>
</xsl:template>
</xsl:stylesheet>
Note: XSLT supports many built-in functions and
first step is to see if any task can be handled through existing functions in
XSLT. Writing script is recommended to do more complex things with XSLT to
extend functionality or to re-use some business logic in existing components.
3. ASP.NET support for XSLT
If you have XML document and XSL
document and would like to process XSLT on server-side and return final result,
ASP.NET has a web server control for you. <asp:xml> does exactly this.
<asp:xml> web server control can take XML document and
XSL document, apply transformations and show the result on the browser.
It is very easy to use XML control in ASP.NET. Try the following:
3.a: Create sample ASP.NET web application
3.b: Copy above XML and XSL file
to "SampleData.XML" and "SampleTransform.XSL"
and copy them to above web application root directory.
3.c: On WebForm1.aspx drag and
drop "Xml" control you see in "WebForm controls" of
your toolbox.
3.d: On Page load add the following code: (Webform1.aspx.cs file)
Xml1.DocumentSource = "SampleData.XML";
Xml1.TransformSource = "SampleTransform.XSL";
3.e: Run the sample web page and
you will see that transformation is performed on server-side by ASP.NET control
and output is rendered on web page.
Testing and timing XSLT:
1. MSXSL.EXE:
Microsoft provides free download of simple XSLT tool (MSXSL.exe). You can get
this for free from Microsoft's website.
This tool also provides time to do the transformation with -t option.
Example: msxsl.exe SampleXML.xml SampleXSL.xsl -o Output1.html -t
The above command transforms SampleXML.xml
(by applying XSL from SampleXSL.xsl) and sends the
output Output1.html. The above command also gives the timings (-t option).
Note: MSXSL.EXE is only light wrapper using MSXML
components. So you have to install latest MSXML from Microsoft site before
using this tool.
2. Internet Explorer (>= 5.x): You can use IE (Version
> 5.x) to reference XSLT inside any XML document and let IE do the
transformation. This is good for testing your XSL files.
Using XSLT without .net?
It is not the intention to
address non-.net related XSLT details in this article. But for the sake of
completion I will cover few options.
If all the users are using IE
(version >= 5.x), then IE has built in support to apply XSL on XML and show
the result. Instead of server-side transformation you could use this, but it
may force all users to use correct IE version. There are some ISAPI filters
available on IIS that can do server-side transformation if browser does not
support it etc.
References:
-
Books on XSLT: I liked Wrox Book "XSLT Programmer's Reference". I think it is a good book on XSLT.
Here's a link to more information about the book: XSLT : Programmer's Reference.
-
W3C XSLT 1.0 recommendation: http://www.w3.org/TR/xslt
-
Good article on using XSLT for merging documents: http://www.fawcette.com/archives/premier/mgznarch/xml/2001/06jun01/rj0103/rj0103.asp
(Samples in this article use few ideas from above article)
-
MSXML 4.0: Microsoft MSXML (Implementation of W3C XSLT 1.0 recommendation)
-
MSXSL.exe: Command Line Transformation Utility (msxsl.exe) - Command line tool to test XSLT
-
.net Framework example using XSLT (for both VB.net/C#)
You can find many useful samples on Microsoft website using
XslTransformation or scripting etc. The following is the link
that uses <msxsl:script> example:
XSLT Stylesheet Scripting using <msxsl:script>
|