cdonner
cdonner

Reputation: 37698

Preserving white space through XML/XSLT processing chain

  1. User types text into a multi-line text box in the browser
  2. Data is stored as part of an XML snippet in a SQL Server xml column
  3. XML snippet is later incorporated into a larger xml document
  4. Document is processed via XSLT into an HTML email
  5. Recipient is expected to see the original line breaks

I tried:

Can this be done without rearchitecting everything?

Per request in the comments, here is the actual code:

The XSL

<TD>
    <xsl:value-of select="//MemoComment"/>
</TD>

Variations of input data into the XSLT

  1. <MemoComment>Previous job: Associate, Boston<br/>Test1<br/>Test2</MemoComment>
  2. <MemoComment>Previous job: Associate, Boston &amp;#xA;Test1 &amp;#xA;Test2</MemoComment>
  3. <MemoComment>Previous job: Associate, Boston&amp;lt;br/&amp;gt;Test1&lt;br/&gt;Test2</MemoComment>

Corresponding XSLT Results

  1. <TD>Previous job: Associate, BostonTest1Test2</TD>
  2. <TD>Previous job: Associate, Boston&amp;#xA;Test1&amp;#xA;Test2</TD>
  3. <TD>Previous job: Associate, Boston&amp;lt;br/&amp;gt;Test1&lt;br/&gt;Test2</TD>

None of which renders correctly in Outlook.

Upvotes: 2

Views: 2177

Answers (2)

jasso
jasso

Reputation: 13986

Reason for your problem was that you used <xsl:value-of>

<TD>
    <xsl:value-of select="//MemoComment"/>
</TD>

when you actually needed <xsl:copy-of>

<TD>
    <xsl:copy-of select="//MemoComment/node()"/>
</TD>

<xsl:value-of> only selects the string value of the selected node. What you actually wanted is to copy of whole node including the elements that it contained. Then you can save the data as a formatted XHTML snippet without the need of element syntax escaping (format number 1 on your input list).

Usage of disabe-output-escaping="yes" is often discouraged because it can lead to outputting malformed XML. Also all XSLT processors don't implement this feature since it actually takes effect only when the document is serialized, and might not have any effect if the output document is passed on from the processor as a data structure.

Upvotes: 3

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 263047

Try embedding escaped <br> elements in your text and setting the disable-output-escaping attribute of your <xsl:value-of> element to yes:

<MemoComment>Previous job: Associate, Boston&lt;br/&gt;Test1&lt;br/&gt;Test2</MemoComment>

<xsl:value-of select="//MemoComment" disable-output-escaping="yes" />

Upvotes: 1

Related Questions