convert xml file into csv format sing camel? - java

i want to create .xml file into csv file using camel. here is my code
CamelContext context = new DefaultCamelContext();
from("file://Input?fileName=test.xml").marshal().csv().to("file://test?fileName=test.csv");
context.start();
But its't creating any file in desired folder "test".

Please spend just a bit more time on the Camel docs, and try out the examples, and read the FAQ. And the introduction articles and whatnot.
The code above isn't even valid, as you would need to put it inside a RouteBuilder.
Also when you start CamelContext, read the javadoc of the start method. And read this FAQ
http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html
Also Camel offers a tracer so you can see the message flow as the messages are being processed. The tracer will be default log this at INFO level to the logger.
http://camel.apache.org/tracer

Here is a sample using camel
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file:src/xmldata?noop=true"/>
<to uri="xslt:file:src/main/fruits.xslt"/>
<to uri="file://TESTOUT?fileName=output.csv"/>
</route>
</camelContext>
sample xml file in src/xmldata folder
<AllFruits xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- All fruits below. -->
<Fruit>
<FruitId>Bannana</FruitId>
<FruitColor>Yellow</FruitColor>
<FruitShape>Moon</FruitShape>
<Customer>
<Name>Joe</Name>
<NumberEaten>5</NumberEaten>
<Weight>2.6</Weight>
</Customer>
<Customer>
<Name>Mark</Name>
<NumberEaten>8</NumberEaten>
<Weight>5.0</Weight>
</Customer>
</Fruit>
</AllFruits>
src/main/fruits.xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text" encoding="ISO-8859-1" />
<xsl:variable name="newline" select="'
'"/>
<xsl:template match="Fruit">
<xsl:for-each select="Customer">
<xsl:value-of select="preceding-sibling::FruitId" />
<xsl:text>,</xsl:text>
<xsl:value-of select="NumberEaten" />
<xsl:text>,</xsl:text>
<xsl:value-of select="Weight" />
<xsl:value-of select="$newline" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Related

XSLT 3.0 Identity transform document collection?

I have one XSLT 3.0:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:n1="urn:hl7-org:v3">
<xsl:output indent="yes" method="xml" encoding="utf-8"/>
<xsl:param name="icd10Map" as="map(xs:string, xs:string)"
select="
map {
'1742': 'C502',
'55090': 'K409',
'8442': 'S8350',
'7172': 'M2332',
'36616': 'H251',
'4550': 'K648'
}"/>
<xsl:variable name="map-keys" select="map:keys($icd10Map)"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="n1:translation[#codeSystemName = 'ICD-9-CM']/#code">
<xsl:attribute name="code">
<xsl:value-of select="$icd10Map($map-keys[translate(normalize-space(current()), '
.;', '') = .])"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
One input XML:
<?xml-stylesheet type="text/xsl" href="./Content/xsl/CDA.xsl"?>
<ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 NIST_C32_schema/C32_CDA.xsd" xmlns="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc">
<realmCode code="US" />
<typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040" />
<templateId root="2.16.840.1.113883.10.20.22.1.1" />
************************************************************
<id extension="TT988" root="2.16.840.1.113883.19.5.99999.1" />
<code codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" code="11504-8" displayName="Surgical Operation Note" />
<title>Operative Report</title>
****************************************************
<component>
<structuredBody>
<component>
<section>
***********************
<entry>
<act moodCode="EVN" classCode="ACT">
<templateId root="2.16.840.1.113883.10.20.22.4.65" />
<code code="10219-4" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="Preoperative Diagnosis" />
<entryRelationship typeCode="SUBJ">
<observation classCode="OBS" moodCode="EVN">
<code code="282291009" displayName="Diagnosis" codeSystem="2.16.840.1.113883.6.96" codeSystemName="SNOMED CT" />
<statusCode code="completed" />
<!-- ICD-9 be transformed to ICD-10 -->
<value nullFlavor="OTH" type="CD">
<translation code="366.16" displayName="Nuclear sclerosis" codeSystem="2.16.840.1.113883.6.103" codeSystemName="ICD-9-CM" />
</value>
</observation>
</entryRelationship>
</act>
</entry>
</section>
</component>
</structuredBody>
</component>
</ClinicalDocument>
The transformation scenario in Oxygen with just one document is without issue:
*******************
<value nullFlavor="OTH" type="CD">
<translation code="H251"
displayName="Nuclear sclerosis"
codeSystem="2.16.840.1.113883.6.103"
codeSystemName="ICD-9-CM"/>
</value>
******************
However, XSLT 3.0 identity transform for collection seems working like this:
<xsl:variable name="inFile" as="node()*" select="collection('hl7.xml')"/>
<xsl:template match="/">
<xsl:text>
ICD9 Target Transformation in the collection is:
</xsl:text>
<xsl:for-each select="$inFile//n1:translation[#codeSystemName = 'ICD-9-CM']/#code">
<xsl:value-of select="$icd10Map($map-keys[translate(normalize-space(current()), '
.;', '') = .])" separator=" , "/>
</xsl:for-each>
</xsl:template>
Result:
ICD9 Target Transformation in the collection is:
H251
H251
K648
K648
K409
K409
S8350
M2332
M2332
S8350
If I change the XSLT to:
<xsl:mode on-no-match="shallow-copy"/>
<xsl:variable name="inFile" as="node()*" select="collection('hl7.xml')"/>
<xsl:template match="n1:translation[#codeSystemName = 'ICD-9-CM']/#code">
<xsl:text>
ICD9 Target Transformation in the collection is:
</xsl:text>
<xsl:for-each select="$inFile">
<xsl:attribute name="code">
<xsl:value-of select="$icd10Map($map-keys[translate(normalize-space(current()), '
.;', '') = .])" separator=" , "/>
</xsl:attribute>
</xsl:for-each>
</xsl:template>
It doesn’t appear any transform happen and is merely an extraction of the URI list in the catalog file hl7.xml.
I develop a Java application which can bulk validate documents against XSD, transform (without collection()) and finally write the documents into database. The logging is the desired result:
Engine Instantiation: com.fc.andante.sax.SAXValidateStreamTransformWrite
Schema Validation Status: files in:/ml/Andante/data/data are validated against schema file:/ml/Andante/data/operation-transform.xsd
User 'auditor' has validated files in:/ml/Andante/data/data on 2020-08-26T23:05:26.357431
*****************
Transaction Status: Authenticating database writer...
Transaction Status: User audited as 'super' is transforming document set...
Transaction Status: Document data/data/cataract.xml is successfully transformed and written into database with uri '/xslt-transform/cataract.xml'
Transaction Status: Document data/data/breast-surgery.xml is successfully transformed and written into database with uri '/xslt-transform/breast-surgery.xml'
Transaction Status: Document data/data/hernia.xml is successfully transformed and written into database with uri '/xslt-transform/hernia.xml'
Transaction Status: Document data/data/colonoscopy.xml is successfully transformed and written into database with uri '/xslt-transform/colonoscopy.xml'
Transaction Status: Document data/data/knee.xml is successfully transformed and written into database with uri '/xslt-transform/knee.xml'
Die Transaktion wurde erfolgreich abgeschlossen 2020-08-26T23:05:28.341385700
Can anyone help to solve the XSLT 3.0 Collections transform issue?
I would use a global parameter
<xsl:param name="inFiles" as="document-node()*" select="collection('hl7.xml')"/>
and then start processing with a named template
<xsl:template name="xsl:initial-template">
<xsl:for-each select="$inFiles">
<xsl:result-document href="/xslt-transform/{tokenize(document-uri(), '/')[last()]}">
<xsl:apply-templates/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
Then you can xsl:import your first XSLT sample from the question or of course edit it to insert the code I have shown. Make sure you let Saxon start with the named template (-it command line option for Saxon; in oXygen by not providing a source document).

How can I join 2 XML in 1 XSL

I have two .xml files which need to be joined
The first file is Song.xml as the following:
<Songs>
<Song>
<SongID>1</SongID>
<SongName>We dont talk anymore</SongName>
<Author>M-TP</Author>
<UploadBy>admin</UploadBy>
<GerneCode>1</GerneCode>
</Song>
</Songs>
and the Gerne.xml which generate from a schema
<ns2:gernes xmlns="http://www.w3.org/2001/XMLSchema/playlist" xmlns:ns2="https://xml.netbeans.org/schema/genses">
<ns2:gerne>
<GerneCode>1</GerneCode>
<GerneName>Pop</GerneName>
<Image>img-pop.jpg</Image>
</ns2:gerne>
</ns2:gerne>
I want to join these .xml files inside a XSL, to which will add the GerneName for every Song that match the GerneName inside the Gerne.xml.
The result im trying to get should be like this:
<Songs>
<Song>
<SongID>1</SongID>
<SongName>We dont talk anymore</SongName>
<Author>M-TP</Author>
<UploadBy>admin</UploadBy>
<GerneName>Pop</GerneName>
<GerneCode>1</GerneCode>
</Song>
</Songs>
Can anyone help me with this? Any e.g or keywords what should I look up for this problem?
Assuming you are limited to XSLT 1.0, you can do:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pl="http://www.w3.org/2001/XMLSchema/playlist"
xmlns:ns2="https://xml.netbeans.org/schema/genses"
exclude-result-prefixes="pl ns2">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="lookup-path" select="'Gerne.xml'" />
<xsl:key name="genre" match="ns2:gerne" use="pl:GerneCode" />
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="GerneCode">
<xsl:variable name="code" select="." />
<!-- switch context to the other file -->
<xsl:for-each select="document($lookup-path)">
<GerneCode>
<xsl:value-of select="key('genre', $code)/pl:GerneName" />
</GerneCode>
</xsl:for-each>
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
This is assuming you are telling your XSLT processor to process the Song.xml file, and passing the path to the Gerne.xml file as a parameter.
Note that the Gerne.xml document you have posted is not well-formed XML (no </ns2:gernes> closing tag).
BTW, the correct term is "genre" - not "gerne" or "gense".

Using XSLT to recursively load relative XML files and apply transformation

I have a xml file whose structure looks like this:
<root>
<includes>
<includeFile name="../other/some_xml.xml"/>
</includes>
<itemlist>
<item id="1" >
<selections>
<selection name="one" />
</selections>
</item>
</itemlist>
The xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
<xsl:output method="xml" indent="yes" xalan:indent-amount="4" />
<xsl:template match="/">
<xsl:element name="ItemList">
<xsl:if test="root/item">
<xsl:call-template name="templ" />
</xsl:if>
</xsl:element>
</xsl:template>
<xsl:template name="templ">
<xsl:element name="ItemList">
<xsl:for-each select="root/itemlist/item">
<xsl:element name="Item">
<xsl:element name="ItemIdentifier">
<xsl:value-of select="#id" />
</xsl:element>
<xsl:element name="Condition">
<xsl:value-of select="selections/selection[1]/#name" />
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
I created one XSLT which i am using to filter out items. The problem is that for every file, i have to check if it contains the includefile tag, which is a relative path pointing to a similar xml, and if it does, i need to collect items from that file also, recursively. For now i transformed the xml using my xslt and then i have to parse the xml to look for includefile tag. This solution doesn't look elegant and i was wondering if all of it could be done via xslt.
The XSLT document function in XSLT 1.0 and in XSLT 2.0 additionally the doc function allow you to pull in further documents, processing is then simply possible with matching templates. So consider to move your XSLT coding style to write matching templates and apply-templates, then you can easily do
<xsl:template match="includes/includeFile">
<xsl:apply-templates select="document(#name)/*"/>
<xsl:template>
and then you simply need to make sure the <xsl:template match="root">...</xsl:template> creates the output you want.

XSLT: set name of transformed output file

I am using Apache Camel file component and xslt component. I have a route where i pickup a xml message, transform using xslt and drop to a different folder.
Apache camel DSL route:
<route id="normal-route">
<from uri="file:{{inputfilefolder}}?consumer.delay=5000" />
<to uri="xslt:stylesheets/simpletransform.xsl transformerFactoryClass=net.sf.saxon.TransformerFactoryImpl" />
<to uri="file:{{outputfilefolder}}" />
</route>
I am mentioning Apache camel also here , to check if there is a way to set the output file name using Camel. I think, even without Camel, there would be a mechanism with pure XSLT.
I need to rename the transformed output file. But always i am getting the same input filename with the transformed content, in the output folder.
eg: input file: books.xml
output file: books.xml [with the transformation applied]
What i am looking for is someotherfilename.xml as the output filename. The output data is correct.
I tried <xsl:result-document href="{title}.xml"> , but then the output xml is blank. Please help.
Input XML file:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book.child.1>
<title>Charithram</title>
<author>P Sudarsanan</author>
</book.child.1>
<book.child.2>
<title>Java Concurrency</title>
<author>Joshua Bloch</author>
</book.child.2>
</books>
XSLT:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes" />
<xsl:variable name="filename" select="'newfilename'" />
<xsl:template match="/">
<xsl:result-document href="{$filename}.xml">
<traders>
<xsl:for-each select="books/*">
<trade>
<title>
<xsl:value-of select="title" />
</title>
</trade>
</xsl:for-each>
</traders>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
Output XML when using <xsl:result-document href="" in XSLT
it is blank..
Output XML when not using <xsl:result-document href="" in XSLT
<?xml version="1.0" encoding="UTF-8"?>
<traders xmlns:xs="http://www.w3.org/2001/XMLSchema">
<trade>
<title>Charithram</title>
</trade>
<trade>
<title>Java Concurrency</title>
</trade>
</traders>
Edit: edited the XSLT as per MartinHonnen's comment
Looks like Camel's default is to use the same file name, but you can override it. As the docs mention you can specify the options of interest as follows:
file:directoryName[?options]
One such option is fileName:
Use Expression such as File Language to dynamically set the filename.
For consumers, it's used as a filename filter. For producers, it's
used to evaluate the filename to write.
In short, modify your route as follows:
<route id="normal-route">
<from uri="file:{{inputfilefolder}}?consumer.delay=5000" />
<to uri="xslt:stylesheets/simpletransform.xsl transformerFactoryClass=net.sf.saxon.TransformerFactoryImpl" />
<to uri="file:{{outputfilefolder}}?fileName=foo.xml" />
</route>
Where foo.xml will be the output file.
Update
You can use Simple or File language to set file names dynamically. There are a few examples in the links.

Config <set-variable> with xpath gives empty string while extracting from xml payload

I am working on a service which add the detials sent in the request to to two different queues.
For this I use to extract part of the XML without disturbing the payload.
But it is giving blank.
My input looks as below
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://example.org/HelloService">
<soapenv:Header/>
<soapenv:Body>
<AddRequest>
<Person>
<firstName>Test</firstName>
<lastName>Tesst</lastName>
<age>23</age>
</Person>
<Company>
<companyName>Test</companyName>
<state>Tesst</state>
<zip>12345</zip>
</Company>
</AddRequest>
</soapenv:Body>
</soapenv:Envelope>
And part of my flow which is extracting the Person element is as below.
<set-variable value="#[xpath://Person]" variableName="person"></set-variable>
<logger level="INFO" message="#[flowVars['person']]" />
........
......
But the logger is printing it as blank
2013-01-30 12:56:08,287 INFO [HelloService.stage1.02] processor.LoggerMessageProcessor (LoggerMessageProcessor.java:108) -
Any idea why it is extracting blank space instead of the xml element.
How can I get the "Person" element from the paylaod using XPATH?
Use this:
<set-variable value="#[xpath('//Person')]" variableName="person" />
EDIT:
<set-variable value="#[xpath('//Person').text]" variableName="person" />
will return text value of an element, if it has one.
Try using the XSLT transformer to extract the xml element.
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="//Person" />
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>

Categories

Resources