aspose file tools*
The moose likes XML and Related Technologies and the fly likes Java and XSLT 2.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Java and XSLT 2.0 " Watch "Java and XSLT 2.0 " New topic
Author

Java and XSLT 2.0

Asha Pande
Greenhorn

Joined: Sep 14, 2012
Posts: 1
I have the following transformer which takes 2 inputs . One is a XML and other one is a XSL. The output needs to be few splitted and transformed XML files.

private static String transformXML(String xmlFile, String xslFile) {
String formattedOutput = "";
try {

TransformerFactory tFactory = TransformerFactory.newInstance();
javax.xml.transform.Transformer transformer = tFactory
.newTransformer(new StreamSource(
new FileInputStream(xslFile)));

InputStream is = new FileInputStream(xmlFile);
String strContent;

BufferedReader bReader = new BufferedReader(new InputStreamReader(
is));
StringBuffer sbfFileContents = new StringBuffer();
String line = null;

while ((line = bReader.readLine()) != null) {
sbfFileContents.append(line);
}

strContent = sbfFileContents.toString();

StreamSource xmlSource = new StreamSource(new StringReader(
strContent));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(xmlSource, new StreamResult(baos));
formattedOutput = baos.toString();
} catch (Exception e) {
e.printStackTrace();
}
return formattedOutput;
}

To accompolish this I need to use XSLT2.0.
I have OrangeVolt com.orangevolt.eclipse.xslt.feature_1.0.7.zip file.
Now how can I write my own transformer using java code that will use 2.0 processor ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Well, there's a whole bunch of extra work being done there. Reading the input document into memory, writing the result to a byte array and then converting it to a String... none of that is necessary. Just give the FileInputStream to the transformer, and send the result to a StringWriter.

But that's irrelevant to your question. Which is, you want the transformer to use XSLT 2.0, correct? Then what you do is to find an XSLT 2.0 product and download it. Follow its instructions on how to get your Java code to use it to do transformations. That's all.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I see that since I posted my reply, you edited your post to mention that you had the OrangeVolt product. But as far as I can see, it isn't an XSLT 2.0 transformer product. In fact to me it looks more like an Eclipse plugin, but at any rate nothing about it mentions XSLT 2.0 at all.

So what I said in my original reply still stands.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

And by the way, welcome to the Ranch!
Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
oragevolt uses XSLT2.0, not sure though .
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 512
    
    3
[0]
I have OrangeVolt com.orangevolt.eclipse.xslt.feature_1.0.7.zip file.
Now how can I write my own transformer using java code that will use 2.0 processor ?

Orangevolt is an Eclipse plugin. The actual xslt engine is plugable just like the javax.TransformerFactory itself. Since Orangevolt bundles Saxon 8.9, and saxon support xslt 2.0: it is in this sense that you can use xslt 2.0 with Orangevolt plugin to perform xslt.

[1] To select a xslt 2 transformer, say saxon, you can do it like this. Instead of only this line...
TransformerFactory tFactory = TransformerFactory.newInstance();

prepare it by setting a property on System and then bring out the factory...

[1.1] Starting in fact from java 1.6 you can do it through the overload of .newInstance() as well - but that is a detail.

[2] Now what specific factors to link up with Orangevolt? If you install properly the plugin, you will be able to use what being installed and set the classpath to it. Those saxon-related jars will be located in the directory "plugins\com.orangevolt.eclipse.xslt_1.0.7" under Eclipse installation directory. You add those related jars to the classpath for the project and it would be fine.

[2.1] As a side note, to discover all the detail for the classpath pertinent to the plugin, you can run a Orangevolt project and do a simple transformation but with the processor chosen to be Saxon rather than anything else in the Orangevolt "properties" page. From the Console panel, it will show you the classpath the IDE do for the user.

[2.2] In the project documentation, there is a page to show you have to use "your own" xslt engine, just in case. It is in 4.5.1.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java and XSLT 2.0