Results 1 to 3 of 3

Thread: QXmlQuery XSLT stripping whitespace

  1. #1
    Join Date
    Jun 2009
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QXmlQuery XSLT stripping whitespace

    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 

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QXmlQuery XSLT stripping whitespace

    ... I have whitespace text nodes ...
    for me there are no such text nodes. You have just some nodes and whitespaces between them. What you want is that this XML:
    Qt Code:
    1. <root>
    2. <node>some text</node>
    3. <node>some other text</node>
    4. </root>
    To copy to clipboard, switch view to plain text mode 
    should mean something else than this:
    Qt Code:
    1. <root>
    2. <node>some text</node>
    3. <node>some other text</node>
    4. </root>
    To copy to clipboard, switch view to plain text mode 
    or this:
    Qt Code:
    1. <root>
    2.  
    3.  
    4.  
    5. <node>some text</node>
    6.  
    7.  
    8.  
    9. <node>some other text</node>
    10.  
    11.  
    12.  
    13. </root>
    To copy to clipboard, switch view to plain text mode 
    But they are all the same as an XML structure and means exactly the same as:
    Qt Code:
    1. <root><node>some text</node><node>some other text</node></root>
    To copy to clipboard, switch view to plain text mode 
    and this is the way they will be processed.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Jun 2009
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QXmlQuery XSLT stripping whitespace

    Hello faldżip,

    You are wrong concerning the importance of whitespace within an xml document. The rules that specify the content that must be kept and can be discarded within an XML document are known as Canonical XML. The Canonical XML specification clearly states that whitespace between elements within the document element must be retained and can not be discarded.

    http://www.w3.org/TR/xml-c14n#Exampl...spaceInContent

    This is especially crucial for mixed content schemas such as XHTML where "<em>hello</em> <em>hello</em>" is a very different beast than "<em>hello</em><em>hello</em>". Whitespace is content and stripping it is the destruction of said content.

    Thanks,
    Mark

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.