XSLT 转换添加不需要的换行符
我正在使用 Vs 2010 编辑一个 xsl 文件,该文件将用于转换以输出 xml 文件。我试图输出一个回车符(),仅此而已。我不希望出现换行符 (x0A)。
无论我在 xsl 中做什么特技,输出的文件总是在我的 CR (CRLF) 中添加一个 LF,而我不希望这样。我只想要 CR 的原因是输出由发送 SMS 消息的辅助系统解析,该系统只需要 LF。
我想知道问题是否出在 xsl 或转换过程中。
样式表:
<xsl:stylesheet xmlns:l="http://schemas.something.no/2008/" version='1.0'>
<xsl:import href='LydiaDateFuncs.xsl'/>
<xsl:template match='/'>
<xsl:apply-imports/>
</xsl:template>
<xsl:output method="xml" encoding="utf-8" indent="no" />
<xsl:template match="l:WorkOrderOccurrenceBE">
<Message>
<Body>
Adding cr here<xsl:text>
</xsl:text><xsl:text>
</xsl:text>No dice.
</Body>
</Message>
</xsl:template>
转换代码:
public static XmlDocument TransformObject(string xslFilepath, object serObj)
{
XmlDocument result = null;
xslTransf.Load(xslFilepath, new XsltSettings { EnableScript = true }, newXmlUrlResolver());
result = TransformObject(xslTransf, serObj);
}
public static XmlDocument TransformObject(XslCompiledTransform xslTransform, object transformObject)
{
XmlDocument result = null;
if (xslTransform != null)
{
DataContractSerializer serializer = new DataContractSerializer(transformObject.GetType());
string serializedObjValue = null;
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, transformObject);
serializedObjValue = (new UTF8Encoding()).GetString(stream.ToArray());
}
if (serializedObjValue != null)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(serializedObjValue);
XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.InsertBefore(xmldec, xmldoc.DocumentElement);
using (Stream stream = new MemoryStream())
{
xslTransform.Transform(xmldoc, null, stream);
stream.Position = 0;
using (StreamReader sr = new StreamReader(stream, Encoding.Unicode))
{
string xmlRes = sr.ReadToEnd();
result = new XmlDocument();
result.LoadXml(xmlRes);
}
}
}
}
return result;
}
有人遇到过类似的问题吗?
Using Vs 2010 I am editing a xsl file which will be used for transformation to output an xml file. I am trying to output a carriage return ( ) and that only. I don't want the linefeed character (x0A) to appear.
No matter what kind of stunt I do in the xsl, the outputted file always adds a LF to my CR (CRLF) and I don't want that. The reason for why I want only the CR is that the output is parsed by a secondary system that sends sms-messages, which requires only LF.
I'm wondering if the problem is in the xsl or the transformation process.
The stylesheet:
<xsl:stylesheet xmlns:l="http://schemas.something.no/2008/" version='1.0'>
<xsl:import href='LydiaDateFuncs.xsl'/>
<xsl:template match='/'>
<xsl:apply-imports/>
</xsl:template>
<xsl:output method="xml" encoding="utf-8" indent="no" />
<xsl:template match="l:WorkOrderOccurrenceBE">
<Message>
<Body>
Adding cr here<xsl:text>
</xsl:text><xsl:text>
</xsl:text>No dice.
</Body>
</Message>
</xsl:template>
The code for transformation:
public static XmlDocument TransformObject(string xslFilepath, object serObj)
{
XmlDocument result = null;
xslTransf.Load(xslFilepath, new XsltSettings { EnableScript = true }, newXmlUrlResolver());
result = TransformObject(xslTransf, serObj);
}
public static XmlDocument TransformObject(XslCompiledTransform xslTransform, object transformObject)
{
XmlDocument result = null;
if (xslTransform != null)
{
DataContractSerializer serializer = new DataContractSerializer(transformObject.GetType());
string serializedObjValue = null;
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, transformObject);
serializedObjValue = (new UTF8Encoding()).GetString(stream.ToArray());
}
if (serializedObjValue != null)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(serializedObjValue);
XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.InsertBefore(xmldec, xmldoc.DocumentElement);
using (Stream stream = new MemoryStream())
{
xslTransform.Transform(xmldoc, null, stream);
stream.Position = 0;
using (StreamReader sr = new StreamReader(stream, Encoding.Unicode))
{
string xmlRes = sr.ReadToEnd();
result = new XmlDocument();
result.LoadXml(xmlRes);
}
}
}
}
return result;
}
Anybody been experiencing a similar issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决此问题的一种方法是在
XslCompiledTransform.Transform()
的相应重载中使用XmlTextWriter
,此
XmlTextWriter
实例必须具有其 <使用XmlWriterSettings
实例设置 code>Settings 属性,您已为其指定了所需的任何换行符作为XmlWriterSettings.NewLineChars
属性。请参阅:http://msdn.microsoft。 com/en-us/library/system.xml.xmlwriter.settings.aspx
和
http://msdn.microsoft.com/en-us/library /system.xml.xmlwritersettings.newlinechars.aspx
One way to solve this problem is to use an
XmlTextWriter
in a respective overload ofXslCompiledTransform.Transform()
This instnce of
XmlTextWriter
must have itsSettings
property set with an instance ofXmlWriterSettings
for which you have specified whatever newline characters you want as the value of theXmlWriterSettings.NewLineChars
property.See: http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.settings.aspx
and
http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.newlinechars.aspx