Re: [xslt] omit XML declaration in XSLT result



Al Pacifico schrieb:
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.

      //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);

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?

Hi Al,

I don't know about C, but quite often when xsl:output isn't respected,
that means the XSLT serializer isn't in charge because you're outputting
the result to a document object - not to a buffer or a string. If you
want the XSLT serializer to be in charge, you must write to a buffer.
But I don't know how that translates to the C API.

In Perl, there are a couple of options:

* output_string(result)
* output_as_bytes(result)
* output_as_chars(result)
* output_fh(result, fh)
* output_file(result, filename)

What can be done in Perl definitely can be done in C.

Hope this helps.

Michael Ludwig


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