[xslt] xsltSaveTo() problem



  Hello,

 I'm facing a strange problem with xsltSaveTo() :
When the ouput method of the stylesheet is not
xml (eg, html or text), the buffer's write callback
is never called. On the other hand xsltSaveToFile() 
works nicely...
 I attach a small test program that fails.

	Fabrice

p.s : I'm using the latest releases of libxml/libxslt

-- 
Fabrice Desré - France Telecom R&D/DMI/GRI
Tel: +(33) 2 96 05 31 43
Fax: +(33) 2 96 05 32 86

#include <stdlib.h>
#include <stdio.h>

#include <libxml/xmlversion.h>
#include <libxml/xmlmemory.h>
#include <libxml/parserInternals.h>
#include <libxml/tree.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlerror.h>
#include <libxml/xmlIO.h>
#include <libxslt/imports.h>
#include <libxslt/xslt.h>
#include <libxslt/templates.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>

void close_xml_res(void * ctx) {
   fprintf(stderr, "Close callback called\n");
}

int write_xml_res(void * ctx, const char *buffer, int len) {
   char tmp[8192];
   
   fprintf(stderr, "Write callback called\n");
   snprintf(tmp, len, "%s", buffer);
   fprintf(stderr, "%s", tmp);
   return 1;
}

int transform(char *styleSheetStr, char *inputStr) {
   int se;
   xsltStylesheetPtr cur = NULL;
   xmlDocPtr xsldoc = NULL, doc = NULL, res = NULL;
   xmlOutputBufferPtr buf;
    
    doc = xmlParseMemory(inputStr, strlen(inputStr));
    if (doc == NULL)
       return 1;
    
    xsldoc = xmlParseMemory(styleSheetStr, strlen(styleSheetStr));
    if (xsldoc == NULL) {
       xmlFreeDoc(doc);
       return 1;
    }
        
    cur = xsltParseStylesheetDoc(xsldoc);
    if (cur == NULL) {
       xmlFreeDoc(xsldoc);
       return 1;
    }
    
    res = xsltApplyStylesheet(cur, doc, NULL);
    
    if (res != NULL) {
       buf = xmlOutputBufferCreateIO(write_xml_res, close_xml_res, NULL, NULL);
       if (buf != NULL) {
          fprintf(stderr, "xsltSaveResultTo --\n");
          xsltSaveResultTo(buf, res, cur);
	  fprintf(stderr, "--\n");
	  fprintf(stderr, "xsltSaveResultToFile --\n");
	  xsltSaveResultToFile(stderr, res, cur);
	  fprintf(stderr, "--\n");
	  xmlDebugDumpDocument(stderr, res);
       }
       else fprintf(stderr, "Unable to create IO buffer.\n");
    }
    xmlFreeDoc(doc);
    xmlFreeDoc(res);
    xsltFreeStylesheet(cur);
    
    return 0;
}

static char doc[] = "<?xml version='1.0' encoding='ISO-8859-1'?>\
<!DOCTYPE test>\
<test>\
  <tag>Hello. thi is a test</tag>\
  <tag2 att='test'>And now an attribute</tag2>\
</test>";

static char htmlssheet[] = "<?xml version='1.0' encoding='ISO-8859-1'?>\
<xsl:stylesheet version='1.0'\
                xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>\
<xsl:output method='html'/>\
<xsl:template match='/'>\
   <html><body>\
   <hr/>\
   <h2>\
   <xsl:apply-templates />\
   </h2>\
   <hr/>\
   </body></html>\
</xsl:template>\
</xsl:stylesheet>";

static char xmlssheet[] = "<?xml version='1.0' encoding='ISO-8859-1'?>\
<xsl:stylesheet version='1.0'\
                xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>\
<xsl:output method='xml'/>\
<xsl:template match='/'>\
   <html><body>\
   <hr/>\
   <h2>\
   <xsl:apply-templates />\
   </h2>\
   <hr/>\
   </body></html>\
</xsl:template>\
</xsl:stylesheet>";

static char txtssheet[] = "<?xml version='1.0' encoding='ISO-8859-1'?>\
<xsl:stylesheet version='1.0'\
                xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>\
<xsl:output method='text'/>\
<xsl:template match='/'>\
   <html><body>\
   <hr/>\
   <h2>\
   <xsl:apply-templates />\
   </h2>\
   <hr/>\
   </body></html>\
</xsl:template>\
</xsl:stylesheet>";

int main(int argc, char ** argv) {
   int ret;
   
   fprintf(stderr, "Testing XML output method :\n");
   ret = transform(xmlssheet, doc);
   
   fprintf(stderr, "\nTesting HTML output method :\n");
   ret = transform(htmlssheet, doc);
   
   fprintf(stderr, "\nTesting TEXT output method :\n");
   ret = transform(txtssheet, doc);
   
   return 0;  
}


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