Hi folks,
My problem is this...
I am having an issue with whitespace between XML tags that occurs when I
transform the XSL output to the browser via ASP.NET leaving me with
unexpected results.
Read on if you dare.
I have a XML file (datum.xml) that contains numeric values as shown here
<datums>
<datum>18</datum>
<datum>35</datum>
<datum> 87</datum>
</datum>
I have a XSL file that extracts the data from the XML and generates SVG
(Scalable Vector Graphics). SVG is a W3C standard, much like XML, used to
describe graphic images.
The transformation occurs in my ASP.NET file as shown here...
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
// Response.AddHeader("Content-Type", "image/svg+xml");
string xmlPath = Request.PhysicalApplicationPath + @"datum.xml";
string xslPath = Request.PhysicalApplicationPath + @"datum.xsl";
XmlReader rdr = XmlReader.Create(xmlPath);
XslCompiledTransform transform = new XslCompiledTransform ();
//Load the XSL stylsheet into the XslCompiledTransform object
transform.Load(xslPath);
transform.Transform(rdr, null, Response.Output);
}
</script>
If the first line in my Page_Load function is commented out, the SVG is
displayed as text in the browser. If I uncomment the same line, I tell the
browser I am outputting SVG and I "expect" this to occur. It does not occur.
So I do a few tests. First I run the web app to view the SVG text displayed
in the browser that is shown here.
<?xml version="1.0" encoding="utf-8" ?>
- <svg height="100" width="100" viewBox="0 0 100 100"
xmlns:svg="http://www.w3.org/2000/svg">
<rect x="10" y="77" width="10" height="23" fill="red" stroke="black" />
<rect x="20" y="6" width="10" height="94" fill="red" stroke="black" />
<rect x="30" y="45" width="10" height="55" fill="red" stroke="black" />
</svg>
Next I copy the text as is and paste it into NotePad and then I save the
file as test1.svg. I then open my IE browser and select File->Open. I click
browse and select test1.svg. I click Ok and see a blank screen.
Finally I copy the same text in NotePad but this time I remove every space
and
CRLF between the tags. I save this file as test2.svg. I open my IE browser
and click File->Open to open test2.svg and I see a barchart which is my
expected result.
What I need to do is find a way to remove the whitespace and CRLF between
the tags in my ASP.NET code.
Any thoughts, ideas are welcome.
- Glenn
Martin Honnen - 28 Oct 2006 13:12 GMT
> - <svg height="100" width="100" viewBox="0 0 100 100"
> xmlns:svg="http://www.w3.org/2000/svg">
You need
<svg xmlns="http://www.w3.org/2000/svg"
to make sure the svg element and its descendants are in the SVG namespace.
Or, if you really want to use xmlns:svg="http://www.w3.org/2000/svg"
then you need to mark up your element as e.g.
<svg:svg xmlns:svg="http://www.w3.org/2000/svg" ...>
<svg:rect .../>
</svg:svg>

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Glenn - 28 Oct 2006 14:28 GMT
Martin,
Yes, I understand I need the xmlns:svg.
My issue involves how the SVG output is passed to the browser. If I take
the text that is displayed to the browser and remove all whitespace and CRLF
then the SVG
would look like this. You can copy and paste this into NotePad, save the
file and then open the file in IE where you will see the graphic image of the
barchart. Here's the SVG ...
<?xml version="1.0" encoding="utf-8" ?><svg height="100" width="100"
viewBox="0 0 100 100"
xmlns:svg="http://www.w3.org/2000/svg"><rect x="10" y="77" width="10"
height="23" fill="red" stroke="black" /><rect x="20" y="6" width="10"
height="94" fill="red" stroke="black" /><rect x="30" y="45" width="10"
height="55" fill="red" stroke="black" /></svg>
> > - <svg height="100" width="100" viewBox="0 0 100 100"
> > xmlns:svg="http://www.w3.org/2000/svg">
[quoted text clipped - 7 lines]
> <svg:rect .../>
> </svg:svg>
willib - 29 Oct 2006 13:22 GMT
Try this way, with svg as defaultnamespace
<?xml version="1.0" encoding="utf-8" ?><svg height="100" width="100"
viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg"><rect x="10" y="77" width="10"
height="23" fill="red" stroke="black" /><rect x="20" y="6" width="10"
height="94" fill="red" stroke="black" /><rect x="30" y="45" width="10"
height="55" fill="red" stroke="black" /></svg>
or that way
<?xml version="1.0" encoding="utf-8" ?>
<svg:svg height="100" width="100" viewBox="0 0 100 100"
xmlns:svg="http://www.w3.org/2000/svg">
<svg:rect x="10" y="77" width="10" height="23" fill="red" stroke="black" />
<svg:rect x="20" y="6" width="10" height="94" fill="red" stroke="black" />
<svg:rect x="30" y="45" width="10" height="55" fill="red" stroke="black" />
</svg:svg>
hth
WilliB
> Martin,
>
[quoted text clipped - 25 lines]
> > <svg:rect .../>
> > </svg:svg>
Glenn - 30 Oct 2006 14:47 GMT
willib,
Thanks for your help. I have made new ground and have successfully been
able to have my transform method write a SVG file out. This time I am using
different XML and XSL files for no real reason really so don't let this be of
concern as they do work together since the newly created SVG file when opened
in the browser displays the correct results.
I am now trying to get the file to automatically open by calling
Response.Output as shown in my code below...
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
Response.AddHeader("Content-Type", "image/svg+xml");
string xmlPath = Request.PhysicalApplicationPath + @"chart.xml";
string xslPath = Request.PhysicalApplicationPath + @"chart.xsl";
XmlReader rdr = null;
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
XslCompiledTransform transform = new XslCompiledTransform();
//Load the XML into the XslCompiledTransform object
transform.Load(xslPath);
XPathDocument xpathDoc = new XPathDocument(xmlPath);
rdr = XmlReader.Create(xslPath, settings);
XmlWriter wrtr = XmlWriter.Create("c:\\website\\svgOutput.svg");
// XmlWriter wrtr = XmlWriter.Create(Response.Output);
transform.Transform(xpathDoc, null, wrtr);
}
</script>
The line that instantiates the wrtr object works if you write to the
svgOutput.svg file
but if you comment the first line and uncomment the second line and pass
Response.Output, the screen is blank.
Any thoughts are appreciated.
-Glenn
> Try this way, with svg as defaultnamespace
> <?xml version="1.0" encoding="utf-8" ?><svg height="100" width="100"
[quoted text clipped - 47 lines]
> > > <svg:rect .../>
> > > </svg:svg>