XML/XSL Demo

The demo allows you to load XML file, recognize the reference to XSL (or load a new XSL file), perform transformation of the XML text to an HTML text and save the result in the file.

XML/XSL Demo

The XML/XSL technology allows you to not care about current design of the pages when you're creating data (information) for these pages. You can transform data later, when the design issue will take place. Thanks to Microsoft , developers have a library, called XML DOM, which allows you to implement XML/XSL technology. The Delphi sample would look simple as follows:

function TForm1.TansformDataToHTML ( const XMLFName, XSLFName: string ): string ;
var XMLDoc, XSLDoc: IXMLDOMDocument;
begin
     XMLDoc := CoDOMDocument.Create;
     XSLDoc := CoDOMDocument.Create;
     try
         if not XMLDoc.load(XMLFName) then Exit;
         if not XSLDoc.load(XSLFName) then Exit;
         Result := XMLDoc.transformNode(XSLDoc);
     finally
         XMLDoc := nil;
         XSLDoc := nil;
     end;
end;

Other parts of the program simply provide the user interface features.

Some changes. Jan 05, 2005.

Since publication of the example, I've received a lot of emails, where authors are pointing out to some difficulties with encodings. I would like to say thanks to Iasen Drenski, Bulgaria, for the help. These difficulties appear when the users want HTML files with different than English encodings, for example, 1251.

In order to fix the problem, there are some changes that has to be made:

1) Change the result type of TansformDataToHTML method

function TForm1.TansformDataToHTML ( const XMLFName, XSLFName: string ): WideString ;

2) Add a function that converts WideString into string using correct code page

function WStr2Str ( ASource: WideString; ACodePage: Integer): string ;
var Len: Integer;
begin
    if ASource = '' then Result := ''
    else begin
       Len := WideCharToMultiByte(ACodePage, 0, PWideChar(ASource), -1, nil, 0, nil, nil);
      SetLength(Result, Len - 1);
      WideCharToMultiByte(ACodePage, 0, PWideChar(ASource), -1, PChar(Result), Len, nil, nil);
    end ;
end;

3) Change SaveAsHTMLActionExecute method so the result would be converted to the correct code page (for example 1251)

Writeln(F, WStr2Str(TansformDataToHTML(FXmlFName, FXslFName), 1251));

To create more general application, the code page value could be read from XML or XSL first.

Also, the result of TansformDataToHTML could be saved to the file as WideString.