title: Saxon in Java: XSLT for CSV to XML
body: <p>Mostly continued from this question: <a href="https://stackoverflow.com/questions/10655762/xslt-csv-or-flat-file-or-plain-text-to-xml">XSLT: CSV (or Flat File, or Plain Text) to XML</a></p>
  
  <p>So, I have an XSLT from here: <a href="http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html" rel="nofollow noreferrer">http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html</a></p>
  
  <p>And it converts a CSV file to an XML document. It does this when used with the following command on the command line:</p>
  
  <blockquote>
    <p>java -jar saxon9he.jar -xsl:csv-to-xml.csv -it:main -o:output.xml</p>
  </blockquote>
  
  <p>So now the question becomes: <strong>How do I do I do this in my Java code?</strong></p>
  
  <p>Right now I have code that looks like this:</p>
  
  <pre><code>TransformerFactory transformerFactory = TransformerFactory.newInstance();
  StreamSource xsltSource = new StreamSource(new File("location/of/csv-to-xml.xsl"));
  Transformer transformer = transformerFactory.newTransformer(xsltSource);
  StringWriter stringWriter = new StringWriter();
  transformer.transform(documentSource, new StreamResult(stringWriter));
  String transformedDocument = stringWriter.toString().trim();
  </code></pre>
  
  <p>(The <code>Transformer</code> is an instance of <code>net.sf.saxon.Controller</code>.)</p>
  
  <p>The trick on the command line is to specify "-it:main" to point right at the named template in the XSLT. This means you don't have to provide the source file with the "-s" flag.</p>
  
  <p>The problem starts again on the Java side. Where/how would I specify this "-it:main"? Wouldn't doing so break other XSLT's that don't need that specified? Would I have to name every template in every XSLT file "main?" Given the method signature of Transformer.transform(), I <em>have</em> to specify the source file, so doesn't that defeat all the progress I've made in figuring this thing out?</p>
  
  <p><strong>Edit:</strong> I found the s9api hidden inside the saxon9he.jar, if anyone is looking for it.</p>