How to apply xsl to xml in Java

This was asked several times, but I really did not find what I was looking for exactly. Usually I do not code in Java, but in C #, so I donโ€™t like Java classes, etc.

Question

I need to create a method that takes 2 parameters. 1. String parameter (xml - therefore it is necessary to convert to some xml class) 2. String parameter with the location of the xsl file path

The thing is, I am making a factory class that needs to convert xml from webservice to xml, which my system can understand. I need a good solution for this. Each ws method will have an xsl file - both a request (convert my xml to what ws understands) and a response (convert to what my system understands).

+7
source share
3 answers

You may find Java Almanac a useful resource.

specifically the Quintessential Program, which converts an XML file with XSL . Sample copied from page (as it continues to fade)

import java.io.*; import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; public class BasicXsl { // This method applies the xslFilename to inFilename and writes // the output to outFilename. public static void xsl(String inFilename, String outFilename, String xslFilename) { try { // Create transformer factory TransformerFactory factory = TransformerFactory.newInstance(); // Use the factory to create a template containing the xsl file Templates template = factory.newTemplates(new StreamSource( new FileInputStream(xslFilename))); // Use the template to create a transformer Transformer xformer = template.newTransformer(); // Prepare the input and output files Source source = new StreamSource(new FileInputStream(inFilename)); Result result = new StreamResult(new FileOutputStream(outFilename)); // Apply the xsl file to the source file and write the result // to the output file xformer.transform(source, result); } catch (FileNotFoundException e) { } catch (TransformerConfigurationException e) { // An error occurred in the XSL file } catch (TransformerException e) { // An error occurred while applying the XSL file // Get location of error in input file SourceLocator locator = e.getLocator(); int col = locator.getColumnNumber(); int line = locator.getLineNumber(); String publicId = locator.getPublicId(); String systemId = locator.getSystemId(); } } } 

Input Example:

 <?xml version="1.0" encoding="UTF-8"?> <map> <entry key="key1" value="value1" /> <entry key="key2" /> </map> 

Example XSLT program:

 <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" indent="yes"/> <xsl:template match="map"> <HTML> <HEAD> <TITLE>Map</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <xsl:template match="entry"> <xsl:value-of select="@key"/>=<xsl:value-of select="@value"/> <br></br> </xsl:template> </xsl:stylesheet> 

As a result of the HTML from the example:

 <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE>Map</TITLE> </HEAD> <BODY> key1=value1<br> key2=<br> </BODY> </HTML> 
+15
source

Google for the "JAXP Tutorial" - there are many resources available.

Although if you want to use XSLT 2.0 (and believe me, you do it!), You will use Saxon, and Saxon offers both JAXP and its own API (called s9api), which will help you take advantage of all the new features in XSLT 2.0.

+1
source

Take a look at http://www.rgagnon.com/javadetails/java-0407.html , hoping to answer your question

0
source

All Articles