开发者

XSLT transformation adding unwanted Linefeed

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>&#xD;</xsl:text><xsl:text>&#xD;</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?


One way to solve this problem is to use an XmlTextWriter in a respective overload of XslCompiledTransform.Transform()

This instnce of XmlTextWriter must have its Settings property set with an instance of XmlWriterSettings for which you have specified whatever newline characters you want as the value of the XmlWriterSettings.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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜