Mokkapps
Mokkapps

Reputation: 2028

Programmatically Saxon XSLT Transformation in Java/Android

Hi there I want to programmatically transform a xml with a xsl stylesheet in my Android application.

This is my transformer function:

//-----------------------------XLST SAXON TRANSFORMATION--------------------
public static void simpleTransform(String sourcePath, String xsltPath,
        String resultDir) {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        try {
            Transformer transformer =
                    tFactory.newTransformer(new StreamSource(new File(xsltPath)));

            transformer.transform(new StreamSource(new File(sourcePath)),
    new StreamResult(new File(resultDir)));
        } catch (Exception e) {
            e.printStackTrace();
        }
}

and witht his code I start the transformation:

           private static String sourcePath;
           private static String xsltPath = SD_CARD_PATH +"/"+ "ets4_calimero_gui.xsl";
           private static String resultDir;

           sourcePath = SD_CARD_PATH + "/"+Name+".xml"; 
           resultDir = SD_CARD_PATH + "/"+Name+"_xlst.xml";

           System.setProperty("javax.xml.transform.TransformerFactory",
                "net.sf.saxon.TransformerFactoryImpl");
           simpleTransform(sourcePath, xsltPath, resultDir);
           Log.d("XLST","XML saved as" + resultDir);

but the created .xml file is empty and logcat throws the following error:

09-27 13:44:41.940: W/System.err(7736): (Location of error unknown)java.io.IOException: Couldn't open file:///mnt/sdcard/rg.xml

but the rg.xml exists, I can see it on my external sd card. I also have added the saxon.jar as classpath and inserted it in the libs folder.

Anyone a idea why the Transformation is not working?

Upvotes: 1

Views: 1516

Answers (2)

Peter Krauss
Peter Krauss

Reputation: 13940

Android uses a lightweight XSLT parser, LibXML2, and the lightweight XSLT standard, XSLT-version-1.

For complex XSLT script (as XSLT2 specific functions) check hasExsltSupport() and use commom module of EXSLT and/or "register functions" to call Java functions from your XSLT script.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163458

You mention "saxon.jar" which makes it seem you might be running a rather old version (perhaps 6.5?). You might like to try something more recent. However, Saxon is not tested or supported on Android; we know that there are problems but we haven't done any serious work on finding out what they are and whether there might be a simple workaround (the code won't compile for Android because it has dependencies on features not present on the Android platform, and the first thing we have to do is get rid of those dependencies.)

If anyone has any experiences getting Saxon to work on Android, we'd like to hear about them.

Upvotes: 1

Related Questions