Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you preserve leading and trailing whitespace in XML?

How does one tell the XML parser to honor leading and trailing whitespace?

Dim xml: Set xml = CreateObject("MSXML2.DOMDocument")
xml.async = False
xml.loadxml "<xml>1 2</xml>"
wscript.echo len(xml.documentelement.text)

Above prints out 3.

Dim xml: Set xml = CreateObject("MSXML2.DOMDocument")
xml.async = False
xml.loadxml "<xml> 2</xml>"
wscript.echo len(xml.documentelement.text)

Above prints out 1. (I'd like it to print 2).

Is there something special I can put in the xml document itself to tell the parser to keep leading and trailing whitespace in the document?

CLARIFICATION 1: Is there an attribute that can be specificed ONCE at the beginning of the document to apply to all elements?

CLARIFICATION 2: Because the contents of the entities may have unicode data, but the xml file needs to be plain ascii, all entities are encoded -- meaning CDATA's unfortunately are not available.

like image 328
Michael Pryor Avatar asked Feb 04 '23 12:02

Michael Pryor


1 Answers

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.DOMDocument
Private Sub Form_Load()
Set xml = CreateObject("MSXML2.DOMDocument")
xml.async = False
xml.loadxml "<xml> 2</xml>"
Dim n
n = 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.

like image 161
Dimitre Novatchev Avatar answered Apr 29 '23 05:04

Dimitre Novatchev