By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

losing newlines and whitespace on xslt processing

P: n/a
Hi, im trying to create a text email message using xslt template , the
transforms work great, but the newlines and whitespace in the xslt doc
are removed. Is there a setting somewhere I have missed:

My template is:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-
prefixes="msxsl">
<xsl:output indent="yes" method="text" media-type="text/xml"
encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="MailTemplate">

<xsl:value-of select="MailTitle"/>
<xsl:text disable-output-escaping="yes">
</xsl:text>
Dear <xsl:value-of select="RecipientName"/>,

<xsl:for-each select="BodylinesCollection/Line">
<xsl:text disable-output-escaping="yes"></xsl:text>
<xsl:text disable-output-escaping="yes"></xsl:text>
<xsl:value-of select="."/>
</xsl:for-each>
<xsl:text disable-output-escaping="yes"></xsl:text>
<xsl:value-of select="LinkUrl"/>
</xsl:template>
</xsl:stylesheet>

My processing is:

XmlSerializer xmlSerialization = new XmlSerializer(typeof
(MailTemplate));
xmlSerialization.Serialize(textWriter, mailTemplate);
memoryStream.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
XmlDocument input = new XmlDocument();
input.Load(memoryStream);
input.PreserveWhitespace = true;
XsltArgumentList args = new XsltArgumentList();
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(GetInvitationTemplate(false));
xsl.OutputSettings.NewLineOnAttributes = true;
xsl.Transform(input, args, resultStream);
resultStream.Flush();
resultStream.Seek(0, SeekOrigin.Begin);
plainText = reader.ReadToEnd();

I have tried playing with indent setting on xslt, no luck, I also
added:

input.PreserveWhitespace = true;
and
xsl.OutputSettings.NewLineOnAttributes = true;

none seem to have any effect.

thanks
Joel
Oct 1 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
joelkeepup wrote:
Hi, im trying to create a text email message using xslt template , the
transforms work great, but the newlines and whitespace in the xslt doc
are removed. Is there a setting somewhere I have missed:

My template is:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-
prefixes="msxsl">
<xsl:output indent="yes" method="text" media-type="text/xml"
encoding="UTF-8" omit-xml-declaration="yes" />
What exactly do you want to create with the stylesheet? You choose
output method="text" but then media-type="text/xml". That does not make
any sense to me.
<xsl:template match="MailTemplate">

<xsl:value-of select="MailTitle"/>
<xsl:text disable-output-escaping="yes">
</xsl:text>
What is all the disable-output-escaping good for? With output
method="text" that attribute has no meaning.
Dear <xsl:value-of select="RecipientName"/>,

<xsl:for-each select="BodylinesCollection/Line">
<xsl:text disable-output-escaping="yes"></xsl:text>
<xsl:text disable-output-escaping="yes"></xsl:text>
<xsl:value-of select="."/>
</xsl:for-each>
<xsl:text disable-output-escaping="yes"></xsl:text>
<xsl:value-of select="LinkUrl"/>
</xsl:template>
</xsl:stylesheet>
Please post the XML you want to process with the XSLT stylesheet and the
text you want to create as the result, then I am sure we can come up
with a stylesheet that creates that result.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 1 '08 #2

P: n/a
On Oct 1, 11:42*am, Martin Honnen <mahotr...@yahoo.dewrote:
joelkeepup wrote:
Hi, im trying to create a text email message using xslt template , the
transforms work great, but the newlines and whitespace in the xslt doc
are removed. Is there a setting somewhere I have missed:
My template is:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform"
* * xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-
prefixes="msxsl">
* <xsl:output indent="yes" method="text" media-type="text/xml"
encoding="UTF-8" omit-xml-declaration="yes" />

What exactly do you want to create with the stylesheet? You choose
output method="text" but then media-type="text/xml". That does not make
any sense to me.
* <xsl:template match="MailTemplate">
* * <xsl:value-of select="MailTitle"/>
* * <xsl:text disable-output-escaping="yes">
</xsl:text>

What is all the disable-output-escaping good for? With output
method="text" that attribute has no meaning.
* * Dear <xsl:value-of select="RecipientName"/>,
* * <xsl:for-each select="BodylinesCollection/Line">
* * * <xsl:text disable-output-escaping="yes"></xsl:text>
* * * <xsl:text disable-output-escaping="yes"></xsl:text>
* * * <xsl:value-of select="."/>
* * </xsl:for-each>
* * *<xsl:text disable-output-escaping="yes"></xsl:text>
* * *<xsl:value-of select="LinkUrl"/>
* </xsl:template>
</xsl:stylesheet>

Please post the XML you want to process with the XSLT stylesheet and the
text you want to create as the result, then I am sure we can come up
with a stylesheet that creates that result.

--

* * * * Martin Honnen --- MVP XML
* * * *http://JavaScript.FAQTs.com/
HI Martin,

im trying to output text, what is the correct media type in this case?

I can remove the escaping stuff, this was an html email and has been
adapted to send text.

The xml is:
"<?xml version=\"1.0\" encoding=\"utf-8\"?><InvitationMailTemplate
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=
\"http://www.w3.org/2001/XMLSchema\"><RecipientName>barney</
RecipientName><MailTitle>iBelong Invitations</
MailTitle><BodylinesCollection><Line>Joel has added you as a member of
the monday testolaa group.</Line></BodylinesCollection><LinkUrl>http://
localhost/Pages/Anonymous/SignIn.aspx?ReturnUrl=~/GroupHandler.ashx?
GroupId=44</LinkUrl><LinkUrlText>monday testolaa</
LinkUrlText><RemoveLinkUrl>http://localhost/Pages/Anonymous/
RejectInvitation.aspx?InvitationId=MgAxAA%3d%3d</
RemoveLinkUrl><RemoveLinkUrlText>here</
RemoveLinkUrlText><SecondLinkUrl>http://localhost/Pages/Anonymous/
SignUp.aspx?GroupId=44&amp;PersistentTheme=iBelong </
SecondLinkUrl><SecondLinkUrlText>here</SecondLinkUrlText></
InvitationMailTemplate>"
I want it to output something like: (including newlines and
whitespace)

iBelong Invitations

Dear Barney,

Joel has added you as a member of the monday testolaa

To get started, click to visit the group now:
http://localhost/Pages/Anonymous/Sig...shx?GroupId=44
.....

thanks
Joel
Oct 1 '08 #3

P: n/a
joelkeepup wrote:
im trying to output text, what is the correct media type in this case?
text/plain
I can remove the escaping stuff, this was an html email and has been
adapted to send text.

The xml is:
"<?xml version=\"1.0\" encoding=\"utf-8\"?><InvitationMailTemplate
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=
\"http://www.w3.org/2001/XMLSchema\"><RecipientName>barney</
RecipientName><MailTitle>iBelong Invitations</
MailTitle><BodylinesCollection><Line>Joel has added you as a member of
the monday testolaa group.</Line></BodylinesCollection><LinkUrl>http://
localhost/Pages/Anonymous/SignIn.aspx?ReturnUrl=~/GroupHandler.ashx?
GroupId=44</LinkUrl><LinkUrlText>monday testolaa</
LinkUrlText><RemoveLinkUrl>http://localhost/Pages/Anonymous/
RejectInvitation.aspx?InvitationId=MgAxAA%3d%3d</
RemoveLinkUrl><RemoveLinkUrlText>here</
RemoveLinkUrlText><SecondLinkUrl>http://localhost/Pages/Anonymous/
SignUp.aspx?GroupId=44&amp;PersistentTheme=iBelong </
SecondLinkUrl><SecondLinkUrlText>here</SecondLinkUrlText></
InvitationMailTemplate>"
I want it to output something like: (including newlines and
whitespace)

iBelong Invitations

Dear Barney,

Joel has added you as a member of the monday testolaa

To get started, click to visit the group now:
http://localhost/Pages/Anonymous/Sig...shx?GroupId=44
....

Here is a sample stylesheet

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output media-type="text/plain" method="text" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:apply-templates select="InvitationMailTemplate"/>
</xsl:template>

<xsl:template match="InvitationMailTemplate">
<xsl:text>

</xsl:text>
<xsl:value-of select="MailTitle"/>
<xsl:text>

</xsl:text>
<xsl:text>Dear </xsl:text>
<xsl:value-of select="RecipientName"/>
<xsl:text>,</xsl:text>
<xsl:text>

</xsl:text>
<xsl:for-each select="BodylinesCollection/Line">
<xsl:value-of select="normalize-space(.)"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:text>

</xsl:text>
<xsl:text>To get started, click to visit the group now:</xsl:text>
<xsl:text>
</xsl:text>
<xsl:value-of select="LinkUrl"/>
</xsl:template>
</xsl:stylesheet>

You could then run that as follows to get a string result:

XslCompiledTransform proc = new XslCompiledTransform();
proc.Load(@"..\..\XSLTFile1.xslt");
StringWriter result = new StringWriter();
proc.Transform(@"..\..\XMLFile1.xml", null, result);
string text = result.ToString();
result.Close();
Console.WriteLine("|{0}|", result);

which outputs

|

iBelong Invitations

Dear barney,

Joel has added you as a member of the monday testolaa group.
To get started, click to visit the group now:
http://localhost/Pages/Anonymous/SignIn.aspx?ReturnUrl=~/GroupHandler.ashx?GroupId=44|

The bars "|{0}|" in the Console.WriteLine are just there to show any
leading or trailing white space here for the post, you would obviously
not include them in the real application.

So wrap any literal text including line breaks into
<xsl:text></xsl:textand output values with xsl:value-of, then you
should have it. You don't need any memory streams to create a string as
the transformation result, a StringWriter suffices.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 1 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.