title: How to split xml to header and items using smooks?
body: <p>I have a xml file roughly like this:</p>
  
  <pre><code>&lt;batch&gt;
      &lt;header&gt;
          &lt;headerStuff /&gt;
      &lt;/header&gt;
      &lt;contents&gt;
          &lt;timestamp /&gt;
          &lt;invoices&gt;
              &lt;invoice&gt;
                  &lt;invoiceStuff /&gt;
              &lt;/invoice&gt;
              &lt;!-- Insert 1000 invoice elements here --&gt;
          &lt;/invoices&gt;
      &lt;/contents&gt;
  &lt;/batch&gt;
  </code></pre>
  
  <p>I would like to split that file to 1000 files with the same headerStuff and only one invoice. Smooks documentation is very proud of the possibilities of transformations, but unfortunately I don't want to do those. </p>
  
  <p>The only way I've figured how to do this is to repeat the whole structure in freemarker. But that feels like repeating the structure unnecessarily. The header has like 30 different tags so there would be lots of work involved also.</p>
  
  <p>What I currently have is this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"    
      xmlns:calc="http://www.milyn.org/xsd/smooks/calc-1.1.xsd"
      xmlns:frag="http://www.milyn.org/xsd/smooks/fragment-routing-1.2.xsd"
      xmlns:file="http://www.milyn.org/xsd/smooks/file-routing-1.1.xsd"&gt;
  
      &lt;params&gt;
          &lt;param name="stream.filter.type"&gt;SAX&lt;/param&gt;
      &lt;/params&gt;
  
      &lt;frag:serialize fragment="INVOICE" bindTo="invoiceBean" /&gt;
  
      &lt;calc:counter countOnElement="INVOICE" beanId="split_calc" start="1" /&gt;
  
      &lt;file:outputStream openOnElement="INVOICE" resourceName="invoiceSplitStream"&gt;
          &lt;file:fileNamePattern&gt;invoice-${split_calc}.xml&lt;/file:fileNamePattern&gt;
          &lt;file:destinationDirectoryPattern&gt;target/invoices&lt;/file:destinationDirectoryPattern&gt;
          &lt;file:highWaterMark mark="10"/&gt;
      &lt;/file:outputStream&gt;
  
      &lt;resource-config selector="INVOICE"&gt;
          &lt;resource&gt;org.milyn.routing.io.OutputStreamRouter&lt;/resource&gt;
          &lt;param name="beanId"&gt;invoiceBean&lt;/param&gt;
          &lt;param name="resourceName"&gt;invoiceSplitStream&lt;/param&gt;
          &lt;param name="visitAfter"&gt;true&lt;/param&gt;
      &lt;/resource-config&gt;
  
  &lt;/smooks-resource-list&gt;
  </code></pre>
  
  <p>That creates files for each invoice tag, but I don't know how to continue from there to get the header also in the file.</p>
  
  <p>EDIT:</p>
  
  <p>The solution has to use Smooks. We use it in an application as a generic splitter and just create different smooks configuration files for different types of input files.</p>