XSLT Transformation using flat file - java

Need some inputs on the below problem.
I have a flat file which contains Accounts numbers
Account1:Valid
Account2:Valid
Account3:Invalid
There is another system generated XML whose contents are transformed in Java via a Transformer class through an XSL file.
I need to enhance the XSL file so that it takes Accounts from flat file into consideration and based on Valid or Invalid status, generate the o/p response XML.
Any pointers on how to approach this? In Java application, I have done simple transformation. But how to enhance to take data from Flat File into consideration ?

If you want to do it with the input as shown, then you need to use XSLT 2.0 and later to use unparsed-text("accounts.txt") to read in the text file and for instance parse it with tokenize(unparsed-text("accounts.txt"), '\n') into lines and/or further with xsl:analyze-string.
XSLT 2.0 is supported in Java by Saxon 9 from http://saxon.sourceforge.net/.
With XSLT 1.0 all you could do is pass in a string parameter with the file contents and then use the rudimentary string functions in XPath 1.0 and named templates to extract the data.

Related

Java transform XSLT

I have to transform xml file according to xls file and I use javax.xml.transform.* but this approach has limitations. I can't use xsl with a size larger than more 64kB. GregorSamsa.
Is there any a library in java to transform according to xsl > 64kB?

JSON To JSON Transformation | Template Engine for Java

I have a requirement to transform an incoming JSON to an output JSON. For this I am looking for a solution that can work based on templates. What I have in my mind is a solution on lines of XSLT transformation that allows converting an XML to a desired output format (XML, HTML, Text) defined by the style sheet.
One option(or rather a workaround) to use XSLT is to convert JSON to XML that is:
input JSON -> XML -> transform -> output JSON
This approach would have a performance overhead of converting JSON to XML and this would become prominent as the size of incoming object increases.
I found a Node/client layer solution that transforms JSON based on the rules specified in a template. More details about can be found [here][1]. However, I was not able to find any solution that works for a java based application.
Any thoughts/help in terms of solution/frameworks to resolve this would be really helpful.
Thanks.
You could try JOLT, advertised as a JSON to JSON transformation library written in Java.
Or you can search this thread for other libraries and tools which can transform JSON.
The new XSLT 3.0 draft also includes support for JSON as input and output format. Saxon has already started an implementation and seems to support for the JSON part.
You could try JSLT, which is a transform language where you write the fixed part of the output in JSON syntax, then insert expressions to compute the values you want to insert in the template. It's quite similar to how XSLT and XPath work together.
It's implemented in Java on top of Jackson.
For simple transformations you can use jmom library.
For a complex transformation you can use template framework like freemarker.
And convert json data to Map/List form using json library so it can be used by template framework.

Create a copy of xml file in memory in java

I need to create a copy of an xml file in memory using java and i need to edit this file in memory without affecting the original one. After making changes to this xml in memory i need to send it as an input to a function. What is the appropriate option .Please help me.
You can use java native api for xml parsing:
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
File file = new File("xml_file_name");
Document doc = builder.parse(file);
and then edit the Document in memory before sending it to your designated function.
Do what you wrote:
Read the file.
Write it to another file.
Edit so called another file.
Pass it to the function. Here you have to decide if it's better to pass a file or a path.
What you are looking for is ByteArrayOutputStream. http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html
This will allow you to write a byte array in to memory most xml lib will accept implementations of OutputStream.
Given the file is XML you should consider using loading it into the Document Object Model (DOM): https://docs.oracle.com/javase/tutorial/jaxp/dom/readingXML.html
That will make it easier for you to modify it and write it back as valid XML document.
I would only suggest loading it as bytes/characters if you're operating on it at a byte level. An example of when that might be appropriate is if you're making some character encoding translation (say UTF-16 -> UTF-8) or removing 'illegal' characters.
Code that tries to parse and modify XML in place usually becomes dreadfully bloated if it covers all valid XML files.
Unless you're a domain expert for XML, pick the parser of the shelf. It's pretty full of good libraries.
If the files may be large and your logic ameanable I would prefer to use an XML stream model such as SAX: https://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html
However I get the impression you're not experienced and non-experts tend to struggle with the event driven parsing model of SAX.
Try DOM first time out.

Creating HTML from XML v/s JAVA object

I am trying to create HTML file from the result of an execution. The result is in the form of XML. There are few transformers that I can use to transform XML to HTML using XSLT file.
Other thing I will also have is the JAVA object of result which I can use for converting it to HTML.
Which of the above two approach is better and is there any API that I can use to convert java object to HTML other than XSLT or FILE I/O.
any one help?
I believe you have to go by xml (either directly or generated from your java object by jaxb).
In principle the templating frameworks (Velocity, Freemarker ...) can let you prepare a template into which you can inject your java object and render the response as you whish. But personally I think it will be easier/simple just to transform the xml that you already have

Converting HTML table into XML

i have an application which generates test case results in the form of HTML table.
I need to convert this html table data into JUnit XML format for some other usage.
How can I parse each entry in of a table. I have browsed over internet, people are commenting other posts to conver html to xhtml which is xml equivalent but this doesn't suffice my requirement. I want the xml to be based on Junit xsd schema
You need a two step process. Use an HTML parser to create a DOM. There's lots to choose from in Java
When you've parsed the HTML and got a DOM, you can transform it into the XML form you want, either by hand code, or by using a transformation language like XSLT. Xalan is typically the library you want for performing XSLT transforms in Java.

Categories

Resources