Solved

MSXML: preserve whitespace in XML

Posted on 2004-04-02
11
1,156 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:AndreasA
11 Comments
 
LVL 5

Expert Comment

by:conorj
ID: 10739672
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.
0
 

Author Comment

by:AndreasA
ID: 10739726
Conor: I have allready tried that. It didn't work :-(
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10740809
What's your raw XML look like with tags, etc.?
0
 

Author Comment

by:AndreasA
ID: 10740914
<?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>
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10740943
So the screen tag looks like this?
<screen>
body {  
  background : rgb(255,255,255);  
  }
p {      
  font-family: Arial;  
  }      
</screen>
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:AndreasA
ID: 10740974
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;  
  }      
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10740996
>>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?
0
 

Author Comment

by:AndreasA
ID: 10741007
exactlly!
0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 total points
ID: 10741730
OK,
I have some good news...
The whitespace is preserved perfectly in the source when you output the document.

The bad news...
Doesn't matter what the source looks like, ie is going to flatten out your elements.

Here's the XML I used....

<?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>
              body {  
  background : rgb(255,255,255);  
  }
p {      
  font-family: Arial;  
  }  
              </screen>
            </mediatypes>
          </stylesheet>
          </stylesheets>
        </folders>
      </folder>
   </folders>
</css>

Here's the asp page  I used.  Notice it can take two paths depending on the query param "Raw", one where I write out the document directly, the second where I transform it with a stylesheet.

<% Dim xmlDoc
Dim xslDoc
Dim xslt
dim xslProc
set xmlDoc = Server.CreateObject("MSXML2.DOMDocument.4.0")
set xslDoc = Server.CreateObject("Msxml2.FreeThreadedDOMDocument.4.0")
xmlDoc.PreserveWhiteSpace=true
xmlDoc.Load("c:\inetpub\wwwroot\test\ct\css.xml")

if Request.QueryString("Raw") then
      response.Write(xmlDoc.xml)
else

      xslDoc.Load("c:\inetpub\wwwroot\test\sty\css.xslt")
      
      set xslt = Server.CreateObject("Msxml2.XSLTemplate.4.0")
      set xslt.stylesheet = xslDoc
        
      set xslProc = xslt.createProcessor()
      xslProc.input = xmlDoc
       xslProc.transform()
      
      response.Write(xslProc.output)
End if
%>

Here's the "Raw" source (what the actual XML output looks like):

<?xml version="1.0"?>
<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>
              body {  
  background : rgb(255,255,255);  
  }
p {      
  font-family: Arial;  
  }  
              </screen>
            </mediatypes>
          </stylesheet>
          </stylesheets>
        </folders>
      </folder>
   </folders>
</css>

Which in fact looks the same as the transformed output.

However, if I turned off xmlDoc.preserveWhitespace and transformed, this was the output...

<?xml version="1.0" standalone="yes"?>
<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>
              body {  
  background : rgb(255,255,255);  
  }
p {      
  font-family: Arial;  
  }  
              </screen>
</mediatypes>
</stylesheet>
</stylesheets>
</folders>
</folder>
</folders>
</css>

The one thing to note, is that in all the outputs, the <screen> elements contents were always rendered with the whitespace preserved.  So the fact that IE is flattening out the element whenit displays the file is not an issue with the underlying source, but just with IE.

So taking it little farther and just outputting the single node instead of the whole doe

Set oNode = xmldoc.documentElement.SelectSingleNode("//stylesheet[@id='" & Request("id") & "']")
response.write oNode.SelectSingleNode(".//screen").text

Actually returns...

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

which IE does flatten out when viewing, but as with the examples above, the whitespace is actually preserved.  So I bet if you look at the actual source of the page returned bu your current asp page you will find the same nicely preserved output



0
 
LVL 5

Expert Comment

by:pmsyyz
ID: 10745638
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
0
 

Author Comment

by:AndreasA
ID: 10747704
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 :-)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now