simula67
4th June 2009, 06:10
Hello,
I am using QXmlQuery to transform some xml. The problem that I am experiencing is that when I have whitespace text nodes between elements that are transformed, the whitespace text nodes are discarded. I have included a ready example below. In this example, the spaces between the <b> elements are stripped and a newline is added. If there are non-whitespace characters between the <b> elements, nothing is stripped and no newline is added. Likewise, if two <b> elements are adjacent with no whitespace separating them, a newline is added.
I have looked through the docs for anything related to normalizing whitespace, but my searches have thus far turned up empty.
Thanks for any help,
Mark
#include <QtCore/QCoreApplication>
#include <QXmlQuery>
#include <QXmlFormatter>
#include <QBuffer>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString content_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<article>Hey <b>txt</b> <b>txt</b><b>yes</b> <b>no</b> and <b>or</b> normal text</article>";
QBuffer content_buffer;
content_buffer.open(QBuffer::ReadWrite);
content_buffer.write(content_str.toUtf8());
content_buffer.reset();
QString transform_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<xsl:stylesheet version=\"2.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
" <xsl:template match=\"/\"><xsl:apply-templates/></xsl:template>"
" <xsl:template match=\"article\"><div id=\"article\"><xsl:apply-templates/></div></xsl:template>"
" <xsl:template match=\"b\"><strong><xsl:apply-templates/></strong></xsl:template>"
"</xsl:stylesheet>";
QBuffer transform_buffer;
transform_buffer.open(QBuffer::ReadWrite);
transform_buffer.write(transform_str.toUtf8());
transform_buffer.reset();
// Define query.
QXmlQuery query(QXmlQuery::XSLT20);
query.setFocus(&content_buffer);
query.setQuery(&transform_buffer);
// Evaluate query to an output buffer.
QBuffer output_buffer;
output_buffer.open(QBuffer::ReadWrite);
QXmlFormatter output_formatter(query, &output_buffer);
output_formatter.setIndentationDepth(0);
query.evaluateTo(&output_formatter);
output_buffer.reset();
QString output_xml_str(output_buffer.data());
std::cout << output_xml_str.toStdString() << std::endl;
// Output is:
//<div id="article">Hey <strong>txt</strong>
//<strong>txt</strong>
//<strong>yes</strong>
//<strong>no</strong> and <strong>or</strong> normal text</div>
return a.exec();
}
I am using QXmlQuery to transform some xml. The problem that I am experiencing is that when I have whitespace text nodes between elements that are transformed, the whitespace text nodes are discarded. I have included a ready example below. In this example, the spaces between the <b> elements are stripped and a newline is added. If there are non-whitespace characters between the <b> elements, nothing is stripped and no newline is added. Likewise, if two <b> elements are adjacent with no whitespace separating them, a newline is added.
I have looked through the docs for anything related to normalizing whitespace, but my searches have thus far turned up empty.
Thanks for any help,
Mark
#include <QtCore/QCoreApplication>
#include <QXmlQuery>
#include <QXmlFormatter>
#include <QBuffer>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString content_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<article>Hey <b>txt</b> <b>txt</b><b>yes</b> <b>no</b> and <b>or</b> normal text</article>";
QBuffer content_buffer;
content_buffer.open(QBuffer::ReadWrite);
content_buffer.write(content_str.toUtf8());
content_buffer.reset();
QString transform_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<xsl:stylesheet version=\"2.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
" <xsl:template match=\"/\"><xsl:apply-templates/></xsl:template>"
" <xsl:template match=\"article\"><div id=\"article\"><xsl:apply-templates/></div></xsl:template>"
" <xsl:template match=\"b\"><strong><xsl:apply-templates/></strong></xsl:template>"
"</xsl:stylesheet>";
QBuffer transform_buffer;
transform_buffer.open(QBuffer::ReadWrite);
transform_buffer.write(transform_str.toUtf8());
transform_buffer.reset();
// Define query.
QXmlQuery query(QXmlQuery::XSLT20);
query.setFocus(&content_buffer);
query.setQuery(&transform_buffer);
// Evaluate query to an output buffer.
QBuffer output_buffer;
output_buffer.open(QBuffer::ReadWrite);
QXmlFormatter output_formatter(query, &output_buffer);
output_formatter.setIndentationDepth(0);
query.evaluateTo(&output_formatter);
output_buffer.reset();
QString output_xml_str(output_buffer.data());
std::cout << output_xml_str.toStdString() << std::endl;
// Output is:
//<div id="article">Hey <strong>txt</strong>
//<strong>txt</strong>
//<strong>yes</strong>
//<strong>no</strong> and <strong>or</strong> normal text</div>
return a.exec();
}