SOL9 Sample: XSLProcessor

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * XSLProcessor.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2011/02/06
// 2011/02/10 Updated to use XMLDOMDocumet based on __uuidof(DOMDocument60)

// Sample program to tranform xml by xsl using XSLTemplate anc XSLProcessor classes.

// If you use SOL::XSLProcessor class with SOL::XSLTemplate, you have to use SOL::XSLDocument class
// to load xsl document file instead of SOL::XMLDOMDocument.

#include <sol/COMInitializer.h>
#include <sol/Locale.h>
#include <sol/xml/XMLDOMDocument.h>
#include <sol/xml/XSLDOMDocument.h>
#include <sol/xml/XSLTemplate.h>
#include <sol/xml/XSLProcessor.h>


void _tmain(int argc, TCHAR** argv)
{
  if (argc !=3) {
    _tprintf(_T("Usage:\n%s sample.xsl sample.xml\n"), argv[0]);
    return;
  }

  if (GetFileAttributes(argv[1]) == 0xffffffff) {
    _tprintf(_T("File not found %s\n"), argv[1]);
    return;
  }

  if (GetFileAttributes(argv[2]) == 0xffffffff) {
    _tprintf(_T("File not found %s\n"), argv[1]);
    return;
  }

  COMInitializer comInitializer;
  Locale locale;
  
  try {
    const TCHAR* xslFile = argv[1];
    const TCHAR* xmlFile = argv[2];

    _tprintf(_T("1 Create XSLDOMDocument for xsl. \n"));
    SOL::XSLDOMDocument xslDocument;

    xslDocument.open(xslFile);
    _tprintf(_T("2 Opened a file %s\n"), xslFile);
      
    SOL::XSLTemplate xslTemplate;
    xslTemplate.putRefStylesheet(xslDocument.getNodePtr());
    _tprintf(_T("3 Put style sheet of %s to XSLTemplate object\n"), xslFile);

    XSLProcessor xslProcessor = xslTemplate.createProcessor ( );
    _tprintf(_T("4 Created an XSLProcessor object from the XSLTemplate object\n"));

    SOL::XMLDOMDocument xmlDocument;

    //2011/02/10 Added the following two lines,
    xmlDocument.putResolveExternals(VARIANT_TRUE);
    xmlDocument.setProperty("ProhibitDTD", VARIANT_FALSE);

    xmlDocument.open(xmlFile);
    _tprintf(_T("5 Opened a file %s\n"), xmlFile);

    xslProcessor.putInput( _variant_t((IUnknown*)xmlDocument) );
    _tprintf(_T("6 PutInput an XMLDOMDocument object of %s to  XSLProcessor\n"), xmlFile);

    _tprintf(_T("7 Call XSLProcessor ::tranform()\n"));
    xslProcessor.transform();

    _variant_t output = xslProcessor.getOutput();
    _bstr_t xml(output);
    _tprintf(_T("8 Transformed XML=[\n%s]\n"), (const TCHAR*)xml);

  } catch (HRESULT hr) {
    _tprintf(_T("Exception 0x%x\n"), hr);
  } catch (Exception& ex) {
    ex.printf();
  } catch (XMLDOMParseError& err) {
    err.display();
  } catch (_com_error& ex) {
    ComError comError(ex);
    comError.printf();
  }

}

Last modified: 2 May 2016

Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.