<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dan Rigsby - Coding Up Style &#187; xslt</title>
	<atom:link href="http://www.danrigsby.com/blog/index.php/category/xslt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.danrigsby.com/blog</link>
	<description>Developer.Speaker.Blogger</description>
	<lastBuildDate>Tue, 17 Nov 2009 18:29:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Preserving line breaks in xml while transforming to html with xslt</title>
		<link>http://www.danrigsby.com/blog/index.php/2008/01/03/preserving-line-breaks-in-xml-while-transforming-to-html-with-xslt/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2008/01/03/preserving-line-breaks-in-xml-while-transforming-to-html-with-xslt/#comments</comments>
		<pubDate>Fri, 04 Jan 2008 04:01:48 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[xslt]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/?p=33</guid>
		<description><![CDATA[When using xslt to transform xml into html, sometimes it may be desired to keep the line breaks in the text of the xml and convert them into &#60;br/&#62; tags.&#160; For instance, we pipe the output of build and database scripts to xml.&#160; When rendering into html, we want to keep the line breaks or [...]]]></description>
			<content:encoded><![CDATA[<p>When using xslt to transform xml into html, sometimes it may be desired to keep the line breaks in the text of the xml and convert them into &lt;br/&gt; tags.&#160; For instance, we pipe the output of build and database scripts to xml.&#160; When rendering into html, we want to keep the line breaks or the text looks garbled.&#160; There are many other situations where you might want to preserve line breaks, but bare with me.</p>
<p>Below I have some text copied from an execution of msbuild.&#160; This text is stored directly into a &lt;text&gt; node in the xml:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">&lt;</span><span style="color: #800000">text</span><span style="color: #0000ff">&gt;</span>Microsoft (R) Build Engine Version 3.5.21022.8
[Microsoft .NET Framework, Version 2.0.50727.1433]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 1/2/2008 6:34:50 PM.
Done Building Project &quot;D:\builds\ININ.BCF.sln&quot; (default targets).

Build succeeded.
    0 Warning(s)
    0 Error(s)<span style="color: #0000ff">&lt;/</span><span style="color: #800000">text</span><span style="color: #0000ff">&gt;</span></pre>
</div>
<p>A simple xslt to convert this into html might look like this:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">&lt;?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&quot;1.0&quot;</span>?<span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:stylesheet</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&quot;1.0&quot;</span>
    <span style="color: #ff0000">xmlns:xsl</span><span style="color: #0000ff">=&quot;http://www.w3.org/1999/XSL/Transform&quot;</span>
    <span style="color: #ff0000">xmlns</span><span style="color: #0000ff">=&quot;http://www.w3.org/TR/xhtml1/strict&quot;</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:output</span> <span style="color: #ff0000">method</span><span style="color: #0000ff">=&quot;html&quot;</span><span style="color: #0000ff">/&gt;</span>

    <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:template</span> <span style="color: #ff0000">match</span><span style="color: #0000ff">=&quot;/&quot;</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:value-of</span> <span style="color: #ff0000">select</span><span style="color: #0000ff">=&quot;/text&quot;</span><span style="color: #0000ff">/&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsl:template</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsl:stylesheet</span><span style="color: #0000ff">&gt;</span></pre>
</div>
<p>The problem with the xslt is that the line breaks aren&#8217;t preserved and as a result, the text looks garbled:</p>
<blockquote>
<p>Microsoft (R) Build Engine Version 3.5.21022.8 [Microsoft .NET Framework, Version 2.0.50727.1433] Copyright (C) Microsoft Corporation 2007. All rights reserved. Build started 1/2/2008 6:34:50 PM. Done Building Project &quot;D:\builds\ININ.BCF.sln&quot; (default targets). Build succeeded. 0 Warning(s) 0 Error(s)</p>
</blockquote>
<p>Here is the same xslt with the inclusion of a PreserveLineBreaks template:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">&lt;?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&quot;1.0&quot;</span>?<span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:stylesheet</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">=&quot;1.0&quot;</span>
    <span style="color: #ff0000">xmlns:xsl</span><span style="color: #0000ff">=&quot;http://www.w3.org/1999/XSL/Transform&quot;</span>
    <span style="color: #ff0000">xmlns</span><span style="color: #0000ff">=&quot;http://www.w3.org/TR/xhtml1/strict&quot;</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:output</span> <span style="color: #ff0000">method</span><span style="color: #0000ff">=&quot;html&quot;</span><span style="color: #0000ff">/&gt;</span>

    <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:template</span> <span style="color: #ff0000">match</span><span style="color: #0000ff">=&quot;/&quot;</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:call-template</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;PreserveLineBreaks&quot;</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:with-param</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;text&quot;</span> <span style="color: #ff0000">select</span><span style="color: #0000ff">=&quot;/text&quot;</span><span style="color: #0000ff">/&gt;</span>
        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsl:call-template</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsl:template</span><span style="color: #0000ff">&gt;</span>

    <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:template</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;PreserveLineBreaks&quot;</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:param</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;text&quot;</span><span style="color: #0000ff">/&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:choose</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:when</span> <span style="color: #ff0000">test</span><span style="color: #0000ff">=&quot;contains($text,'&amp;#xA;')&quot;</span><span style="color: #0000ff">&gt;</span>
                <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:value-of</span> <span style="color: #ff0000">select</span><span style="color: #0000ff">=&quot;substring-before($text,'&amp;#xA;')&quot;</span><span style="color: #0000ff">/&gt;</span>
                <span style="color: #0000ff">&lt;</span><span style="color: #800000">br</span><span style="color: #0000ff">/&gt;</span>
                <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:call-template</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;PreserveLineBreaks&quot;</span><span style="color: #0000ff">&gt;</span>
                    <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:with-param</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;text&quot;</span><span style="color: #0000ff">&gt;</span>
                        <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:value-of</span> <span style="color: #ff0000">select</span><span style="color: #0000ff">=&quot;substring-after($text,'&amp;#xA;')&quot;</span><span style="color: #0000ff">/&gt;</span>
                    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsl:with-param</span><span style="color: #0000ff">&gt;</span>
                <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsl:call-template</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsl:when</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:otherwise</span><span style="color: #0000ff">&gt;</span>
                <span style="color: #0000ff">&lt;</span><span style="color: #800000">xsl:value-of</span> <span style="color: #ff0000">select</span><span style="color: #0000ff">=&quot;$text&quot;</span><span style="color: #0000ff">/&gt;</span>
            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsl:otherwise</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsl:choose</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsl:template</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;/</span><span style="color: #800000">xsl:stylesheet</span><span style="color: #0000ff">&gt;</span></pre>
</div>
<p>The template preserves line breaks by writing out the text before the line break, adding a &lt;br/&gt; tag, then recursively calling the template again to look for addition line breaks in the remaining text.&#160; This process continues until every line break is essentially replaced with a &lt;br/&gt;. </p>
<blockquote>
<p>Microsoft (R) Build Engine Version 3.5.21022.8<br />
    <br />[Microsoft .NET Framework, Version 2.0.50727.1433] </p>
<p>Copyright (C) Microsoft Corporation 2007. All rights reserved. </p>
<p>Build started 1/2/2008 6:34:50 PM. </p>
<p>Done Building Project &quot;D:\builds\ININ.BCF.sln&quot; (default targets). </p>
<p>Build succeeded. </p>
<p>0 Warning(s) </p>
<p>0 Error(s)</p>
</blockquote>
<p>This template could even be modified to replace other characters such as tabs depending on your needs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2008/01/03/preserving-line-breaks-in-xml-while-transforming-to-html-with-xslt/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>XsltTransformer: Transforming an xml string with an xslt string</title>
		<link>http://www.danrigsby.com/blog/index.php/2007/12/21/xslttransformer-transforming-an-xml-string-with-an-xslt-string/</link>
		<comments>http://www.danrigsby.com/blog/index.php/2007/12/21/xslttransformer-transforming-an-xml-string-with-an-xslt-string/#comments</comments>
		<pubDate>Fri, 21 Dec 2007 18:04:29 +0000</pubDate>
		<dc:creator>Dan Rigsby</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[xslt]]></category>

		<guid isPermaLink="false">http://www.danrigsby.com/blog/?p=32</guid>
		<description><![CDATA[
There are many times when I have xml and xslt as strings and need the transformed data as a string.&#160; I put together a utility class to take care of this.

/// &#60;summary&#62;
/// Transforms the specified xml string using the specified xslt string.
/// &#60;/summary&#62;
/// &#60;param name="xml"&#62;The xml string to transform.&#60;/param&#62;
/// &#60;param name="xslt"&#62;The xslt string to transform [...]]]></description>
			<content:encoded><![CDATA[<p class="csharpcode-wrapper">
<p class="csharpcode">There are many times when I have xml and xslt as strings and need the transformed data as a string.&nbsp; I put together a utility class to take care of this.</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #008000">/// &lt;summary&gt;</span>
<span style="color: #008000">/// Transforms the specified xml string using the specified xslt string.</span>
<span style="color: #008000">/// &lt;/summary&gt;</span>
<span style="color: #008000">/// &lt;param name="xml"&gt;The xml string to transform.&lt;/param&gt;</span>
<span style="color: #008000">/// &lt;param name="xslt"&gt;The xslt string to transform with.&lt;/param&gt;</span>
<span style="color: #008000">/// &lt;returns&gt;The transformed xml string.&lt;/returns&gt;</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> Transform(
    <span style="color: #0000ff">string</span> xml,
    <span style="color: #0000ff">string</span> xslt
    )
{
    <span style="color: #0000ff">string</span> output = String.Empty;
    StringWriter stringWriter = <span style="color: #0000ff">null</span>;
    XmlTextWriter xmlWriter = <span style="color: #0000ff">null</span>;
    XmlDocument inputDocument = <span style="color: #0000ff">new</span> XmlDocument();
    XmlDocument xsltDocument = <span style="color: #0000ff">new</span> XmlDocument();
    System.Xml.Xsl.XslCompiledTransform xslCompiledTransformer = <span style="color: #0000ff">null</span>;

    <span style="color: #0000ff">try</span>
    {
        <span style="color: #008000">// Create Xml Document</span>
        inputDocument.LoadXml(xml);

        <span style="color: #008000">// Create XsltDocument</span>
        xsltDocument.LoadXml(xslt);

        stringWriter = <span style="color: #0000ff">new</span> StringWriter();
        xmlWriter = <span style="color: #0000ff">new</span> XmlTextWriter(stringWriter);

        <span style="color: #008000">// Apply the generated text stylesheet. </span>
    <span style="color: #cc6633">#if</span> DEBUG
        xslCompiledTransformer =
            <span style="color: #0000ff">new</span> System.Xml.Xsl.XslCompiledTransform(<span style="color: #0000ff">true</span>);
    <span style="color: #cc6633">#else</span>
        xslCompiledTransformer =
            <span style="color: #0000ff">new</span> System.Xml.Xsl.XslCompiledTransform(<span style="color: #0000ff">false</span>);
    <span style="color: #cc6633">#endif</span>
        xslCompiledTransformer.Load(xsltDocument);
        xslCompiledTransformer.Transform(inputDocument, xmlWriter);

        <span style="color: #008000">// Generate the output Xml</span>
        output = stringWriter.GetStringBuilder().ToString();
    }
    <span style="color: #0000ff">finally</span>
    {
        <span style="color: #0000ff">if</span> (xmlWriter != <span style="color: #0000ff">null</span>)
        {
            xmlWriter.Close();
        }

        <span style="color: #0000ff">if</span> (stringWriter != <span style="color: #0000ff">null</span>)
        {
            stringWriter.Dispose();
        }

        <span style="color: #0000ff">if</span> (xslCompiledTransformer != <span style="color: #0000ff">null</span>)
        {
            xslCompiledTransformer.TemporaryFiles.Delete();
        }
    }

    <span style="color: #0000ff">return</span> output;
}</pre>
</div>
<p>&nbsp;</p>
<p>A couple of things of note:</p>
<ol>
<li>When I first started using this, I had this running in memory and it would transform over 100 times an hour.&nbsp; We had a variety of stylesheets, and the xml was almost always different.&nbsp; If we didn&#8217;t make a call to TemporaryFiles.Delete(), then these temporary files would be kept and overtime this resulted in a lot of useless files.&nbsp; This seems like a bug to me that this files aren&#8217;t cleaned up by default, but I am sure there is some clever reason for this.&nbsp; It may depend on how you are implementing it.&nbsp; I do like having the flexibility of deciding whether or not to keep them though.&nbsp; You can read more about these options on the <a href="http://msdn2.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform(VS.80).aspx">MSDN page for XsltCompiledTransform</a>.
<li>I had thought of making the XsltCompiledTransform a static variable, but depending on how this method is used, I didn&#8217;t want to always have this loaded in memory.&nbsp; If you make use of this code, you may decide to do this. </li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.danrigsby.com/blog/index.php/2007/12/21/xslttransformer-transforming-an-xml-string-with-an-xslt-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

