XSLT Problem : XML déclaration disappear

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I dont know if its the good place to ask this question but I have a problem with my XSL transformation. My xslt file works fine and produces the expected result. But, when Iam using this transformation in a WCF service operation, the XML declaration disappear
and I have a CRLF character instead.
Here is how it works :
First, I generate the XML to transform with a XmlDictionnaryWriter. If I place a breakpoint just after the ms.Position = 0 I have the XML declaration. I lost it after the CreateMessage call.
<pre class="prettyprint public Message GenDoc()
{
MemoryStream ms = new MemoryStream();
XmlDictionaryWriter xw = XmlDictionaryWriter.CreateTextWriter(ms);
xw.WriteStartDocument();
xw.WriteStartElement("Documentation");

... etc ...

xw.WriteEndDocument();
xw.Flush();
ms.Position = 0;

XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(ms, XmlDictionaryReaderQuotas.Max);
Message ret = Message.CreateMessage(MessageVersion.None, "*", new MyBodyWriter(xdr));
return ret;

}[/code]
<br/>

In order to perform the XSL transformation, I use a custom BodyWriter (WCF) like this :
<pre class="prettyprint class MyBodyWriter : BodyWriter
{
private static XslCompiledTransform _xslTransform = InitializeTransform();
private XmlDictionaryReader _originalRequestReader;

private static XslCompiledTransform InitializeTransform()
{
XslCompiledTransform xslTransform = new XslCompiledTransform();
xslTransform.Load(AppDomain.CurrentDomain.BaseDirectory + "\Doc\doc.xslt");
return xslTransform;
}

public MyBodyWriter(XmlDictionaryReader originalRequestReader)
: base(false)
{
this._originalRequestReader = originalRequestReader;
XslCompiledTransform transform = new XslCompiledTransform();
}

protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
_xslTransform.Transform(_originalRequestReader, writer);
}
}[/code]
I dont understand why the XML declaration disappear after the transformation and why it is replaced by a CRLF character.

Here is my xsl file :
<pre class="prettyprint <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0
<xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" indent="yes"/>
<xsl:template match="/ <html xmlns="http://www.w3.org/1999/xhtml
<head>

</head>
<body>

<table border="0" cellpadding="2" cellspacing="0" style="width: 100%;
<tr>
<td width="1%
<img alt="" src="/Doc/logo.png" width="50" />
</td>
<td>Documentation</td>
</tr>
</table>

<xsl:apply-templates/>

</body>
</html>

</xsl:template>

<xsl:template match="/Documentation/Operation

<table style="width:100%;
<tr>
<td colspan = "2
<b>
<xsl:value-of select="@Name"/> (<xsl:value-of select="Template/@Method"/>)
</b>
</td>
</tr>
<tr>
<td width="100" valign="top
Description :
</td>
<td>
<xsl:value-of select="Description"/>
</td>
</tr>
<tr>
<td valign="top
Template :
</td>
<td>
<xsl:value-of select="Template"/>
</td>
</tr>
<tr>
<td>
Roles :
</td>
<td>
<xsl:value-of select="Roles"/>
</td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>[/code]
<br/>

Thanks for your help

<br/>
<br/>

View the full article
 
Back
Top