2

Hi I've got several XSLT 2.0 files. I need to transform these with C#.. I use the following code I got from this site: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63

public bool Transform(string XMLPath, string XSLPath, string newXMLname){

        try{

            XPathDocument myXMLPath = new XPathDocument(XMLPath);          //load the Xml doc
            XslCompiledTransform myXSLTrans = new XslCompiledTransform();

            myXSLTrans.Load(XSLPath);                                       //load the Xsl 

            XmlTextWriter myWriter = new XmlTextWriter(newXMLname, null);     //create the output stream

            myXSLTrans.Transform(myXMLPath, null, myWriter);                   //do the actual transform of Xml ---> fout!!??

            myWriter.Close() ;
            return true;


        }catch(Exception e){

            return false;
        }
    }

But it doesn't work.. I think it's because I use XSLT version 2.0. Is there a code/way to do this? Because there is no way to change my XSLT files to version 1.0...

Thanks in advance!

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
user737917
  • 107
  • 1
  • 1
  • 9
  • As much as I'd prefer to encourage using XSLT2, I can appreciate that being able to do it with just the .NET framework is preferable in a lot of cases. How much of your stylesheet has to be XSLT2? There may be a way of converting it to be XSLT1 compatible. – Flynn1179 May 23 '11 at 12:20
  • I have several XSLT 2.0 files. Most of them are functions.. which is only supported by 2.0. – user737917 May 23 '11 at 12:38
  • Ouch.. guess not then. Check out this question: http://stackoverflow.com/questions/1525299/xpath-and-xslt-2-0-for-net – Flynn1179 May 23 '11 at 12:45
  • I there an easy way to convert it to 1.0? Or it's only possible to do this manually? Thx! – user737917 May 23 '11 at 20:42
  • Unfortunately, it's pretty much a manual process. Much of XSLT2's improvements just make code simpler, and possible to do without using 'tricks' such as meunchian grouping. Functions can usually be done with named templates. If you load your XSLT docs into visual studio, it should tell you the majority of things it can't do; many might be easy to re-write in XSLT1. Any you can't convert, ask a question here. – Flynn1179 May 24 '11 at 00:04

2 Answers2

4

The two XSLT 2.0 processors that are designed to work in the .NET environment are Saxon.NET and XQSharp.

The XslCompiledTransform and XslTransform processors that come as part of .NET only implement XSLT 1.0.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Oke, thanks! Can you give me some help which C# code I need to use instead if I choose the Saxon? – user737917 May 23 '11 at 13:13
  • You must first: 1.Download a version of Saxon.Net and install it. 2. Read the Saxon.Net documentation -- there are many example files. – Dimitre Novatchev May 23 '11 at 13:20
  • Hmmm, so there is no way to do this without installing the Saxon.exe? My C# code and XSLT files will be implemented in a software tool. Maybe it's better to rewrite to XSLT 1.0, otherwise the installation of the Saxon needs to be implemented in the software too I guess? – user737917 May 23 '11 at 20:40
  • @user737917: Yes, but this is just copying several dlls -- no separate installation. – Dimitre Novatchev May 23 '11 at 22:57
  • @DimitreNovatchev, I have a quick question, (for practicing) I downloaded and referenced `saxon9ee-api.dll` in C# project and It just works fine. My question is, can I use it in my companies projects freely (for simple xml to XML/HTML transformations) ? or do I have to purchase it? – Rookie Programmer Aravind Feb 14 '13 at 12:42
  • I downloaded from http://www.saxonica.com/download/download_page.xml which doesn't prompt for payments. The license agreement does mention about payment but that doesn't clarify the usage. I also sent an Email to support@saxonica.com but haven't received any response about it. – Rookie Programmer Aravind Feb 14 '13 at 12:43
  • @InfantPro'Aravind', AFAIK, the license one gets for EE is a 30-day license. I think PE is absolutely free. Recommend contacting @ MichaelKay -- either personally, or by asking a question in the "saxon" tag. – Dimitre Novatchev Feb 14 '13 at 15:30
1

Natively .Net Framework doesn't support XSLT 2.0. I would suggest to use XSLT 1.0, but if you can't, then use third party component, for example Saxon.

VikciaR
  • 3,324
  • 20
  • 32
  • Okay, suppose I choose to go for the saxon-He 9.3 what should I do? I can put the source code from Saxon into C#? – user737917 May 23 '11 at 12:35
  • 1
    Make sure you download both the Saxon 9.3 for EE product, and the saxon-resources.zip file. In the resources file you will find plenty of examples of how to use the Saxon API from a C# application to run an XSLT 2.0 transformation. – Michael Kay May 23 '11 at 13:41