unit mdhLibxml2;

{
[mdhLibxml2] [1.0]
Delphi 2005
March 2006

LICENSE

The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
"http://www.mozilla.org/MPL/"

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.

The Original Code is "[mdhLibxml2.pas]".

The Initial Developer of the Original Code is Martin Holmes (Victoria,
BC, Canada, "http://www.mholmes.com/"). Copyright (C) 2005 Martin Holmes 
and the University of Victoria Computing and Media Centre. The code was 
co-developed for university and personal projects, and rights are shared
by Martin Holmes and the University of Victoria. All Rights Reserved.
}

{
Written by Martin Holmes, Spring 2006.

This unit provides wrapper functions for accessing libxml2 and associated
XML and XSLT libraries. The libxml2 libraries are usually distributed as
dlls, and need to be in the program folder. This library uses wrapper units
created by:

Eric Zurcher <Eric.Zurcher@csiro.au>
        - interface generation from API description via XSLT stylesheet 
Petr Kozelka <pkozelka@email.cz>
	-  original header translations

with contributions from:

Uwe Fechner <ufechner@4commerce.de>
	- testing, demo application(s), initiated the libxslt translation
Martijn Brinkers <martijn_brinkers@yahoo.co.uk>
Mikhail Soukhanov <m.soukhanov@geosys.ru>

http://xmlsoft.org/						- the libxml2 libraries web site
http://www.zlatkovic.com/projects/libxml/index.html		- Windows build of libxml2 by Igor Zlatkovic

The purpose of my library is to provide simple clean functions for making the
most common calls to the libxml2 libraries in order to (for example) perform
XSLT transformations on XML files and save the results.

Dependencies:

The following dlls need to be in the same folder as the executable calling these
functions:

        iconv.dll
        libexslt.dll
        libxml2.dll
        libxmlsec-openssl.dll
        libxslt.dll
        zlib1.dll

The following wrapper libraries are currently required from the "Libxml2 for pascal"
projced at <http://sourceforge.net/projects/libxml2-pas>:

        libxml2
        libxslt

As more functionality from these libraries is used in projects, the list of pas
files required may grow.

}


interface
  
uses
  Windows, Messages,  SysUtils, Classes, Graphics, Controls, Forms,
  libxml2, libxslt, XMLUtilities, TntSysUtils;

function mdhDoXSLTTransform(XMLFile: WideString;
                         XSLTFile: WideString;
                         OutputFileName: WideString): Boolean;

implementation

function mdhDoXSLTTransform(XMLFile: WideString;
                         XSLTFile: WideString;
                         OutputFileName: WideString): Boolean;


var
  TheXSLTStylesheetPtr : xsltStylesheetPtr;
  TheXMLDocPtr : xmlDocPtr;
  TheXMLResultPtr : xmlDocPtr;
  TheXSLTTransformContextPtr  : xsltTransformContextPtr;
  FMode: string;
  FParam: string;
  FParams: array of string;
  Params: PPChar;
  QName: WideString;
begin
  Result := False; //default
  try
{Pass in a QName for the XSLT to use if it needs to, based on the
 output file name.}
    FMode := '';
    QName := WideExtractFileName(OutputFileName);
    QName := WideChangeFileExt(QName, '');
    ufrmXMLUtilities.MakeXMLNCName(QName);
    FParam := '"' + string(QName) + '"';
    SetLength(FParams, 3);
    SetLength(FParams[0], Length('FileId'));
    FParams[0] := 'FileId';
    SetLength(FParams[1], Length(FParam));
    FParams[1] := FParam;
    FParams[2] := '';
    Params := PPChar(FParams);
    try
      TheXSLTStylesheetPtr := xsltParseStylesheetFile(pchar(string(XSLTFile)));
      TheXMLDocPtr := xmlParseFile(pchar(string(XMLFile)));
      TheXSLTTransformContextPtr := xsltNewTransformContext(TheXSLTStylesheetPtr, TheXMLDocPtr);
      TheXMLResultPtr := xsltApplyStylesheetUser(
                    TheXSLTStylesheetPtr, TheXMLDocPtr,
                    Params, nil, nil,
                    TheXSLTTransformContextPtr);
      {TheXMLResultPtr := xsltApplyStylesheet(TheXSLTStylesheetPtr, TheXMLDocPtr,
                                            Params);}
      xsltSaveResultToFileName(pchar(string(OutputFileName)),
                    TheXMLResultPtr,
                    TheXSLTStylesheetPtr, 0);
      Result := True;
    finally
//Clean up after ourselves
      xsltFreeTransformContext(TheXSLTTransformContextPtr);
      xsltFreeStylesheet(TheXSLTStylesheetPtr);
      xmlFreeDoc(TheXMLResultPtr);
      xmlFreeDoc(TheXMLDocPtr);
      xsltCleanupGlobals();
      xmlCleanupParser();
    end;
  except
//Returning false is enough
  end;

end;

end.
