Solved

MSXML: preserve whitespace in XML

Posted on 2004-04-02
11
1,165 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unattended XML settings 4 122
XML Removal- Powershell 4 28
Fulfillment API php code sample 1 56
In sql, how to roll up multiple rows to only one row. 4 30
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…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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