Re: [xslt] [xml] Losing DTD reference when saving XML file



On Wed, 2007-02-14 at 14:27 -0500, Daniel Veillard wrote:
> On Wed, Feb 14, 2007 at 01:49:30PM -0500, Ross Mohn wrote:
> > Any hint on how to keep the DTD in the XML file when saving it, please?
> > Thanks in advance! -Ross
> 
>   I don't know because I don't understand, libxml2 always saves it by
> default.

Thanks -- I did some more tests and discovered that it is removed from
the xmlDoc when I use the xsltApplyStylesheet() function. Shouldn't that
function treat the 2nd parameter as read-only? I've attached a short
test.c file that reproduces the problem. Any help appreciated!

Before running the program the head of test.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beeswax SYSTEM "beeswax.dtd">
<beeswax>
...

After running the program the head of test.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<beeswax>
...

Thanks -Ross

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

#define FILENAME "test.xml"
#define XSLTNAME "test.xslt"

int
main(int argc, char *argv[]) {
	int i;
	xmlDoc *doc, *tdoc;
	xmlParserCtxt *ctxt;
	xmlXPathContext *xp_ctxt;
	xsltStylesheet *cur;

	LIBXML_TEST_VERSION

	ctxt = xmlNewParserCtxt();
	if(ctxt == NULL) {
		fprintf(stderr, "Failed to allocate parser context\n");
		return 1;
	}
	fprintf(stderr, "Allocated parser context\n");

	doc = xmlCtxtReadFile(ctxt, FILENAME, NULL, XML_PARSE_DTDVALID);
	if(doc == NULL) {
		fprintf(stderr, "Failed to parse %s\n", FILENAME);
		xmlFreeParserCtxt(ctxt);
		return 1;
	}
	fprintf(stderr, "Parsed %s\n", FILENAME);

	if(ctxt->valid == 0) {
		fprintf(stderr, "Failed to validate %s\n", FILENAME);
		xmlFreeDoc(doc);
		xmlFreeParserCtxt(ctxt);
		return 1;
	}
	fprintf(stderr, "Validated %s\n", FILENAME);
	xmlFreeParserCtxt(ctxt);

	xp_ctxt = xmlXPathNewContext(doc);
	if(xp_ctxt == NULL) {
		fprintf(stderr, "Failed to allocate XPath context\n");
		return 1;
	}
	fprintf(stderr, "Allocated XPath context\n");

	cur = xsltParseStylesheetFile(XSLTNAME);

	/* THIS IS THE OFFENDING LINE */
	tdoc = xsltApplyStylesheet(cur, doc, NULL);

	xmlSaveFormatFile(FILENAME, doc, 1);
	fprintf(stderr, "Saved %s\n", FILENAME);

	xmlFreeDoc(tdoc);
	xsltFreeStylesheet(cur);

	xmlXPathFreeContext(xp_ctxt);
	xmlFreeDoc(doc);

	xmlMemoryDump();
	return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]