title: Problems Trying to Pretty Print XSLT Output
body: <p>this is my first post so please let me know if I can make it more constructive in any way. I have read the forum guidelines so if I inadvertantly break them in anyway it will be nothing more than an innocent mistake.</p>
  
  <h1>The Question</h1>
  
  <p>Is a simple one:</p>
  
  <p><strong>How do I pretty print the output of an XSL file?</strong></p>
  
  <p>But with some criteria:</p>
  
  <ol>
  <li><strong>Using only native XSL functionality.</strong></li>
  <li><strong>Without having to use a second XSL file to do a 'second pass'.</strong></li>
  <li><strong>It must also work for elements with mixed content.</strong></li>
  </ol>
  
  <p>I have googled this reasonably thoroughly but have not found a clear answer to this question. I have only used XSL for about a week so go easy if I have somehow missed the answer elsewhere.</p>
  
  <hr>
  
  <h2>An Example</h2>
  
  <p>This XML...</p>
  
  <pre><code>&lt;email&gt;
      &lt;attachedItem&gt;priceless photograph.jpg&lt;/attachedItem&gt;
      &lt;attachedItem&gt;important document.doc&lt;/attachedItem&gt;
      &lt;attachedItem&gt;access codes.pdf&lt;/attachedItem&gt;
  &lt;/email&gt;
  </code></pre>
  
  <p>...Transformed by this XSL...</p>
  
  <pre><code>&lt;!-- Pretty Print Output --&gt;
  &lt;xsl:strip-space elements="*"/&gt;
  &lt;xsl:output method="xml" indent="yes"/&gt;
  
  &lt;xsl:template match="/"&gt;
  &lt;email&gt;
      "Please find attached the stuff."
      &lt;xsl:apply-templates/&gt;
  &lt;/email&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="attachedItem"&gt;
      &lt;xsl:copy/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>...Produces this result...</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;email&gt;
          "Please find attached the stuff."
          &lt;attachedItem&gt;priceless photograph.jpg&lt;/attachedItem&gt;
     &lt;attachedItem&gt;important document.doc&lt;/attachedItem&gt;
     &lt;attachedItem&gt;access codes.pdf&lt;/attachedItem&gt;
  &lt;/email&gt;
  </code></pre>
  
  <p><em>Using the Saxon6.5.5 engine</em></p>
  
  <h2>Desired Output</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;email&gt;
     "Please find attached the stuff."
     &lt;attachedItem&gt;priceless photograph.jpg&lt;/attachedItem&gt;
     &lt;attachedItem&gt;important document.doc&lt;/attachedItem&gt;
     &lt;attachedItem&gt;access codes.pdf&lt;/attachedItem&gt;
  &lt;/email&gt;
  </code></pre>
  
  <hr>
  
  <h1>My Own Progress on the Problem</h1>
  
  <p>From the XSL above you will see I have discovered the use of <code>&lt;xsl:strip-space&gt;</code> and <code>&lt;xsl:output&gt;</code>. This meets the first 2 criteria but not the 3rd. In other words, it produces nice pretty printed XML without the mixed content, but with it I recieve the undesired output you can see above.</p>
  
  <p>I know that the reason I get this output is because of the way whitespace is preserved in the source XML. White space is <em>always</em> preserved if it is part of a text node that contains other non-whitespace characters, regardless of the <code>&lt;xsl:strip-space&gt;</code> instructions. However despite my understanding I still cannot think of a solution.</p>
  
  <p>Although I have addressed the first 2 criteria myself I would still like to know if this is the best way to achieve a pretty printed result.</p>
  
  <p>Thanks in advance!</p>