user5458829
user5458829

Reputation: 163

Transformation using XSLT

Need help to convert SOAP message to XML: The element "externalID"can occur multiple time so based on "externalID" this element need to generate "Detail" tag in output XML.

Need some code sample to convert this SOAP message to desired output XML Input SOAP Message:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mem="http://example.com/vc/types/attachments">
   <soapenv:Header/>
   <soapenv:Body>
      <mem:payRequest>
         <payload>
            <uuid>?</uuid>
            <umi>?</umi>
            <externalID>
               <externalLineID></externalLineID>
               <groupNo>?</groupNo>
               <payeeCd>dfsdfsfd</payeeCd>
               <paymntAmt>?</paymntAmt>
               <acctCd>dfafa</acctCd>
               <transID>fasf</transID>
               <errCD>?</errCD>
               <errMessage>?</errMessage>
            </externalID>
            <externalID>
               <externalLineID>?</externalLineID>
               <groupNo>?</groupNo>
               <payeeCd>dfsdfsfd</payeeCd>
               <paymntAmt>?</paymntAmt>
               <acctCd>dfafa</acctCd>
               <transID>fasf</transID>
               <errCD>?</errCD>
               <errMessage>?</errMessage>
            </externalID>
            <errCD>?</errCD>
            <errMessage>?</errMe`enter code here`ssage>
         </payload>
      </mem:payRequest>
   </soapenv:Body>
</soapenv:Envelope>

==============

Desired Output XML:

<payRequest>
    <Detail>
        <uuid>?</uuid>
        <umd>?</umd>
        <claimNumber></claimNumber>
        <claimLineNumber>?</claimLineNumber>
        <dd>1</dd>
        <claimStatus></claimStatus>
        <clientNumber></clientNumber>
        <payMode>Other</payMode>
        <paymentAmount></paymentAmount>
        <accountCode></accountCode>
        <transactionId></transactionId>
        <errorCode></errorCode>
        <returnCode></returnCode>
    </Detail>
    <Detail>
        <uuid>?</uuid>
        <umd>?</umd>
        <claimNumber></claimNumber>
        <claimLineNumber>?</claimLineNumber>
        <dd>1</dd>
        <claimStatus></claimStatus>
        <clientNumber></clientNumber>
        <payMode>Other</payMode>
        <paymentAmount></paymentAmount>
        <accountCode></accountCode>
        <transactionId></transactionId>
        <errorCode></errorCode>
        <returnCode></returnCode>
    </Detail>
</payRequest>

Upvotes: 0

Views: 50

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

I'd suggest you try it along these lines:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
     <payRequest>
         <xsl:for-each select="//externalID">
            <Detail>            
                <uuid>
                    <xsl:value-of select="../uuid"/>
                </uuid>
                <umd>
                    <xsl:value-of select="../umi"/>
                </umd>
                <claimNumber>
                    <xsl:value-of select="XYZ"/>
                </claimNumber>
                <claimLineNumber>
                    <xsl:value-of select="externalLineID"/>
                </claimLineNumber>
                <dd>
                    <xsl:value-of select="XYZ"/>
                </dd>
                <claimStatus>
                    <xsl:value-of select="XYZ"/>
                </claimStatus>
                <clientNumber>
                    <xsl:value-of select="XYZ"/>
                </clientNumber>
                <payMode>
                    <xsl:value-of select="XYZ"/>
                </payMode>
                <paymentAmount>
                    <xsl:value-of select="paymntAmt"/>
                </paymentAmount>
                <accountCode>
                    <xsl:value-of select="XYZ"/>
                </accountCode>
                <transactionId>
                    <xsl:value-of select="transID"/>
                </transactionId>
                <errorCode>
                    <xsl:value-of select="errCD"/>
                </errorCode>
                <returnCode>
                    <xsl:value-of select="XYZ"/>
                </returnCode>
            </Detail>
         </xsl:for-each>
      </payRequest>
</xsl:template>

</xsl:stylesheet>

You did not provide any information regarding which source nodes are supposed to provide the data for the result. I have tried to guess a few and used XYZ as a placeholder for the others.

Note: you should never have to use a hack like *[local-name()='externalID'].

Upvotes: 0

Related Questions