Extending XSLT using C#
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
sql server forums
Chapter:   XML
Current Lesson:
Extending XSLT using C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  XML transformation using Xslt in C# [next Lesson]  Using Stack class in C#
Extending XSLT using C#
  by: maheshcr

Extending XSLT using C#

by: maheshcr

XSLT is a transformation language for XML. It allows server systems to transform the source XML tree into a more suitable form for clients. XSLT uses node patterns to match against templates to perform its transformations. Though it makes complex transformations relatively simple there are some situations where we might have to use some custom classes.

Some of the situations where we might need to extend XSLT are:

1) Call custom business logic
2) Perform different actions depending on Permissions
3) Perform complex formatting for dates, strings etc
4) Or even call a webservice!!


Steps to extend XSLT

1) Create the custom object to use from within XSLT(in C#)
CustomDate custDate = new CustomDate() ;
2) Provide a custom namespace declaration for the custom class within XSLTs namespace declaration(in XSLT file)
<xsl:transform
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:myCustDate="urn:custDate">
3) Pass an instance of the custom object to XSLT, with the same namespace as in last step(in C#)
xslArgs.AddExtensionObject("urn:custDate", custDate) ;
4) Use the object from within XSLT(in XSLT file)
<xsl:value-of select="myCustDate:GetDateDiff(./joiningdate)"/>
Sample code

For our example let us assume we have a XSLT sheet where we need to manipulate dates. We need to show the number of days the employee has been with the company. Since XSLT has no native date manipulation functions, let us use an extension object for our task.
using System ;
using System.IO ;
using System.Xml ;
using System.Xml.Xsl ;
using System.Xml.XPath ;

public class XsltExtension{

    public static void Main(string[] args){
        
        if (args.Length == 2){
            
            Transform(args[0], args[1]) ;
            
        }else{
            
            PrintUsage() ;
            
        }
    }
    
    public static void Transform(string sXmlPath, string sXslPath){
        
        try{
            
            //load the Xml doc
            XPathDocument myXPathDoc = new XPathDocument(sXmlPath) ;

            XslTransform myXslTrans = new XslTransform() ;
            
            //load the Xsl 
            myXslTrans.Load(sXslPath) ;
            
            XsltArgumentList xslArgs = new XsltArgumentList() ;
            
            //create custom object
            CustomDate custDate = new CustomDate() ;
            
            //pass an instance of the custom object
            xslArgs.AddExtensionObject("urn:custDate", custDate) ;
                        
            //create the output stream
            XmlTextWriter myWriter = new XmlTextWriter("extendXSLT.html", null) ;
            
            //pass the args,do the actual transform of Xml
            myXslTrans.Transform(myXPathDoc,xslArgs, myWriter) ;        

            myWriter.Close() ;

        }catch(Exception e){

            Console.WriteLine("Exception: {0}", e.ToString());
        }
        
    }
    
    public static void PrintUsage(){
        Console.WriteLine("Usage: XsltExtension.exe <xml path> >xsl path<") ;
    }

}

//our custom class
public class CustomDate{
    
    //function that gets called from XSLT
    public string GetDateDiff(string xslDate){
        
        DateTime dtDOB = DateTime.Parse(xslDate) ;
        
        DateTime dtNow = DateTime.Today ;
        
        TimeSpan tsAge = dtNow.Subtract(dtDOB) ;
        
        return tsAge.Days.ToString() ;
    }

}
Compile this code and use the provided members.xml and memberdisplay.xsl to run this console application. You should see a extendXSLT.html file within the same folder. Open this file and notice that our class CustomDate has been called to calculate the number of days the employee has been in the company.


Summary :
XSLT is a powerfull transformation language for XML, however using extension objects in .NET and C# should ensure that we could easily accomplish what would be impossible or hard with XSLT alone.

XSLTExtention.cs (control-a to copy)



Members.xml (control-a to copy)



Memberdisplay.xsl (control-a to copy)


1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  XML
Current Lesson:
Extending XSLT using C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  XML transformation using Xslt in C# [next Lesson]  Using Stack class in C#



Today's Top Movers
vulpes 3766
MadHatter 2056
jal 675
muster 623
George2 545

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
simboy 1



Monthly Leaders
vulpes 3766
MadHatter 2096
jal 675
muster 623
George2 545

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
Gsuttie 6046
juliet 4679

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center