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 .DOMDocume nt")
xmldoc.async = false
xmldoc.load(Server.MapPath ("styleshe ets.xml"))
Set oNode = xmldoc.documentElement.Sel ectSingleN ode("//sty lesheet[@i d='" & 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
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
xmldoc.async = false
xmldoc.load(Server.MapPath
Set oNode = xmldoc.documentElement.Sel
response.write oNode.SelectSingleNode("./
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
ASKER
Conor: I have allready tried that. It didn't work :-(
What's your raw XML look like with tags, etc.?
ASKER
<?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>
<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>
<screen>
body {
background : rgb(255,255,255);
}
p {
font-family: Arial;
}
</screen>
ASKER
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 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?
body {
background : rgb(255,255,255);
}
p {
font-family: Arial;
}
Which is what you want it to look like in IE, yes?
ASKER
exactlly!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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 :-)
xmldoc.async = false
xmldoc.load(Server.MapPath
to
xmldoc.async = false
xmldoc.preserveWhiteSpace = True
xmldoc.load(Server.MapPath
and your whitespace will be preserved.
Hope this helps.
rgds,
Conor.