Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT processing with Java?

Tags:

java

xslt

How to transform XML with XSLT processor in Java using the JDK?

like image 953
Venkat Avatar asked Jan 05 '11 13:01

Venkat


People also ask

What is XSLT in Java?

Using the XSLT Processor for Java: Overview. The Oracle XDK XSLT processor is a software program that transforms an XML document into another text-based format. For example, the processor can transform XML into XML, HTML, XHTML, or text.

Does anyone use XSLT anymore?

XSLT is very widely used. As far as we can judge from metrics like the number of StackOverflow questions, it is in the top 30 programming languages, which probably makes it the top data-model-specific programming language after SQL. But XSLT isn't widely used client-side, that is, in the browser.

How is XSLT processed?

The XSLT processor operates on two inputs: the XML document to transform, and the XSLT stylesheet that is used to apply transformations on the XML. Each of these two can actually be multiple inputs. One stylesheet can be used to transform multiple XML inputs. Multiple stylesheets can be mapped to a single XML input.


1 Answers

Here is sample for using java api for transformer, as @Raedwald said:

import javax.xml.transform.*; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.File; import java.io.IOException; import java.net.URISyntaxException;  public class TestMain {     public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {         TransformerFactory factory = TransformerFactory.newInstance();         Source xslt = new StreamSource(new File("transform.xslt"));         Transformer transformer = factory.newTransformer(xslt);          Source text = new StreamSource(new File("input.xml"));         transformer.transform(text, new StreamResult(new File("output.xml")));     } } 

The input can also be from a string or DOMSource, the output can be to a DOMSource etc.

like image 84
Askar Kalykov Avatar answered Oct 13 '22 06:10

Askar Kalykov