Invoking XSLT with Java
Added by Anonymous over 19 years ago
Legacy ID: #3164576 Legacy Poster: hitman2005 (hitman2005)
Hi, I've got some problems with the translation of the following command line in a class in Java, I mean the transformation happen but no produce results and there's no exception risen. I attach here the command line and my method I create in Java, can you help me? Line: java -mx512M -DentityExpansionLimit=128000 -jar lib/saxon.jar repository/$day/access-sorted.xml results/$QUERY_NAME/query.xsl > results/$QUERY_NAME/$day-sessions.xml~ Java: (Using xsl and xml file I decided to implement BufferedInputStream,is an error?) public void ProduceQuery(String xmlti, String xslt, String xmltf) throws Exception { try{ File xml = new File(xmlti); File xsl = new File(xslt); File fine = new File(xmltf); TransformerFactory tfactory = TransformerFactory.newInstance(); InputStream xslIS = new BufferedInputStream(new FileInputStream(xsl)); StreamSource xslSource = new StreamSource(xslIS); // Create a transformer for the stylesheet. Transformer transformer = tfactory.newTransformer(xslSource); InputStream xmlIS = new BufferedInputStream(new FileInputStream(xml)); StreamSource xmlSource = new StreamSource(xmlIS); StreamResult result = new StreamResult(fine); // Transform the source XML to System.out. transformer.transform(xmlSource, result); } catch (Exception e) { e.printStackTrace(); } }
Replies (7)
Please register to reply
RE: Invoking XSLT with Java - Added by Anonymous over 19 years ago
Legacy ID: #3164637 Legacy Poster: Michael Kay (mhkay)
I can't see anything obviously wrong with your code. You're not setting systemId() on the Source object, which will cause problems with resolving relative URIs, but that shouldn't cause the result to be empty. Does the transformation produce results if you change the result object to be new StreamResult(System.out)?
RE: Invoking XSLT with Java - Added by Anonymous over 19 years ago
Legacy ID: #3164663 Legacy Poster: hitman2005 (hitman2005)
It is a problem to answer to your question because the program must invoke a lot of transformation and each transformation use the file created in the previous transformation. Can I send you via email the program so to understand very well the problem? Thank you
RE: Invoking XSLT with Java - Added by Anonymous over 19 years ago
Legacy ID: #3164722 Legacy Poster: Michael Kay (mhkay)
I'm sorry, I can try and answer your questions but I can't write or debug your code for you. Michael Kay
RE: Invoking XSLT with Java - Added by Anonymous over 19 years ago
Legacy ID: #3164736 Legacy Poster: hitman2005 (hitman2005)
No, I didn't want it, I only wanted to give you a global view on my problem to reply on my question if you could. I didn't understand what you have said about the sourceID(): can you give me more details? Have I to specify the use of saxon.jar as a system property or it is defined by default? Thank you
RE: Invoking XSLT with Java - Added by Anonymous over 19 years ago
Legacy ID: #3164756 Legacy Poster: Michael Kay (mhkay)
If you don't call setSystemId() on the Source object, then the system won't know the base URI of the source document and won't be able to resolve any relative URIs contained in it. My usual approach if I know the file name is to do new StreamSource((new File(fileName).toURI().toString()) There's no point creating the BufferedInputStream yourself, you can leave this to the system. I find the above works better than new StreamSource(new File(filename)) because the file-to-URI conversion in the File.toURI method is better than the one used by the InputSource class. If saxon.jar is on your class path then you will normally get Saxon when you do TransformerFactory.newInstance(). If you want to make doubly sure, set the system property javax.xml.transform.TransformerFactory to "net.sf.saxon.TransformerFactoryImpl" - or just do new net.sf.saxon.TransformerFactoryImpl() directly. Michael Kay
RE: Invoking XSLT with Java - Added by Anonymous over 19 years ago
Legacy ID: #3165204 Legacy Poster: hitman2005 (hitman2005)
I work with jdk1.4 and java_home references to the folder in which jdk1.4 can be found. Saxon.jar: I put it in my project folder.. Should I replace sax.jar in folder JRE with saxon.jar? Should I move saxon.jar in another folder? Thank you
RE: Invoking XSLT with Java - Added by Anonymous over 19 years ago
Legacy ID: #3166746 Legacy Poster: Michael Kay (mhkay)
You don't need sax.jar - since JDK 1.3 the SAX classes have been part of the JDK. The saxon8.jar or saxon.jar file can be in any directory you like so long as it's on your classpath. Michael Kay
Please register to reply