YOU ARE HERE: Home > Tech > .Net > Article

XSLT Transformation using .Net
By John Henry Xu This article was rated:
 
Printer Version Printer Friendly | Add As Favorite | Link to Article

About the Author

Dr. John Xu (also known as Jack Xu) is a seasoned system architect. He was chief architect for large distributed portals. He also developed search engine and Java forums. He can be reached by email xixu@yahoo.com.

Microsoft .Net has provided many classes and methods for XML. One important feature in XML classes is .net XSLT transforming between XML documents.

In this article, we use .net to transform one input XML file into another XML file, using XSLT. We will use one example to demonstrate how easy to use C#.net performing this task.

We write an input XML file, an XSL file and a .net class in this example.

1. C#.net Code

We have written a C# program to process XSL transformation. List 1.1 is the code that provides functions for transformation.

List 1.1 TransformFile.cs


using System;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;

class TransformFiles
{
static void Main (string[] args)
{
if (args.Length < 3) {
Console.WriteLine ("Syntax: TrasformFile xmldoc xsldoc xmloutput");
return;
}

try {
XPathDocument doc = new XPathDocument (args[0]);
XslTransform xsl = new XslTransform ();
xsl.Load (args[1]);
StreamWriter writer = new StreamWriter(args[2],false);

xsl.Transform (doc, null, writer, null);

}
catch (Exception ex) {
Console.WriteLine (ex.Message);
}
}
}

To compile above class, we type a command line in a console window.

csc TransformFile.cs

And we obtained executable file TransformFile.exe

Explanation:

In class TransformFile.cs, we use main method that accept command line arguments for input file, xsl file and out put file.

XpathDocument class in .net is a class that provides a fast, read-only cache for XML document processing using XSLT.

XslTransform class transforms XML data using an XSLT stylesheet.

We use

TrasformFile xmldoc xsldoc xmloutput

And obtained the output file as the file name specified in xmloutput.


2. Input XML and XSL files

Assume we have input file myinput.xml and xsl file as transform.xsl

List 2.1 myinput.xml


<?xml version="1.0" encoding="UTF-8"?>
<company>
<division>
<name>Finance</name>
<function>Check and cash process</function>
<manager>Rick Reumann</manager>
<email>struts@reumann.net</email>
<country>Germany</country>
</division>
<division>
<name>IT Support</name>
<function>Support day to day operation</function>
<manager>Bill Simpson</manager>
<email>bill@usanalyst.com</email>
<country>Canada</country>
</division>
<division>
<name>Sales</name>
<function>Marketing and sales</function>
<manager>John Henry Xu</manager>
<email>xixu@yahoo.com</email>
<country>USA</country>
</division>
</company>

List 2.2 transform.xsl


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" >

<xsl:output method="xml" />
<xsl:template match="/">
<xsl:element name="list">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="company/division/name">
<xsl:element name="divisionName">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="company/division/manager">
<xsl:element name="managerName">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="company/division/country">
<xsl:element name="Country">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="company/division/function|company/division/email" />

</xsl:stylesheet>


3. Transformed XML

Let's run command line in a console application:

TrasformFile myinput.xml transform.xsl output.xml

List 3.1 output.xml


<?xml version="1.0" encoding="utf-8"?>
<list>
<divisionName>Finance</divisionName>
<managerName>Rick Reumann</managerName>
<Country>Germany</Country>
<divisionName>IT Support</divisionName>
<managerName>Bill Simpson</managerName>
<Country>Canada</Country>
<divisionName>Sales</divisionName>
<managerName>John Henry Xu</managerName>
<Country>USA</Country>
</list>

4. Conclusion

In this article, we demonstrated how to use xslt in .net framework. And you can extend the methods in this article for any XML transformation tasks in .NET.


Was this article helpful to you?yesno
1 of 1 people found this article helpful.



Related Publications
 
Implementing AJAX Using ASP.NET
XSLT Transformation using .Net

(Registered users can post questions/comments)

 
 TLINKS SEARCH
Advanced Search
Help
 Recommended Links
Red Cross
Responding to hurricane katrina relieve. Donate today. It's a Great Feeling to Help.
http://www.redcross.org
Getusjobs.com
Getusjobs.com is the job site focused on American jobs. See the results that put us on top.
http://www.getusjobs.com
Database Tool
TLinkSoft® tools empowers developers, integrators and DBAs to be more productive.
http://www.cppunit.org/download.jsp
USAnalyst.com
USAnalyst.com provide a community for database analysts, business analysts, developer analysts and managers.
http://www.cppunit.org/article

Powered by Tlinks Systems