cancel
Showing results for 
Search instead for 
Did you mean: 

Mapping question

Former Member
0 Kudos

I can have one of the following source messages.

<?xml version="1.0" encoding="UTF-8"?>

<GenericRecord>

  <record><MBH1></record>

</GenericRecord>

<GenericRecord>

  <record><BAL1></record>

</GenericRecord>

<GenericRecord>

  <record><MBH2></record>

</GenericRecord>

  <GenericRecord>

  <record><BAL2></record>

</GenericRecord>

  <GenericRecord>

  <record><PAY2></record>

</GenericRecord>

  <GenericRecord>

  <record><MBH3></record>

</GenericRecord>

  <GenericRecord>

  <record><BAL3></record>

</GenericRecord>

  <GenericRecord>

  <record><PAY3></record>

</GenericRecord>

</ns0:GenericRecs>

Or this payload.

<?xml version="1.0" encoding="UTF-8"?>

<ns0:GenericRecs xmlns:ns0="xxx">

<GenericRecord>

  <record><MBH1></record>

  <record><BAL1></record>

  <record><MBH2></record>

  <record><BAL2></record>

  <record><PAY2></record>

  <record><MBH3></record>

  <record><BAL3></record>

  <record><PAY3></record>

</GenericRecord>

</ns0:GenericRecs>

Have in mind that only one structure will be used and I prefer the structure that produces the simpliest mapping.

My mapping needs to produce the following from either source structure.

<?xml version="1.0" encoding="UTF-8"?>

<ns0:target xmlns:ns0="xxx">

<Card>

  <Data>MBH1</Data>

  <Data>BAL1</Data>

</Card>

<Card>

  <Data>MBH2</Data>

  <Data>BAL2</Data>

  <Data>PAY2</Data>

</Card>

<Card>

  <Data>MBH3</Data>

  <Data>BAL3</Data>

  <Data>PAY3</Data>

</Card>

</ns0:target>

For each field with MBH in the mapping should produce one card segment and all fields after that MBH field until next MBH field should create one filed inside the card segment including the first field with MBH in.

Second field with MBH shoudl create the second card segement...

I'm only able to produce the following result.

<?xml version="1.0" encoding="UTF-8"?>

<ns0:target xmlns:ns0="xxx">

<Card>

  <Data>MBH1</Data>

</Card>

<Card>

  <Data>MBH2</Data>

</Card>

<Card>

  <Data>MBH3</Data>

</Card>

</ns0:target>

Does anybody have an idea on how to solve it?

BR

Kalle

Accepted Solutions (1)

Accepted Solutions (1)

gagandeep_batra
Active Contributor
0 Kudos

Hi kalle,

i have tested with following example that may help you. i have use java code for that .

Regards

Gagandeep

Answers (2)

Answers (2)

peter_wallner2
Active Contributor
0 Kudos

Hello Kalle,

Can you use XSLT? This is a tricky one. Your source XML is not valid - the end tags are missing, so I adapted your original payload and it looks like this now:

<?xml version="1.0" encoding="UTF-8"?>

<GenericRecs>

    <GenericRecord>

        <record>

            <MBH1/>

        </record>

        <record>

            <BAL1/>

        </record>

        <record>

            <MBH2/>

        </record>

        <record>

            <BAL2/>

        </record>

        <record>

            <PAY2/>

        </record>

        <record>

            <MBH3/>

        </record>

        <record>

            <BAL3/>

        </record>

        <record>

            <PAY3/>

        </record>

    </GenericRecord>

</GenericRecs>

Using this XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:strip-space elements="*"/>

   

    <xsl:key name="kByNameSuf" match="record/*"

        use="translate(name(), translate(name(),'0123456789',''),'')"/>

   

    <xsl:template match="node()|@*">

        <xsl:copy>

            <xsl:apply-templates select="node()|@*"/>

        </xsl:copy>

    </xsl:template>

   

    <xsl:template match=

        "record[generate-id(*[1])=

        generate-id(key('kByNameSuf',translate(name(*[1]),translate(name(*[1]),'0123456789',''),''))[1])]">

        <Card>

            <xsl:for-each select=

                "key('kByNameSuf',translate(name(*[1]),translate(name(*[1]),'0123456789',''),''))">

                <Data><xsl:value-of select="name()"/></Data>

            </xsl:for-each>

        </Card>

    </xsl:template>

    <xsl:template match="record"/>

</xsl:stylesheet>

you get this XML output:

<GenericRecs>

    <GenericRecord>

        <Card>

            <Data>MBH1</Data>

            <Data>BAL1</Data>

        </Card>

        <Card>

            <Data>MBH2</Data>

            <Data>BAL2</Data>

            <Data>PAY2</Data>

        </Card>

        <Card>

            <Data>MBH3</Data>

            <Data>BAL3</Data>

            <Data>PAY3</Data>

        </Card>

    </GenericRecord>

</GenericRecs>

The XSLT assumes though that there are only digits in the suffix of the element name.

Best regards,

Peter

stephen_xue
Active Participant
0 Kudos

my proposal would be:

1. create a udf for the whole message.

     in the UDF, insert a context change when the sufix has changed.

2. use a split for context change afterwards.

hope it will help you.