title: XProc: multiple XSLT transformation with intermediate files
body: <p>I need to do several XSLT transformations with intermediate XML files. (I need the files, the real case is a bit more tricky as a later step loads intermediate files)</p>
  
  <pre><code>first.xml ------------&gt;   intermediate.xml ------------&gt; final.xml
            first.xsl                         final.xsl
  </code></pre>
  
  <p>I'd like to create an XProc pipleline. I have tried to write the following code, but this gives me an error:</p>
  
  <pre><code>SCHWERWIEGEND: runxslt.xpl:26:44:err:XD0011:Could not read: intermediate.xml 
  17.05.2012 15:15:35 com.xmlcalabash.drivers.Main error
  SCHWERWIEGEND: It is a dynamic error if the resource referenced by a p:document element does not exist, cannot be accessed, or is not a well-formed XML document.
  17.05.2012 15:15:35 com.xmlcalabash.drivers.Main error
  SCHWERWIEGEND: Underlying exception: net.sf.saxon.s9api.SaxonApiException: I/O error reported by XML parser processing file:/&lt;somepath&gt;/intermediate.xml:
  /&lt;somepath&gt;/intermediate.xml (No such file or directory)
  </code></pre>
  
  <p>(where SCHWERWIEGEND means something like FATAL) So obviously the file <code>intermediate.xml</code> has not been written.</p>
  
  <p>This is the <code>xpl</code>-document that I have used:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
    xmlns:c="http://www.w3.org/ns/xproc-step" version="1.0"&gt;
  
    &lt;p:input port="source"&gt;
      &lt;p:document href="first.xml"/&gt;
    &lt;/p:input&gt;
  
    &lt;p:output port="result" sequence="true"&gt;
      &lt;p:empty/&gt;
    &lt;/p:output&gt;
  
    &lt;p:xslt name="first-to-intermediate"&gt;
      &lt;p:input port="stylesheet"&gt;
        &lt;p:document href="first.xsl"/&gt;
      &lt;/p:input&gt;
      &lt;p:input port="parameters"&gt;
        &lt;p:empty/&gt;
      &lt;/p:input&gt;
    &lt;/p:xslt&gt;
  
    &lt;p:store href="intermediate.xml" /&gt;
  
    &lt;p:xslt&gt;
      &lt;p:input port="source"&gt;
        &lt;p:document href="intermediate.xml"/&gt;
      &lt;/p:input&gt;
      &lt;p:input port="stylesheet"&gt;
        &lt;p:document href="final.xsl"/&gt;
      &lt;/p:input&gt;
      &lt;p:input port="parameters"&gt;
        &lt;p:empty/&gt;
      &lt;/p:input&gt;
    &lt;/p:xslt&gt;
  
    &lt;p:store href="final.xml"/&gt;
  
  &lt;/p:declare-step&gt;
  </code></pre>
  
  <p>Just for the sake of completeness: these are the transformation files:</p>
  
  <p>source.xml:</p>
  
  <pre><code>&lt;root&gt;
    &lt;element name="A" /&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>first.xsl:</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0"&gt;
    &lt;xsl:output indent="yes"/&gt;
  
    &lt;xsl:template match="root"&gt;
      &lt;root&gt;
        &lt;xsl:apply-templates/&gt;
      &lt;/root&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match="element"&gt;
      &lt;intermediate name="A" /&gt;
    &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>final.xsl:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0"&gt;
    &lt;xsl:output indent="yes"/&gt;
  
    &lt;xsl:template match="root"&gt;
      &lt;root&gt;
        &lt;xsl:apply-templates/&gt;
      &lt;/root&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match="intermediate"&gt;
      &lt;final name="A" /&gt;
    &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Here is a note on the real application (the above is a simplification, of course).</p>
  
  <ol>
  <li>First step: convert the source into something more suitable for my processing. Output: <code>companies.xml</code></li>
  <li>Take the output from step 1 and create an index file (<code>index.xml</code>) from that. The index file must be editable manually.</li>
  <li>The third step is to merge the files created by step 1 and 2 and create a final xml (<code>final.xml</code>)</li>
  </ol>
  
  <p>The index file must be written to disk and I must be able to run the last step by itself (that's a different problem - I'd write a different pipeline for that)</p>
  
  <p>The output of <code>companies.xml</code> (step 1) is optional, it could be saved in memory (but it might get large).</p>