How to do a XSLT transformation in FreePascal? lazarus / 62-General ---------------------------------------------------------------------- kirchfritz · 2011-06-01 10:05 Any ideas or code snippets of how to do a simple XSLT Transformation, given a XML and a XSLT-stylesheet? Without calling XSLTProc.EXE as an external Process! Please! Fritz ---------------------------------------------------------------------- Arbee · 2011-06-01 13:06 Can't you use the libxslt library directly in Lazarus?  afaik xsltproc basically does that too. ---------------------------------------------------------------------- kirchfritz · 2011-06-01 14:56 Sorry I am a newbie in Freepascal. Do you have codesamples how to use libxslt library in FreePascal? ---------------------------------------------------------------------- Arbee · 2011-06-02 09:48 I do not have examples in FPC.  You can get information on libxslt (and I guess that in its turn needs libxml) here http://xmlsoft.org/XSLT/.  libxml already has a Pascal wrapper.  Don't know about libxslt. It is a C library, but I would assume it comes as a DLL in the Windows version.  Or if not, could be moulded into one.  Perhaps you can start a project to translate it into Pascal? by the way ... why is a detour via xsltproc or via another parser like saxon out of the question? ---------------------------------------------------------------------- kirchfritz · 2011-06-03 12:34 I dont want to torture the enduser with requirements like "You need saxon version xyz" or "You need XSLTPROC.EXE" or "You need Java Virtual machine version x.y.z" Give the enduser a single EXE together with an INI-file is the ideal way for me. That's why I want to do the XSLT transformation inside my FreePascal/Lazarus program without calling external resources. ---------------------------------------------------------------------- Rave · 2011-06-03 21:46 Can't you just distribute XSLTPROC.EXE alongside with program? ---------------------------------------------------------------------- williamda · 2011-09-19 14:47 If you are compiling for Windows, you can use MSXML via ActiveX that is always included in Windows. Here is an example: function ApplyStylesheet( DocumentFile: String; const XSLTFile: String): String; var   Doc, XSL: Variant; begin   XSL := CreateOleObject('MSXML.DOMDocument');   Doc := CreateOleObject('MSXML.DOMDocument');   oc.Load( DocumentFile );   XSL.Load( XSLTFile );   Result := Doc.transformNode(XSL); end; ---------------------------------------------------------------------- marcov · 2011-09-21 10:28 Quote from: williamda on September 19, 2011, 04:47:52 pmIf you are compiling for Windows, you can use MSXML via ActiveX that is always included in Windows. Beware: Afaik the one that comes with original XP is buggy. ---------------------------------------------------------------------- williamda · 2011-09-28 14:41 There is a very good parser library based in libxml2 called idom2-pas http://idom2-pas.sourceforge.net/ It´s DOM based and compatible with Windows MSXML DOM. It compiles in lazarus with very few changes and it´s cross platform. You only have to include some DLL/SO with your application. ---------------------------------------------------------------------- kirchfritz · 2011-12-03 16:23 I tried this: Code: [Select]program MyApp; {$mode objfpc}{$H+} uses   Classes, SysUtils,  COMObj; {$R *.res}   function ApplyStylesheet( DocumentFile: String; const XSLTFile: String): String;   var     Doc, XSL: Variant;   begin     XSL := CreateOleObject('MSXML.DOMDocument');     Doc := CreateOleObject('MSXML.DOMDocument');     Doc.ValidateOnParse := True;     Doc.Async := False;     XSL.ValidateOnParse := True;     XSL.Async := False;     XSL.Load( XSLTFile );     Doc.Load( DocumentFile );     Result := Doc.transformNode(XSL);   end; begin   if ParamCount <> 2 then   begin     writeln('Usage:');     writeln('Application ');     halt;   end;   if not fileExists(ParamStr(1)) then   begin     writeln('XML not found: ',ParamStr(1));     halt;   end;   if not fileExists(ParamStr(2)) then   begin     writeln('XSLT not found: ',ParamStr(2));     halt;   end;   Writeln(applyStylesheet(ParamStr(1),ParamStr(2))); end.       It compiles without error, but at run time program aborts with error message: Stylesheet is empty. I am abolutely sure that my stylesheet is not empty. Can anybody help me to find my error? ---------------------------------------------------------------------- Laksen · 2011-12-03 17:01 Looks like a bug in FPC since the following works: Code: [Select]  function ApplyStylesheet( DocumentFile: String; const XSLTFile: String): String;   var     Doc, XSL,t: Variant;   begin     XSL := CreateOleObject('MSXML.DOMDocument');     Doc := CreateOleObject('MSXML.DOMDocument');     Doc.ValidateOnParse := True;     Doc.Async := False;     XSL.ValidateOnParse := True;     XSL.Async := False;     t := XSLTFile;     XSL.load( t );     t := DocumentFile;     Doc.load( t );     Result := Doc.transformNode(XSL);   end; ---------------------------------------------------------------------- evresis · 2022-04-21 10:17 To avoid runtime xslt errors add this   var result : widestring;         ft:text;        err:variant;      aFileName:string;       aFileName:='myoutput';   if (XSL.parseError.errorCode <> 0) then begin       Err := XSL.parseError;       writeln('XSL error ' + Err.reason);     end else begin      Result:=Doc.transformNode(XSL);      assign(ft,aFileName);      rewrite(ft);      write(ft,result);      close(ft);    end;