Can you preserve leading and trailing whitespace in XML? Can you preserve leading and trailing whitespace in XML? xml xml

Can you preserve leading and trailing whitespace in XML?


As I commented, all answers recommending the usage of the xml:space="preserve" are wrong.

The xml:space attribute can only be used to control the treatment of whitespace-only nodes, that is text nodes composed entirely of whitespace characters.

This is not at all the case with the current problem.

In fact, the code provided below correctly obtains a length of 2 for the text node contained in:

<xml> 2</xml>

Here is the VB code that correctly gets the length of the text node (do not forget to add a reference to "Microsoft XML, v 3.0"):

Dim xml As MSXML2.DOMDocumentPrivate Sub Form_Load()Set xml = CreateObject("MSXML2.DOMDocument")xml.async = Falsexml.loadxml "<xml> 2</xml>"Dim nn = Len(xml.documentelement.selectSingleNode("text()").nodeValue)wscript.echo Len(n)End Sub

If you put a breakpoint on the line:

wscript.echo Len(n)

you'll see that when the debugger breaks there, the value of n is 2, as it is required.

Therefore, this code is the solution that was being sought.


As mentioned by Dimitre Novatchev, for XML, whitespace is not deletedat will by the parser. The white space is part if the node'svalue. Since I do not speak Visual Basic, here is a C program withlibxml which prints the length of the first text node. There isabsolutely no need to set xml:space.

% ./whitespace "<foo> </foo>"Length of " " is 1% ./whitespace "<foo> 2</foo>"Length of " 2" is 2% ./whitespace "<foo>1 2</foo>" Length of "1 2" is 3

Here is the program:

#include <stdio.h>#include <string.h>#include <libxml/parser.h>intmain(int argc, char **argv){    char           *xml;    xmlDoc         *doc;    xmlNode        *first_child, *node;    if (argc < 2) {        fprintf(stderr, "Usage: %s XML-string\n", argv[0]);        return 1;    }    xml = argv[1];    doc = xmlReadMemory(xml, strlen(xml), "my data", NULL, 0);    first_child = doc->children;    first_child = first_child->children;        /* Skip the root */    for (node = first_child; node; node = node->next) {        if (node->type == XML_TEXT_NODE) {            fprintf(stdout, "Length of \"%s\" is %i\n", (char *) node->content,                    strlen((char *) node->content));        }    }    return 0;}