Link to home
Start Free TrialLog in
Avatar of AndreasA
AndreasA

asked on

MSXML: preserve whitespace in XML

In short: I would like to preserve the whitespaces in XML. How is that done with MSXML?

The long version:

I am building a CMS where I store data in XML files.

The stylesheets (CSS) are stores in a XML file and called with an ASP-file on the output page. Like this: <link rel="stylesheet" type="text/css" href="css.asp?id=1" />

css.asp:

set xmldoc = Server.CreateObject("Msxml.DOMDocument")
xmldoc.async = false
xmldoc.load(Server.MapPath("stylesheets.xml"))
Set oNode = xmldoc.documentElement.SelectSingleNode("//stylesheet[@id='" & Request("id") & "']")
response.write oNode.SelectSingleNode(".//screen").text

The problem is that the MSXML parser removes all whitespaces and css like this:

body {  
  background : rgb(255,255,255);  
  }
p {       
  font-family: Arial;  
  }       

Becomes:

body { background : rgb(255,255,255);} p { font-family: Arial; }

Which the browser doesn't understand. So I need to preserve the whitespaces. But how?

I have tried to search the internet and found different solutions but can't seem to get it to work. Hope somebody can help me.

Cheers,

Andreas

Avatar of conorj
conorj
Flag of Ireland image

change

xmldoc.async = false
xmldoc.load(Server.MapPath("stylesheets.xml"))

to

xmldoc.async = false
xmldoc.preserveWhiteSpace = True
xmldoc.load(Server.MapPath("stylesheets.xml"))

and your whitespace will be preserved.

Hope this helps.

rgds,
Conor.
Avatar of AndreasA
AndreasA

ASKER

Conor: I have allready tried that. It didn't work :-(
What's your raw XML look like with tags, etc.?
<?xml version="1.0" encoding="iso-8859-1"?>
<css count="1" nextid="2" foldercount="1" foldernextid="2">
  <folders>
    <folder id="1">
      <title>My Stylesheet</title>
      <folders/>
        <stylesheets>
          <stylesheet id="1" rights="">
            <title>Indfødte folks</title>
            <mediatypes>
              <screen>
              </screen
                ...
            </mediatypes>
          </stylesheet>
        </folders>
      </folder>
   </folders>
</css>
So the screen tag looks like this?
<screen>
body {  
  background : rgb(255,255,255);  
  }
p {      
  font-family: Arial;  
  }      
</screen>
Yes and no...

When I look at my XML file in IE, the <screen> node looks like this (no whitespace):

body { background : rgb(255,255,255);} p { font-family: Arial; }


When I open the node in a textarea it looks like this (with withspace):

body {  
  background : rgb(255,255,255);  
  }
p {      
  font-family: Arial;  
  }      
>>When I open the node in a textarea it looks like this (with withspace):

body {  
  background : rgb(255,255,255);  
  }
p {      
  font-family: Arial;  
  }    

Which is what you want it to look like in IE, yes?
exactlly!
ASKER CERTIFIED SOLUTION
Avatar of dfiala13
dfiala13

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have you tried adding:

xml:space="preserve"

to your XML document's root element?

Perhaps the MSXML parser is aware of it and will obey it.

http://www.w3.org/TR/REC-xml/#sec-white-space
dfiala13: Thanks for an excellent answer. You are absolutely correct, its IE that flattens the stylesheet and the whitespace are actually preserved. It works beautifully :-)