Question

I have an xslt 2.0 file which is being used to transform a csv file to an xml file. The xsl has been taken from here: http://p2p.wrox.com/xslt/40898-transform-csv-file-xml.html#post164344

Now I am trying to execute this through Java transformer (using the Saxon9 xsl transformer factory). Since the csv file is being passed into the xsl as a parameter, there is no need for me to pass anything in the Source parameter in the transform method. Since the javadocs for the transform method state the following: The javadocs for the Transformer.transform method clearly state that the following:

"An empty Source is represented as an empty document as constructed by DocumentBuilder.newDocument(). The result of transforming an empty Source depends on the transformation behavior; it is not always an empty Result." I tried to create an empty document and try the transformation as seen below:

TransformerFactory transformerFactory =  TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",null);
    Source xsltSource = new StreamSource("file:///C:/my.xsl");
    Transformer xsltTransformer = transformerFactory.newTransformer(xsltSource);
xsltTransformer.setParameter("pathToCSV", "'file:///C:/input.csv'");
StringWriter writer = new StringWriter();
xsltTransformer.transform(new DOMSource(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()), new StreamResult(writer));

The above piece of code does not output anything and does not work as expected since I think the empty document given as input is taken into consideration rather than the csv file passed in the following line in the xsl:

<xsl:param name="pathToCSV"  />
<xsl:variable name="input" select="unparsed-text($pathToCSV)"/>

Could anyone give me pointers on how to accomplish what I am trying to achieve?

Was it helpful?

Solution

Consider to use the Saxon API http://saxonica.com/documentation/html/using-xsl/embedding/s9api-transformation.html and not to use the JAXP API if you want to use XSLT 2.0 features like starting with a named template as the XSLT you linked to requires. Or, if you want to use JAXP with an empty dummy document you at least need to add a template doing

<xsl:template match="/">
  <xsl:call-template name="main"/>
</xsl:template>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top