Transformation of text file into an xml using datapower

Uselesssss picture Uselesssss · Feb 15, 2014 · Viewed 9.5k times · Source

I want to transfer a non-xml text file delimited by '|' characters into an xml using Datapower.

Following is file  (sample1)

10|20003|24/23/25|23890

Now i have to break this into the following XML

 <ResponseType>
        <ResCode>10</ResCode>
        <Id>20003</Id>
         <SoftCode>24/23/25</SoftCode>
        <StatusCode>23890</StatusCode>
  </ResponseType>

What I did was following--

1>Create a Transform action in the service that will be receiving non-XML requests.

2>Select "Use XSLT specified in this action on a non-XML message" to specify that this is a Binary Transform.

3>Upload the following stylesheet as the Processing Control File.

    <?xml version="1.0" encoding="utf-8"?>
       <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dp="http://www.datapower.com/extensions"
    version="1.0">

    <dp:input-mapping href="sample1.ffd" type="ffd"/>


    <xsl:output method="xml"/>

    <xsl:template match="/">
    <xsl:copy-of select="ResponseType"/>
    <xsl:call-template name="str:tokenize">
     <xsl:with-param name="string" select="string" />

    </xsl:call-template>
    </xsl:template>


    <xsl:template name="str:tokenize">
<xsl:with-param name="string" select="">

    str:tokenize('string', '|')
</xsl:with param>
</xsl:template>
    </xsl:stylesheet>

and here is my sample1.ffd(which I have uploaded in my local:// directory in Datapower

<File name="ResponseType">
<!-- capture all data into this tag -->
<Field name="ResCode/Id/SoftCode/StatusCode" />
</File> 

But I am not getting desired output , I think my xslt is quite wrong What can I do do to get desired output?

Answer

Anders picture Anders · Feb 27, 2014

In DataPower using FFD the following should work:

1) Add the FFD file (below one of my old education samples):

<File name="CSVFILE">
    <Group name="CSVLine" minOccurs="0" maxOccurs="unbounded" delim="\n">
        <Field name="id"/>
        <Field name="fname" delim=","/>
        <Field name="lname" delim=","/>
        <Field name="title" delim=","/>
        <Field name="dept" delim=","/>
        <Field name="org"/>
    </Group>
</File>

2) Add the XSLT (this one simply copies the FFD transformed XML to output):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:dp="http://www.datapower.com/extensions"
                version="1.0">

    <dp:input-mapping href="CSVFILE.FFD" type="ffd"/>

    <!-- This stylesheet copies the input to the output -->
    <xsl:output method="xml"/>

    <xsl:template match="/">

        <xsl:copy-of select="." />

    </xsl:template>
</xsl:stylesheet>

3) Send a message:

1,Anders,Wasen,B2B Architect,DataPower Dev,Enfo Zystems
2,Jean-Luc,Piccard,Captain,USS Enterprise,Star Fleet

4) This will result in the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<CSVFILE>
 <CSVLine>
  <id>1</id>
  <fname>Anders</fname>
  <lname>Wasen</lname>
  <title>B2B Architect</title>
  <dept>DataPower Dev,Enfo Zystems</dept>
  <org/>
 </CSVLine>
 <CSVLine>
  <id>2</id>
  <fname>Jean-Luc</fname>
  <lname>Piccard</lname>
  <title>Captain</title>
  <dept>USS Enterprise,Star Fleet</dept>
  <org/>
 </CSVLine>
</CSVFILE>

Make sure that you change the XSLT Transform action into "Transform Binary" and set Request Type to "non-xml", else it will not work!

Hope this will help you! :)