Solved

MSXML: preserve whitespace in XML

Posted on 2004-04-02
11
1,153 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
Comment Utility
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
Comment Utility
Conor: I have allready tried that. It didn't work :-(
0
 
LVL 12

Expert Comment

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

Author Comment

by:AndreasA
Comment Utility
<?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
Comment Utility
So the screen tag looks like this?
<screen>
body {  
  background : rgb(255,255,255);  
  }
p {      
  font-family: Arial;  
  }      
</screen>
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:AndreasA
Comment Utility
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
Comment Utility
>>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
Comment Utility
exactlly!
0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

13 Experts available now in Live!

Get 1:1 Help Now