Re: [xslt] XSLT transformation to Plain Text using Python bindings requires using children().serialize()?



Sorry for the inconvenience,

the attached patch is the most recent version -- i needed
to fix a call to free to xmlMemFree.
There's the beginning of a test in file python/test/basic.py
that should be extendend during the next few days.

  Ralf
diff -rbu libxslt/python/libxslt-python-api.xml libxslt_patch/python/libxslt-python-api.xml
--- libxslt/python/libxslt-python-api.xml	Fri May 24 15:03:04 2002
+++ libxslt_patch/python/libxslt-python-api.xml	Sun Aug 25 13:00:00 2002
@@ -5,6 +5,12 @@
     </file>
   </files>
   <symbols>
+    <function name='xsltSaveResultToString' file='python'>
+      <info>Have the stylesheet serialize the result of a transformation to a python string</info>
+      <return type='char *' info='The result document as a string' />
+      <arg name='style' type='xsltStylesheetPtr' info='a parsed XSLT stylesheet'/>
+      <arg name='result' type='xmlDocPtr' info='The result document'/>
+    </function>
     <function name='xsltApplyStylesheet' file='python'>
       <info>Apply the stylesheet to the document</info>
       <return type='xmlDocPtr' info="the result document or NULL in case of error"/>
diff -rbu libxslt/python/libxslt.c libxslt_patch/python/libxslt.c
--- libxslt/python/libxslt.c	Wed Aug 21 19:00:38 2002
+++ libxslt_patch/python/libxslt.c	Sun Aug 25 14:39:34 2002
@@ -251,6 +251,49 @@
     return(py_retval);
 }
 
+PyObject *
+libxslt_xsltSaveResultToString(PyObject *self, PyObject *args) {
+    PyObject *py_retval;        /* our final return value, a python string   */ 
+    xmlChar  *buffer;
+    xmlChar  *tmp;
+    int       size    = 0;
+    int       emitted = 0;
+    xmlDocPtr result;
+    PyObject *pyobj_result;
+    xsltStylesheetPtr style;
+    PyObject *pyobj_style;
+
+    if (!PyArg_ParseTuple(args, (char *)"OO:xsltSaveResultToString", &pyobj_style, &pyobj_result))
+      goto FAIL;
+    result = (xmlDocPtr) PyxmlNode_Get(pyobj_result);
+    style  = (xsltStylesheetPtr) Pystylesheet_Get(pyobj_style);
+
+     
+    /* FIXME: We should probably add more restrictive error checking
+     * and raise an error instead of "just" returning NULL.
+     * FIXME: Documentation and code for xsltSaveResultToString diff
+     * -> emmitted will never be positive non-null. 
+     */
+    emitted = xsltSaveResultToString(&buffer, &size, result, style);
+    if(!buffer || emitted < 0) 
+      goto FAIL;
+    /* We haven't tested the aberrant case of a transformation that
+     * renders to an empty string. For now we try to play it save.
+     */
+    if(size)
+      {
+      buffer[size] = '\0';
+      py_retval = PyString_FromString((char *) buffer); 
+      xmlMemFree(buffer);
+      }
+    else
+      py_retval = PyString_FromString("");
+    return(py_retval);
+ FAIL:
+    return(0);
+}
+
+
 /************************************************************************
  *									*
  *			Error message callback				*
diff -rbu libxslt/python/tests/basic.py libxslt_patch/python/tests/basic.py
--- libxslt/python/tests/basic.py	Thu Feb  7 23:34:59 2002
+++ libxslt_patch/python/tests/basic.py	Sun Aug 25 14:36:53 2002
@@ -1,4 +1,5 @@
 #!/usr/bin/python -u
+import sys
 import libxml2
 import libxslt
 
@@ -11,6 +12,10 @@
 doc = libxml2.parseFile("test.xml")
 result = style.applyStylesheet(doc, None)
 style.saveResultToFilename("foo", result, 0)
+stringval = style.saveResultToString(result)
+if (len(stringval) != 68):
+  print "Error in saveResultToString"
+  sys.exit(255)
 style.freeStylesheet()
 doc.freeDoc()
 result.freeDoc()
Only in libxslt_patch/python/tests: foo


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