473,413 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

losing newlines and whitespace on xslt processing

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
3 4489
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tjerk Wolterink | last post by:
Hello all, how does xsl handle white space? I know you can set domething like this for nice indentation: <xsl:output method="xhtml" indent="yes"/> But know i have xsl code like this:
2
by: Wolfgang Jeltsch | last post by:
Hello, it is often convenient to insert whitespace into an XML document in order to format it nicely. For example, take this snippet of a notional DocBook XML document: <para> This is a...
1
by: Porthos | last post by:
Hello all, Is there a facet pattern that will allow for the inclusion of a newline or carriage return to occur within a tag? From the W3C schema document and from previous posts I've read that ...
7
by: jjouett | last post by:
I'm trying to preserve the newline characters in my transformed XML (read from a file) to provide a meaningful line number when validation errors occur. However, my current method which performs an...
3
by: thomas.porschberg | last post by:
Hi, I want to read records from a database and export it in an arbitrary format. My idea was to feed a class with a String array fetched from the database and let this class fire SAX events as...
4
by: lihao0129 | last post by:
Hi, folks: I recently went through a strange problem with my Javascript code, say: I have a string variable which are from a 'textarea' element and I want to remove the trailing newlines inside...
5
by: barcaroller | last post by:
I have a text file with mixed carriage returns ('\n' and '\r\n'). On Linux, both the std::string getline() global function and the std::iostream getline() member function are keeping some of the...
2
by: killy971 | last post by:
I have been testing different libraries to process XSL transformations on large XML files. The fact is that I read a document from Intel, stating their library (XSLT accelerator) was more twice...
5
by: John Gordon | last post by:
My XSLT files have many occurrences of this general pattern: <a> <xsl:attribute name="href"> <xsl:value-of select="xyz" /> </xsl:attribute> </a> When I execute an XSL transform, the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.