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:
  1. #include <QtCore/QCoreApplication>
  2.  
  3. #include <QXmlQuery>
  4. #include <QXmlFormatter>
  5. #include <QBuffer>
  6.  
  7. #include <iostream>
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. QCoreApplication a(argc, argv);
  12.  
  13. QString content_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  14. "<article>Hey <b>txt</b> <b>txt</b><b>yes</b> <b>no</b> and <b>or</b> normal text</article>";
  15. QBuffer content_buffer;
  16. content_buffer.open(QBuffer::ReadWrite);
  17. content_buffer.write(content_str.toUtf8());
  18. content_buffer.reset();
  19.  
  20. QString transform_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  21. "<xsl:stylesheet version=\"2.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
  22. " <xsl:template match=\"/\"><xsl:apply-templates/></xsl:template>"
  23. " <xsl:template match=\"article\"><div id=\"article\"><xsl:apply-templates/></div></xsl:template>"
  24. " <xsl:template match=\"b\"><strong><xsl:apply-templates/></strong></xsl:template>"
  25. "</xsl:stylesheet>";
  26. QBuffer transform_buffer;
  27. transform_buffer.open(QBuffer::ReadWrite);
  28. transform_buffer.write(transform_str.toUtf8());
  29. transform_buffer.reset();
  30.  
  31. // Define query.
  32. QXmlQuery query(QXmlQuery::XSLT20);
  33. query.setFocus(&content_buffer);
  34. query.setQuery(&transform_buffer);
  35.  
  36. // Evaluate query to an output buffer.
  37. QBuffer output_buffer;
  38. output_buffer.open(QBuffer::ReadWrite);
  39. QXmlFormatter output_formatter(query, &output_buffer);
  40. output_formatter.setIndentationDepth(0);
  41. query.evaluateTo(&output_formatter);
  42. output_buffer.reset();
  43.  
  44. QString output_xml_str(output_buffer.data());
  45.  
  46. std::cout << output_xml_str.toStdString() << std::endl;
  47.  
  48. // Output is:
  49. //<div id="article">Hey <strong>txt</strong>
  50. //<strong>txt</strong>
  51. //<strong>yes</strong>
  52. //<strong>no</strong> and <strong>or</strong> normal text</div>
  53.  
  54. return a.exec();
  55. }
To copy to clipboard, switch view to plain text mode