XML Transformation in GoAnywhere MFT using XSL

  Kiến thức lập trình

I am a beginner in GoAnywhere MFT
I have a following use case and would really appreciate if someone can point me towards the correct path/guide me with the solution.

There would be one XML file being pulled from the directory and there are 2 XSL files which defines the way XML has to be converted to a schema/format that is required by the user/system.

How can this be achieved in GoAnywhere MFT ? The Write XML seems very tricky and confusing.

For your reference, I am attaching a sample of all 3 files.

Input

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Header>
    <LoyaltyProgramID>WPG</LoyaltyProgramID>
    <FileTypeID>WJ01</FileTypeID>
    <BatchNumber>3646</BatchNumber>
    <FileCreationDateTime>2024-04-08T10:47:01</FileCreationDateTime>
    <EnvironmentIndicator>P</EnvironmentIndicator>
    <TotalRecords>1028</TotalRecords>
  </Header>
  <Transactions>
    <Transaction>
      <SequenceNumber>407</SequenceNumber>
      <PartnerReferenceNumber>xxxxxxxxxx</PartnerReferenceNumber>
      <MemberNumber>xxxxxxxxx</MemberNumber>
      <AccountID>xxxxxxxxx</AccountID>
      <ProductCode>MC4</ProductCode>
      <TransactionType>A</TransactionType>
      <TransactionDate>2024-04-03</TransactionDate>
      <FirstName>xxxxxx</FirstName>
      <LastName>xxxxxxxxxx-xxxxxxxxx</LastName>
      <LanguageCode>EN</LanguageCode>
      <AddressLine1>5007 VANTAGE CRESCENT NORTHW</AddressLine1>
      <City>CALGARY</City>
      <ProvinceCode>AB</ProvinceCode>
      <PostalCode>xxxxxxx</PostalCode>
      <CountryCode>CAN</CountryCode>
      <ResidentialPhone>xxxxxxxxxx</ResidentialPhone>
      <EmailAddress>[email protected]</EmailAddress>
      <AcquisitionChannel>5BW001</AcquisitionChannel>
    </Transaction>
    <Transaction>
      <SequenceNumber>408</SequenceNumber>
      <PartnerReferenceNumber>xxxxxxxxxx</PartnerReferenceNumber>
      <AccountID>xxxxxxxxx</AccountID>
      <ProductCode>MC2</ProductCode>
      <TransactionType>A</TransactionType>
      <TransactionDate>2024-04-03</TransactionDate>
      <FirstName>xxxxxx</FirstName>
      <LastName>xxxxxxxx</LastName>
      <LanguageCode>EN</LanguageCode>
      <AddressLine1>105 MACNAB STREET SOUTH 2</AddressLine1>
      <City>HAMILTON</City>
      <ProvinceCode>ON</ProvinceCode>
      <PostalCode>xxxxxxx</PostalCode>
      <CountryCode>CAN</CountryCode>
      <ResidentialPhone>xxxxx</ResidentialPhone>
      <EmailAddress>[email protected]</EmailAddress>
      <AcquisitionChannel>3DW001</AcquisitionChannel>
    </Transaction>
  </Transactions>
</Root>


Output

<?xml version="1.0" encoding="UTF-8"?>
<SiebelMessage MessageId="1-1K3H" MessageType="Integration Object" IntObjectName="WJ_Contact_Batch" IntObjectFormat="Siebel Hierarchical">
    <ListOfWj_Contact_Batch LoyaltyProgramID="WPG" FileTypeId="WJ01" BatchNumber="3646" FileCreationDateTime="2024-04-08T10:47:01" EnvironmentIndicator="P" TotalRecords="1028">
        <Contact SequenceNumber="407" PartnerReferenceNumber="xxxxxxxxxx" TransactionType="A" TransactionDate="04/03/2024">
            <ProfileID>
                xxxxxxxxx
            </ProfileID>
            <Email>
                [email protected]
            </Email>
            <FirstName>
                Aubrey
            </FirstName>
            <LastName>
                Blair pattison
            </LastName>
            <AcquisitionChannel>
                5BW001
            </AcquisitionChannel>
            <PreferredLanguage>
                English
            </PreferredLanguage>
            <ListOfContact_PersonalAddress>
                <Contact_PersonalAddress IsPrimaryMVG="Y">
                    <City>
                        CALGARY
                    </City>
                    <Country>
                        Canada
                    </Country>
                    <PostalCode>
                        xxxxxxx
                    </PostalCode>
                    <State>
                        AB
                    </State>
                    <Address>
                        5007 VANTAGE CRESCENT NORTHW
                    </Address>
                    <AddressLine2 />
                </Contact_PersonalAddress>
            </ListOfContact_PersonalAddress>
            <ListOfContact_AlternatePhone>
                <Contact_AlternatePhone IsPrimaryMVG="Y">
                    <AlternatePhone>
                        xxxxxxxxxx
                    </AlternatePhone>
                    <AlternatePhoneType>
                        Home
                    </AlternatePhoneType>
                </Contact_AlternatePhone>
                <Contact_AlternatePhone IsPrimaryMVG="N">
                    <AlternatePhone />
                    <AlternatePhoneType>
                        Business
                    </AlternatePhoneType>
                </Contact_AlternatePhone>
            </ListOfContact_AlternatePhone>
            <ListOfPersonalPaymentProfile>
                <PersonalPaymentProfile>
                    <WJAccountId>
                        xxxxxxxxx
                    </WJAccountId>
                    <WJProductCode>
                        xxxxx xxxxx
                    </WJProductCode>
                </PersonalPaymentProfile>
            </ListOfPersonalPaymentProfile>
            <BusinessName />
        </Contact>
        <Contact SequenceNumber="408" PartnerReferenceNumber="xxxxxxxxxx" TransactionType="A" TransactionDate="04/03/2024">
            <ProfileID />
            <Email>
                [email protected]
            </Email>
            <FirstName>
                Julian
            </FirstName>
            <LastName>
                Mcguire
            </LastName>
            <AcquisitionChannel>
                3DW001
            </AcquisitionChannel>
            <PreferredLanguage>
                English
            </PreferredLanguage>
            <ListOfContact_PersonalAddress>
                <Contact_PersonalAddress IsPrimaryMVG="Y">
                    <City>
                        HAMILTON
                    </City>
                    <Country>
                        Canada
                    </Country>
                    <PostalCode>
                        xxxxxxx
                    </PostalCode>
                    <State>
                        ON
                    </State>
                    <Address>
                        105 MACNAB STREET SOUTH 2
                    </Address>
                    <AddressLine2 />
                </Contact_PersonalAddress>
            </ListOfContact_PersonalAddress>
            <ListOfContact_AlternatePhone>
                <Contact_AlternatePhone IsPrimaryMVG="Y">
                    <AlternatePhone>
                        xxxxxxxxxx
                    </AlternatePhone>
                    <AlternatePhoneType>
                        Home
                    </AlternatePhoneType>
                </Contact_AlternatePhone>
                <Contact_AlternatePhone IsPrimaryMVG="N">
                    <AlternatePhone />
                    <AlternatePhoneType>
                        Business
                    </AlternatePhoneType>
                </Contact_AlternatePhone>
            </ListOfContact_AlternatePhone>
            <ListOfPersonalPaymentProfile>
                <PersonalPaymentProfile>
                    <WJAccountId>
                        xxxxxxxxx
                    </WJAccountId>
                    <WJProductCode>
                        xxxxx xx
                    </WJProductCode>
                </PersonalPaymentProfile>
            </ListOfPersonalPaymentProfile>
            <BusinessName />
        </Contact>
    </ListOfWj_Contact_Batch>
</SiebelMessage>


XSL File (Sample)

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

   <xsl:output method="xml" indent="yes"/>
   
   <xsl:variable name="dashFile" select="document('specialChar.xml')/characters"/>
   <xsl:variable name="t1">
      <xsl:copy-of select="$dashFile/translations/tran/source"/>
   </xsl:variable>
   <xsl:variable name="t2">
      <xsl:copy-of select="$dashFile/translations/tran/target"/>
   </xsl:variable>
   
   <xsl:template match="*">
      <xsl:copy>
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates/>
      </xsl:copy>
   </xsl:template>
   
   <xsl:template match="/*[local-name()='Root']/*[local-name()='Transactions']/*[local-name()='Transaction']/*[local-name()='FirstName']">
      <xsl:call-template name="translate"/>
   </xsl:template>
   
   <xsl:template match="/*[local-name()='Root']/*[local-name()='Transactions']/*[local-name()='Transaction']/*[local-name()='LastName']">
      <xsl:call-template name="translate"/>
   </xsl:template>
<!--   
   <xsl:template match="/*[local-name()='Root']/*[local-name()='Transactions']/*[local-name()='Transaction']/*[local-name()='EmailAddress']">
      <xsl:call-template name="translate"/>
   </xsl:template>
-->   
   <xsl:template match="/*[local-name()='Root']/*[local-name()='Transactions']/*[local-name()='Transaction']/*[local-name()='AddressLine1']">
      <xsl:call-template name="translate"/>
   </xsl:template>
   
   <xsl:template match="/*[local-name()='Root']/*[local-name()='Transactions']/*[local-name()='Transaction']/*[local-name()='AddressLine2']">
      <xsl:call-template name="translate"/>
   </xsl:template>
   
   <xsl:template match="/*[local-name()='Root']/*[local-name()='Transactions']/*[local-name()='Transaction']/*[local-name()='City']">
      <xsl:call-template name="translate"/>
   </xsl:template>
   
   <xsl:template name="translate">
      <xsl:variable name="raw" select="."/>
      <xsl:variable name="clean" select="translate($raw, $t1, $t2)"/>
      <xsl:element name="{name()}">
         <xsl:copy-of select="@*"/>
         <xsl:value-of select="$clean"/>
      </xsl:element>
   </xsl:template>
   
</xsl:stylesheet>

Tried exploring the Write XML feature of GoAnywhere but it doesn’t have the solution. Also tried looking for any other alternatives but got nowhere. Open to utilize any other way like running a BAT file/script to carry this task.

LEAVE A COMMENT