How to transform XML with XSLT processor in Java using the JDK?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With