title: Writing XSLT output to the multiple objects in memory
body: <p>I'm playing with XSLT and writing the result to the multiple outputs in Java as these links describe:
  <a href="https://stackoverflow.com/questions/27351242/xsl-multiple-outputs-using-java">link1</a>, 
  <a href="https://stackoverflow.com/questions/30562483/using-transformer-in-java-for-multiple-outputs-from-an-xslt">link2</a>. </p>
  
  <p>When it comes to writing to the files everything works perfectly, but now I'd like to store the output XMLs as either Document object or some kind of InputStream/String - anything but file on a drive. <strong>Writing</strong> the documents to the <strong>files</strong> using Transformer and than <strong>reading</strong> them to the <strong>objests</strong>(Document, String etc.) <strong>is not an option</strong>.</p>
  
  <p>My question is: what should I change in my xslt map and java code to get the output as an objects, not the files on a drive. (the only solution that I came up with is to use two XSLT on this one XML but it's not the elegant way)</p>
  
  <p>My Java codes are similar to the answers in the links above.
  I'm using XSLT 2.0 and transform using :</p>
  
  <pre><code>transformer.transform(xmlText, new DOMResult());
  </code></pre>
  
  <p>I also added </p>
  
  <pre><code>  &lt;xsl:result-document href="{concat($jobName, '_assets.xml')}"&gt;
          &lt;xsl:apply-templates select="GetMetadata/Metadata" mode="assets"/&gt;
      &lt;/xsl:result-document&gt;
      &lt;xsl:result-document href="{concat($jobName, '_flows.xml')}"&gt;
          &lt;xsl:apply-templates select="GetMetadata/Metadata" mode="flows"/&gt;
      &lt;/xsl:result-document&gt;
  </code></pre>
  
  <p>to my xsl map [writing to the files works fine]</p>
  
  <p><strong>LATER</strong></p>
  
  <p><strong>Correct implementation:</strong> (thank you Michael)</p>
  
  <pre><code>public void multipleXMLExtractor(Document inputDom, String xsltPath) throws TransformerException, UnsupportedEncodingException {
  
  Source xmlInput = new DOMSource(inputDom);
  Source xsltMap = new StreamSource(new File(xsltPath));
  final Map&lt;String, StreamResult&gt; resultsMap = new HashMap&lt;String, StreamResult&gt;();
  resultsMap.put("output1", new StreamResult(new ByteArrayOutputStream()));
  resultsMap.put("output2", new StreamResult(new ByteArrayOutputStream()));
  
  TransformerFactory tFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", XMLviaXSLTransformer.class.getClassLoader());
  Transformer transformer = tFactory.newTransformer(xsltMap);
  ((net.sf.saxon.jaxp.TransformerImpl) transformer).getUnderlyingController().setOutputURIResolver(new OutputURIResolver() {
      @Override
      public OutputURIResolver newInstance() {
          return this;
      }
  
      @Override
      public Result resolve(String s, String s1) throws TransformerException {
          return resultsMap.get(s);
      }
  
      @Override
      public void close(Result result) throws TransformerException {
      }
  });
  
  StreamResult standardResult = new StreamResult(new ByteArrayOutputStream());
  transformer.transform(xmlInput, standardResult);
  
  
  for (Map.Entry&lt;String, StreamResult&gt; stringStreamResultEntry : resultsMap.entrySet()) {
      System.out.println(stringStreamResultEntry.getKey() + ": " + ((ByteArrayOutputStream) stringStreamResultEntry.getValue().getOutputStream()).toString("UTF-8"));
  }
  </code></pre>
  
  <p>}</p>