/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <libxml/parser.h>
#include <libxslt/transform.h>

#define XML_FILE "employees.xml"
#define XSL_FILE "employees.xsl"

int main()
{
    xmlDoc *doc;
    xsltStylesheet *tf;
    xmlDoc *res;
    FILE *fp;
    /* read from file to DOM tree */
    doc = xmlReadFile(XML_FILE, NULL, 0);
    if (doc == NULL)
    {
        fprintf(stderr, "Failed to parse %s\n", XML_FILE);
        return EXIT_FAILURE;
    }
    /* transform from one dom to another dom */
    tf = xsltParseStylesheetFile(XSL_FILE);
    res = xsltApplyStylesheet(tf, doc, NULL);
    /* write out */
    fp = fopen("employees2.xml", "w");
    xmlDocDump(fp, res);
    fclose(fp);
    /* clean up */
    xmlFreeDoc(res);
    xsltFreeStylesheet(tf);
    xmlFreeDoc(doc);
    xmlCleanupParser();
    /* */
    return EXIT_SUCCESS;
}
