Skip to Content
0

HCI: Need xslt code to convert xml to text.

Dec 18, 2017 at 07:34 AM

292

avatar image
Former Member

Hi Experts

Below is my XML after one of my mapping

i need to convert it to text using an xslt code

<?xml version="1.0" encoding="UTF-8"?>
<ns0:FILE xmlns:ns0="http://bc.***.com/FILE">
   <ACCOUNT_ID>1234567</ACCOUNT_ID>
   <CURRENCY>EUR</CURRENCY>
   <BRANCH_ID>1234</BRANCH_ID>
   <TRANSACTION_TYPE>D</TRANSACTION_TYPE>
   <AMOUNT>47.5</AMOUNT>
   <PARTICULARS>2000000023100017</PARTICULARS>
</ns0:FILE>

I want this to be converted like below using xslt.

1234567EUR1234D47.52000000023100017

I need help in writing the xslt code for this one 
Regards
NAINA
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Evgeniy Kolmakov Dec 18, 2017 at 08:45 AM
0

Hi Naina!

XSLT isn't intended for producing text files from XML input. You could achieve it using java or groovy.

Regards, Evgeniy.

Show 2 Share
10 |10000 characters needed characters left characters exceeded

An XSLT stylesheet can create text output, similar to how it can create XML and HTML. Depending on the transformation, it can even be the best choice. Extracting pieces of data from deep within an XML document is so much easier in XSLT, whereas processing that data to generate text is probably easier in Groovy.

0

Hi Morten!

Acually I didn't say it couldn't. I just believe that using java, for example, gives you more flexibility and control on desired output.

Regards, Evgeniy.

1
Morten Wittrock
Dec 18, 2017 at 02:58 PM
0

Hi Naina

You can use the xsl:output element to indicate the type of output. Here's a small stylesheet that generates the text string you need:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://bc.***.com/FILE">
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:value-of select="/ns0:FILE/ACCOUNT_ID"/>
        <xsl:value-of select="/ns0:FILE/CURRENCY"/>
        <xsl:value-of select="/ns0:FILE/BRANCH_ID"/>
        <xsl:value-of select="/ns0:FILE/TRANSACTION_TYPE"/>
        <xsl:value-of select="/ns0:FILE/AMOUNT"/>
        <xsl:value-of select="/ns0:FILE/PARTICULARS"/>
    </xsl:template>
</xsl:stylesheet>

Regards,

Morten

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Jan 05 at 01:53 PM
0

Hi Naina,

This can be achieved using available standard pallet. You can achieve this like below, no need to go for XSLT mapping.

1. Use a message mapping to convert your structure like below. Simple concatenate function available in Graphical mapping is enough to achieve this.

<?xml version="1.0" encoding="UTF-8"?>
<Record>
<Text> ACCOUNT_ID+CURRENCY+BRANCH_ID+TRANSACTION_TYPE+AMOUNT+PARTICULARS </Text>
</Record>

2. After this step, use XML to CSV converter -

Path to Source Element in XSD: //Record

Field Separator in CSV: tab

Thanks,

Apu

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Apr 23 at 07:00 AM
0

Hi Naina,

You can use the below XSLT mapping to convert your XML record to Text file.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes" method="text"/>
  <xsl:strip-space elements="*"/>
   <xsl:template match="/">
        <xsl:apply-templates select="//file"/>
    </xsl:template>
    <xsl:template match="ACCOUNT_ID"> 
    <xsl:value-of select="."/>
    </xsl:template>
       <xsl:template match="CURRENCY"> 
    <xsl:value-of select="."/>
    </xsl:template>
       <xsl:template match="BRANCH_ID"> 
    <xsl:value-of select="."/>
    </xsl:template>
    <xsl:template match="TRANSACTION_TYPE"> 
    <xsl:value-of select="."/>
    </xsl:template>
    <xsl:template match="AMOUNT"> 
    <xsl:value-of select="."/>
    </xsl:template>
    <xsl:template match="PARTICULARS"> 
    <xsl:value-of select="."/>
        <xsl:text>
</xsl:text>
    </xsl:template>
   </xsl:stylesheet>

Share
10 |10000 characters needed characters left characters exceeded