title: Extract text from Word-Document using XSLT
body: <p>I must extract paragraphs (means: Headlines with their content) from a Word-document using XSLT. I have analyzed the structure and can reach the necessary nodes in the .docx-file with XSLT. But now i do not know how to group the content of the <code>w:t</code>-tags between the headings because Word splits the texts in a very strange way.</p>
  
  <p>The input-data looks like:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;w:body xmlns:w="somenamespace"&gt;
     &lt;w:p&gt;
        &lt;w:pPr&gt; &lt;w:pStyle w:val="Heading1" /&gt; &lt;/w:pPr&gt;
        &lt;w:r&gt; &lt;w:t&gt;My Headl&lt;/w:t&gt; &lt;/w:r&gt;
        &lt;w:r&gt; &lt;w:t&gt;ine&lt;/w:t&gt; &lt;/w:r&gt;
     &lt;/w:p&gt;
     &lt;w:p&gt;
        &lt;w:r&gt; &lt;w:t&gt;text 1.1.1 &lt;/w:t&gt; &lt;/w:r&gt;
        &lt;w:r&gt; &lt;w:t&gt;text 1.1.2 &lt;/w:t&gt; &lt;/w:r&gt;
     &lt;/w:p&gt;
     &lt;w:p&gt;
        &lt;w:r&gt; &lt;w:t&gt;text 1.2.1 &lt;/w:t&gt; &lt;/w:r&gt;
        &lt;w:r&gt; &lt;w:t&gt;text 1.2.2 &lt;/w:t&gt; &lt;/w:r&gt;
     &lt;/w:p&gt;
     &lt;w:p&gt;
        &lt;w:pPr&gt; &lt;w:pStyle w:val="Heading1" /&gt; &lt;/w:pPr&gt;
        &lt;w:r&gt; &lt;w:t&gt;My seco&lt;/w:t&gt; &lt;/w:r&gt;
        &lt;w:r&gt; &lt;w:t&gt;nd Headline&lt;/w:t&gt; &lt;/w:r&gt;
     &lt;/w:p&gt;
     &lt;w:p&gt;
        &lt;w:r&gt; &lt;w:t&gt;text 2.1.1 &lt;/w:t&gt; &lt;/w:r&gt;
        &lt;w:r&gt; &lt;w:t&gt;text 2.1.2 &lt;/w:t&gt; &lt;/w:r&gt;
     &lt;/w:p&gt;
     &lt;w:p&gt;
        &lt;w:r&gt; &lt;w:t&gt;text 2.2.1 &lt;/w:t&gt; &lt;/w:r&gt;
        &lt;w:r&gt; &lt;w:t&gt;text 2.2.2 &lt;/w:t&gt; &lt;/w:r&gt;
     &lt;/w:p&gt;
  &lt;/w:body&gt;
  </code></pre>
  
  <p>Concatenating the content of a single paragraph is no problem. So it is simple to merge the data to a compact structure like the following:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Document&gt;
      &lt;Paragraphs&gt;
          &lt;Headline&gt;My Headline&lt;/Headline&gt;
          &lt;Content&gt;text 1.1.1 text 1.1.2 &lt;/Content&gt;
          &lt;Content&gt;text 1.2.1 text 1.2.2 &lt;/Content&gt;
          &lt;Headline&gt;My second Headline&lt;/Headline&gt;
          &lt;Content&gt;text 2.1.1 text 2.1.2 &lt;/Content&gt;
          &lt;Content&gt;text 2.2.1 text 2.2.2 &lt;/Content&gt;
      &lt;/Paragraphs&gt;
  &lt;/Document&gt;
  </code></pre>
  
  <p>But this structure is not always useful because it still does not have one xml-element for the content of one paragraph.
  So does anyone know how to merge all paragraphs <strong>between</strong> the <code>w:p</code>-elements which does represent a headline?
  I would like to have an XSLT which transforms the <code>w:body</code>-content to a structure like:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Document&gt;
      &lt;Paragraph&gt;
          &lt;Headline&gt;My Headline&lt;/Headline&gt;
          &lt;Content&gt;text 1.1.1 text 1.1.2 text 1.2.1 text 1.2.2 &lt;/Content&gt;
      &lt;/Paragraph&gt;
      &lt;Paragraph&gt;
          &lt;Headline&gt;My second Headline&lt;/Headline&gt;
          &lt;Content&gt;text 2.1.1 text 2.1.2 text 2.2.1 text 2.2.2 &lt;/Content&gt;
      &lt;/Paragraph&gt;
  &lt;/Document&gt;
  </code></pre>
  
  <p>What i have found yet:</p>
  
  <ul>
  <li><p>If a <code>w:p</code>-element contains a <code>w:pPr</code>-element then it is always the first child-node of this <code>w:p</code>-element</p></li>
  <li><p>If a <code>w:p</code>-element matches on this condition <code>./w:pPr/w:pStyle[@w:val='Heading1']&gt;</code> then all <code>w:r</code>-elements in this <code>w:p</code>-element belongs to the headline of the paragraph.</p></li>
  </ul>