[xslt] omit XML declaration in XSLT result
- From: Al Pacifico <adpacifico users sourceforge net>
- To: xslt gnome org
- Subject: [xslt] omit XML declaration in XSLT result
- Date: Wed, 22 Jul 2009 09:48:34 -0700
I'm using libxslt 1.1.24 (with dependency libxml2 2.7.3) from C++ to
convert some XML to JSON. The xsl:output element attributes
omit-xml-declaration and method are correctly set to "yes" and "text"
respectively. My JSON result string produced by the following code is
prefaced by an XML declaration, which is unwanted. Code
(json_utils.cc):
#include "json_utils.h"
#include <libxml++/libxml++.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <glibmm/i18n.h>
#include <sstream> //For stringstream
#include <string>
#include <iostream>
#include <fstream>
<snip>
Glib::ustring result;
//Use libxslt to transform the XML:
xmlDocPtr style = xmlReadMemory(stylestring.c_str(),
stylestring.length(),0,0, 0);
if (style)
{
//Parse the stylesheet:
xsltStylesheetPtr cur = xsltParseStylesheetDoc(style);
if(cur)
{
//Use the parsed stylesheet on the XML:
xmlDocPtr pDocOutput = xsltApplyStylesheet(cur,
const_cast<xmlDoc*>(xml_document.cobj()), 0);
xsltFreeStylesheet(cur);
//Get the output text:
xmlChar* buffer = 0;
int length = 0;
xmlIndentTreeOutput = 1; //Format the output with extra white
space. TODO: Is there a better way than this global variable?
xmlDocDumpFormatMemoryEnc(pDocOutput, &buffer, &length, 0, 0);
// GOTCHA TODO This is outputting the XML declaration despite
// the xsl:output directive in the stylesheet. Funny thing is, the
// same stylesheet does not do that with xsltproc. Need to trace
// with gdb with debuginfo RPM's?
if(buffer)
{
// Create a Glib::ustring copy of the buffer
// Here we force the use of Glib::ustring::ustring(
InputIterator begin, InputIterator end )
// instead of Glib::ustring::ustring( const char*, size_type )
because it
// expects the length of the string in characters, not in bytes.
result = Glib::ustring( reinterpret_cast<const char
*>(buffer), reinterpret_cast<const char *>(buffer + length) );
// Deletes the original buffer
xmlFree(buffer);
}
xmlFreeDoc(pDocOutput);
}
}
return result;
My result string is beginning with '<?xml version="1.0"
encoding="UTF-8"?>\n', not what I want. Same stylesheet (available at
http://www.xml.lt/static/files/xml2json.xsl) and xsltproc or
libxslt_tutorial.c from the libxslt website works fine.
I must be doing something wrong. Guidance?
-Al
--
Al Pacifico
Seattle, WA
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]