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



On Sat, Aug 24, 2002 at 01:26:11PM -0400, Daniel Veillard wrote:

>   in python/libxslt.c :-)

Ok, ok, _that_ was the easy spot, i had to understand the way
libxslt-python-api.xml works in generating the libxsltclass.py
file. Anyway, patch attached (is 'diff  -rbu' the prefered format?).
Oh, BTW, i think there's a small documentation bug in libxslt -- in
file xsltutils.c the gtk-doc comment states that xsltSaveResultToString
"Returns the number of byte written or -1 in case of failure". Looking
at the code i couldn't find the path that would return the number of
byte written, the three return statements either return zero (on what
i'd take to be success) or -1.

  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 13:00:05 2002
@@ -251,6 +251,48 @@
     return(py_retval);
 }
 
+PyObject *
+libxslt_xsltSaveResultToString(PyObject *self, PyObject *args) {
+    PyObject *py_retval;        /* our final return value, a python string   */ 
+    xmlChar  *buffer;
+    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))
+        return(NULL);
+    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) 
+      {
+	return(NULL);
+      }
+    /* We haven't tested the aberrant case of a transformation that
+     * renders to an empty string. For now we try to play it safe.
+     */
+    if(size)
+      {
+      buffer[size] = '\0';
+      py_retval = PyString_FromString((char *) buffer); 
+      free(buffer);
+      }
+    else
+      py_retval = PyString_FromString("");
+    return(py_retval);
+}
+
+
 /************************************************************************
  *									*
  *			Error message callback				*


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