C# XSLT 变换添加 和 到输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/848841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# XSLT transform adding 
 and 
 to the output
提问by DMCS
I have an XSLT transform issue:
我有一个 XSLT 转换问题:
style="width:{Data/PercentSpaceUsed}%;"
And the value of Data/PercentSpaceUsed is integer 3.
而 Data/PercentSpaceUsed 的值为整数 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.
下面是做转换的代码:xslt_xslt
是转换XML,sw.ToString()
包含了
和

这是我没有想到。
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();
采纳答案by Robin Day
The 

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

是回车和换行的XML或您的XSLT内部将两种。确保 xml 就像
<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.
我相信有一种方法可以阻止在您的转换中使用空格,尽管我不知道它的存在。
回答by jelovirt
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)}%;"
回答by randomfigure
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
注意:使用上述设置,制表符用于缩进