Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# XSLT transform adding 
 and 
 to the output

I have an XSLT transform issue:

style="width:{Data/PercentSpaceUsed}%;"

And the value of Data/PercentSpaceUsed is integer 3.

And it outputs:

style="width:
     3
 %;"

instead of what I expected:

style="width:3%;"

Here's the code that does the transform: xslt_xslt is the transform xml, sw.ToString() contains the 
 and 
 which I did not expect.

var xslTransObj = new XslCompiledTransform();
var reader = new XmlTextReader(new StringReader(xslt_xslt));
xslTransObj.Load(reader);
var sw = new StringWriter();
var writer = new XmlTextWriter(sw);
xslTransObj.Transform(new XmlTextReader(new StringReader(xslt_data)), writer);

ResultLiteral.Text = sw.ToString();
like image 859
DMCS Avatar asked May 11 '09 16:05

DMCS


3 Answers

The 
 are carriage returns and line feeds either within your XML or your XSLT. Make sure the xml is like

<Value>3</Value>

Rather than

<Value>
    3
</Value>

I believe there is a way to stop whitespace being used within your transformation although I don`t know it off the top of my head.

like image 83
Robin Day Avatar answered Sep 30 '22 02:09

Robin Day


You're getting whitespace from the source document. Use

style="width:{normalize-space(Data/PercentSpaceUsed)}%;"

to strip out the whitespace. The other option in your case would be to use

style="width:{number(Data/PercentSpaceUsed)}%;"
like image 34
jelovirt Avatar answered Sep 30 '22 02:09

jelovirt


try


XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
settings.IndentChars = "\t";
settings.NewLineHandling = NewLineHandling.None;
XmlWriter writer = XmlWriter.Create(xmlpath, settings);

for input whitespace to be preserved on output for attribute values.

note: with above settings, tabs are used for indentation

like image 32
randomfigure Avatar answered Sep 30 '22 01:09

randomfigure