2 Copyright (c) 2008 Instituto Nokia de Tecnologia
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13 * Neither the name of the INdT nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
31 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
32 * @date Mon Aug 25 15:50:20 2008
34 * @brief A XSLT helper module, converts a XML doc to another format
37 * Depends on libxml and libxslt.
40 * - doxygen comments about use
41 * - move code to a '.c' file or make functions static
42 * - make 'xslt_resources' an abstract type
49 #include <libxml/HTMLtree.h>
50 #include <libxml/catalog.h>
51 #include <libxml/debugXML.h>
52 #include <libxml/xinclude.h>
53 #include <libxml/xmlIO.h>
54 #include <libxml/xmlmemory.h>
55 #include <libxslt/transform.h>
56 #include <libxslt/xslt.h>
57 #include <libxslt/xsltInternals.h>
58 #include <libxslt/xsltutils.h>
61 struct xslt_resources
{
64 xsltStylesheetPtr cur
;
69 struct xslt_resources
*xslt_new(void)
71 struct xslt_resources
*result
;
72 result
= (struct xslt_resources
*)malloc(sizeof(struct xslt_resources
));
74 memset(result
, 0, sizeof(struct xslt_resources
));
79 int xslt_initialize(struct xslt_resources
*ctx
, const char *stylesheet_path
)
82 if (!stylesheet_path
|| !ctx
)
85 xmlSubstituteEntitiesDefault(1);
86 xmlLoadExtDtdDefaultValue
= 1;
89 xsltFreeStylesheet(ctx
->cur
);
91 ctx
->cur
= xsltParseStylesheetFile((const xmlChar
*)stylesheet_path
);
93 fprintf(stderr
, "Cannot create XSLT context!\n");
102 int xslt_transform(struct xslt_resources
*ctx
, const char *document
)
105 if (!ctx
|| !document
)
109 xmlFreeDoc(ctx
->doc
);
111 xmlFreeDoc(ctx
->output
);
114 ctx
->doc
= xmlReadMemory(document
, strlen(document
), "noname.xml",
117 fprintf(stderr
, "Cannot create document with "
122 ctx
->output
= xsltApplyStylesheet(ctx
->cur
, ctx
->doc
, NULL
);
124 fprintf(stderr
, "Cannot create document with "
130 xmlFree(ctx
->xml_str
);
133 xmlDocDumpMemory(ctx
->output
, &(ctx
->xml_str
), &(ctx
->length
));
139 xmlFreeDoc(ctx
->doc
);
143 xmlFreeDoc(ctx
->output
);
150 void xslt_delete(struct xslt_resources
*ctx
)
156 xmlFreeDoc(ctx
->doc
);
158 xmlFreeDoc(ctx
->output
);
160 xmlFree(ctx
->xml_str
);
162 xsltFreeStylesheet(ctx
->cur
);
163 xsltCleanupGlobals();