13

I've been given a sample XML file (ultimately my client will receive several of these each day), and an XSLT file which will apparently transform the XML into something with a meaningful layout, suitable for displaying in a browser or printing.

I'd like to put something into an existing legacy Delphi app, such that the user can pick one of the XML files from the drive and 'display' it (in a TWebBrowser or similar).

I have no idea where to start, and Googling delphi, xml and xslt gets me examples that seem to be datamodule/database based; I just want to transform a given XML file into something on-screen. It looks like it might involve a PageProducer but I can't quite get my head around what I need to do. Anyone offer any pointers, or maybe a link to a quick tutorial?

Many thanks!

robsoft
  • 5,525
  • 4
  • 35
  • 47
  • Ok, this has piqued some interest from a couple of fellow Delphi programmers so I'll make a point of coming back here in the next few days and posting an example of how I got it working (when I do). :-) – robsoft Jun 13 '09 at 07:59
  • Kieveli's tip about adding the stylesheet reference in the doc was really useful, as it saved time when testing and checking things. Ken's tip did the trick, taking into account the change noted in his comments. – robsoft Jun 28 '09 at 08:31

4 Answers4

10
Uses
  XMLDoc, XMLIntf;

function Transform(XMLContent : string; XSLContent : string) : WideString;
var
  XML : IXMLDocument;
  XSL : IXMLDocument;
begin

  XML := LoadXMLData(XMLContent);
  XSL := LoadXMLData(XSLContent);

  XML.DocumentElement.TransformNode(XSL.DocumentElement, Result)

end;
Ken Byington
  • 160
  • 9
  • Couldn't get the code sample formatting to work. I'm using IE 8. Maybe that's the reason? – Ken Byington Jun 16 '09 at 12:57
  • 1
    In the example above, XMLContent is the XML you will be receiving and XSLContent is the XSLT. We use this to produce HTML pages from XML document templates. But the output is whatever the XSLT generates. No third party components needed. Uses the default Delphi XML parser. (MSXML) – Ken Byington Jun 16 '09 at 13:08
  • Just to help anyone: I used this and it needed me to call CoInitialize(nil) before calling the transform function and call CoUnInitialize(); after to free the memory. To use those functions, you need to add ActiveX and Windows to the uses clause. – Jonathan Aug 08 '10 at 16:51
6

I used an MSXML library to do the XSLT transformation in Delphi. It was a long time ago. Worked like a charm!

I'm not sure what output format your XSLT will generate, but knowing that will help you figure out how to display it. We generated HTML from XML via XSLT, and displayed it using an ActiveX Web Browser (IE) control on a pane in our application.

Here's a link on MSSXML and Delphi that might help.

BTW: If this is your first time working with XSLT, you can manually edit an XML file, and add in a directive to get it to display using a specific XSLT. When you open the XML in Firefox after the edit, the XSLT will be applied, and it will show you what will be the output of your MSXML calls. Here's the line you add manually to the xml:

<?xml-stylesheet type="text/xsl" href="myStyleSheet.xsl"?>
Kieveli
  • 10,944
  • 6
  • 56
  • 81
  • Thanks for this! When I edit the XML document as you suggest, I do get a change in appearance but it's clearly not using the information in the XSLT document properly; I'm wondering if they don't really match and so the transformation isn't working because it's not finding the entities the XSLT refers to. I'll go back to the client and ask for a bit more help. Thanks for getting me started though, really appreciate it. – robsoft Jun 13 '09 at 07:55
  • _"and it will show you what will be the output of your MSXML calls"_ >> no, when you apply your stylesheet that way and display it through Firefox, **the [TransformiiX](https://en.wikipedia.org/wiki/TransforMiiX) processor is used, not MSXML.** – Abel Sep 21 '15 at 15:41
2

If you need only transformations, you can use TXMLDocument, save to disk and than display the result in a TWebBrowser (via Navigate('file:///...')).

Personally I had some problems with MSXML so I started to use DIXML.

bluish
  • 26,356
  • 27
  • 122
  • 180
ErvinS
  • 1,116
  • 7
  • 13
  • +1 for the DIXML link, that looks really useful. I haven't explored this problem properly yet but I'm hoping I can do something simple just with e couple of standard components - so thanks for the suggestion! – robsoft Jun 13 '09 at 07:58
1

You can try install the AltovaXML, it can be call as a COM+. It is free and can be download from: http://www.altova.com/altovaxml.html

MsXml can only works with xslt 1.0 but AltovaXml can work with xslt 2.0

Feng
  • 11
  • 1
  • changed name to RaptorXML and not free, from what I am able to see (eitehr they hid it well, or there are only the trial downloads available) – ciuly Mar 12 '15 at 12:07