title: split XML in several sites using XSLT 2.0
body: <p>I know this have been asked so often and I checked all the posts but still I dont get it. I have the following situation: I am working on a letter edition that consists of 30 xml files, each letter in one xml file. Each of the xml file includes a .png of the original letter (pic / site). I wrote a XSLT Stylesheet to convert all the XML into html.  In the end my html looks like that: on the left site are the pics, on the right site is the transcription, the text.
  every letter has different amount of sites. I now want to split every letter per page, so that every new page is in a seperate html file (including the single pic).</p>
  
  <p>so my XML looks like ( I am working with TEI):</p>
  
  <pre><code>&lt;TEIHeader&gt;...&lt;/TEIHeader&gt; 
  &lt;text&gt;
  &lt;body&gt;
  
    &lt;div type="letter"&gt;
      &lt;pb n="1" type="front"/&gt;
      &lt;div&gt; here is the text of my first site of the letter&lt;/div&gt;
  
  
  &lt;pb n="2" type="back"/&gt;
  &lt;div&gt; here is the text of my second site of the letter &lt;/div&gt;
  </code></pre>
  
  <p>and so on...</p>
  
  <p>p.e. the html of my 2nd page should look like that:</p>
  
  <pre><code>    &lt;img src="01.januar.1910.2.png" alt="brief 1.2" width="100%" /&gt;
       &lt;div class="fright margin-left"&gt;
  
             &lt;p class="seitenzahl"&gt;-2-&lt;/p&gt;
            text of my 2nd site of the letter
             &lt;/p&gt;
  </code></pre>
  
  <p>I thought about using</p>
  
  <pre><code>&lt;xsl:template match="node()[following::tei:pb[@n = '2']]"&gt;
  </code></pre>
  
  <p>to get the 2nd site of the xml (just for the text) using</p>
  
  <pre><code>&lt;xsl:result-document&gt;
  </code></pre>
  
  <p>I either managed to overwrite my existing file (p.e. 04.html) or I got the message "cannot write more than one result document to the same uri" (I work with Oxygen, Saxon-PE 9.4.0.6)</p>
  
  <p>For the pictures my XML looks like that:</p>
  
  <pre><code>&lt;facsimile&gt;
  &lt;graphic url="01.januar.1910.1.png" n="1"/&gt;
  &lt;graphic url="01.januar.1910.2.png" n="2"/&gt;
  &lt;/facsimile&gt;
  </code></pre>
  
  <p>So there are different problems:</p>
  
  <ul>
  <li>The different letters have different number of pages</li>
  <li>For every page I want to creat a new html file (with a new name)
  The file names are always Numberoftheletter_ML.xml (p.e. 01_ML.xml /01_ML.html, 02_ML.xml and so on) and I want for every new page 01_ML_1.html, 01_ML_2.html, etc.</li>
  </ul>
  
  <p>I tried to do the following with result-document, but it overwrites my original letter (01_ML.html):</p>
  
  <pre><code> &lt;xsl:result-document href="{$filename}.html"  method="html" &gt;
  </code></pre>
  
  <p>It would be great if someone can help me. If you need more code I can update (its just so much code till now)</p>
  
  <p>UPDATE: 
  This is my XSLT for creating new doc.</p>
  
  <pre><code> &lt;xsl:template match="node()[following::tei:pb[@n = '2']]"&gt;
      &lt;xsl:variable name="filename" select="generate-id()"&gt;&lt;/xsl:variable&gt;
  
      &lt;xsl:result-document href="///D:/directory/{$filename}2_ML.html"  method="html" &gt;
  
          &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
              &lt;head&gt;
                  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
                  &lt;link rel="stylesheet" type="text/css" href="mycss.5.css" /&gt;
  
  
                  &lt;title&gt;Letters (1910 - 1914)&lt;/title&gt;
              &lt;/head&gt;
              &lt;body&gt;
                  ...
  </code></pre>