Using XSLT to turn content of WYSIWYG stored in XML into HTML
You should change your mindset when working with XSLT: look at it as a functional language, where you declare rules for the transformation of elements. Think about it in terms of events: when a p element is found in input, I expect this and that in the output.

First, regarding the definition of your data. You should ask for (or create) a more formal description of the data. There are different formats available to describe the structure (grammar), DTD and XML Schema being the more common. There are more simple alternatives such as RELAX NG and Schematron.

My guess, from your stylesheet and your descriptions:


there is always a &lt;system-index-block&gt; at the top
there is a list of &lt;story&gt; elements in &lt;system-index-block&gt;
there is mixed HTML contents in &lt;story&gt;, with &lt;p&gt;,&lt;div&gt;,&lt;br&gt;,&lt;em&gt; and text allowed


I am not sure about the last part: is it possible to have both text and div or paragraph at the top level in a story?

&lt;story&gt;
  Text before a &lt;p&gt;Paragraph&lt;/p&gt; in between or &lt;div&gt;Div&lt;/div&gt; even after ?
&lt;/story&gt;


You should break your unique template rule into several template rules, one or several for each element. Then to direct the flow from rule to rule, do not use for-each but rather apply-templates:

&lt;xsl:template match="system-index-block"&gt;
  &lt;!-- Note:
       You probably need to create the basic HTML structure Here: html, head, body
  --&gt;
  &lt;xsl:apply-templates /&gt; &lt;!-- you can specify select="story" but it is not required --&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="story[p|div]"&gt; &lt;!-- If wrapped in an element --&gt;
   &lt;xsl:apply-templates mode="copy" /&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="story"&gt; &lt;!-- wrap the content in a paragraph tag --&gt;
  &lt;p&gt;
    &lt;xsl:apply-templates mode="copy" /&gt;
  &lt;/p&gt;
&lt;/xsl:template&gt;


Note that I used the mode "copy" above: you can now create different rules for the same nodes depending on the mode, for example:

&lt;xsl:template match="text()" /&gt; &lt;!-- ignore text in normal mode, probably whitespace --&gt;

&lt;xsl:template match="text()" mode="copy"&gt;
    &lt;xsl:copy /&gt;
&lt;/xsl:template&gt;


Also, you will probably get into trouble because you only declared a namespace for the xsl prefix. You typically need a prefix/namespace declaration for the input and output formats. You will then need to use the prefix in match/select expressions and in the tag names for the output, for example, with input prefix bound to your input namespace and html bound to the html namespace:

&lt;xsl:template match="input:story"&gt; &lt;!-- wrap the content in a paragraph tag --&gt;
  &lt;html:p&gt;
    &lt;xsl:apply-templates mode="copy" /&gt;
  &lt;/html:p&gt;
&lt;/xsl:template&gt;


To go further, here are pointers that you will probably find useful:


Namespaces and XSLT Stylesheets
Identity transformation