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
Qt Code:
#include <QtCore/QCoreApplication> #include <QXmlQuery> #include <QXmlFormatter> #include <QBuffer> #include <iostream> int main(int argc, char *argv[]) { "<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.write(content_str.toUtf8()); content_buffer.reset(); "<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.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; QXmlFormatter output_formatter(query, &output_buffer); output_formatter.setIndentationDepth(0); query.evaluateTo(&output_formatter); output_buffer.reset(); 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(); }To copy to clipboard, switch view to plain text mode
Bookmarks