Basically, I am transforming XML content into XHTML using XSLT. As
such the XSLT includes carefully formatted XHTML fragments so that the
final XHTML source code is readable. However, the line breaks and
indenting present in the XSLT file are getting lost during the
transform.
An example of an XHTML fragment in one of my XSLT templates:
<!--FOOTER TEMPLATE-->
<xsl:template name="Footer">
<table width="100%">
<tr><td align="center" class="centered_text">
<br />
<hr width="70%" style="color:#CCCCCC;"/>
Copyright Electronic Solutions Company. All rights reserved.
</td></tr>
</table>
</xsl:template>
<!--END OF FOOTER TEMPLATE-->
The above ends up in the final XHTML without any of the whitespace
before, between, and after the HTML elements using the following
managed C++ to do the transform:
// Create a reader to load the style sheet.
//XmlReader^ xsltReader = XmlReader::Create( sLocalFileName );
XmlTextReader^ xsltReader = gcnew XmlTextReader
( sLocalFileName );
XsltSettings^ xsltSettings = gcnew XsltSettings
( true, true );
XslCompiledTransform^ xsltTransform = gcnew
XslCompiledTransform;
xsltTransform->Load( xsltReader, xsltSettings, gcnew
XmlUrlResolver() );
//
// Get writer to the output string for use by the transform.
//
StringBuilder^ sb = gcnew StringBuilder();
//XmlWriterSettings^ writerSettings = gcnew XmlWriterSettings
();
//writerSettings->Encoding = encoding;
XmlWriter^ writer = XmlWriter::Create( sb, xsltTransform-
>OutputSettings );
//XmlTextReader^ reader = XmlTextReader::Create( gcnew
XmlNodeReader( xmldoc ) );
XmlTextReader^ xmlReader = gcnew XmlTextReader( gcnew
StringReader( xmldoc->OuterXml ) );
//
// Do it!
//
xsltTransform->Transform( xmlReader, writer );
The attempt at using an XmlReader and an XmlTextReader to load the
stylesheet are there to use their default values to preserve
whitespace. The results are no different for me than just loading the
transform with the XSLT file name directly.
This is the output element from my XSLT file:
<xsl:output method="xml" encoding="iso-8859-1" indent="yes" omit-
xml-declaration="yes"/>
FWIW: When using local client javascript (calling the DOM
transformNode method) to transform the XML for display to the user,
all works as expected. It is only when the above C++ is executed as
part of a post to an ASP.Net page using the ASP.Net Development Server
as the back end.
I'm hoping someone knows the magical combination of .Net reader and
setting classes and methods to without me working through the million
possible permutations -- ok, a million may be an exaggeration, but
there are a lot... :)
Any hints, thoughts, or criticisms?
Thanks for your help,
Steve