Jay
Jay

Reputation: 29

XSLT apply-templates or call-template based on xml value

XML/XPath/XSLT question. I have a great-great-grandparent node that should drive the value of the deeper grandchild node. All other nodes should be copied

Here is the XML (from a vendor, I have no control over the format)

the payment type (located in the PmtInf/PmtTpInf/LclInstrm/Cd value can be ABC or XYZ and based on that value the PmtInf/Dbtr/Id/OrgId/Othr/Id value should be 111111111 or 222222222

I can successfully match with xslt like

<xsl:template match="CstmrCdtTrfInitn/PmtInf/PmtTpInf/LclInstrm[iso3:Cd='ABC']">

but any elements I write out at the higher level are at the same level (that would be expected)

If I match the lower level, I am not able to "find" the higher node value <xsl:template match="PmtInf/Dbtr/Id/OrgId/Othr">

I have tried setting the value to variable to compare, I have tried using xslt like

"../../../../PmtInf/PmtTpInf/LclInstrm/Cd = 'ABC'"

or xpath "//Id/ancestor::LclInstrm[1]"

<?xml version="1.0" encoding="utf-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
   <CstmrCdtTrfInitn>
      <GrpHdr>
         <MsgId>1302</MsgId>
         <CreDtTm>2016-04-06T08:30:44.533-07:00</CreDtTm>
         <NbOfTxs>2</NbOfTxs>
         <CtrlSum>0000.000</CtrlSum>
         <InitgPty>
            <Nm>NAME</Nm>
            <Id>
               <OrgId>
                  <Othr>
                     <Id>SPECIALID</Id>
                     <SchmeNm>
                        <Prtry>CUST</Prtry>
                     </SchmeNm>
                  </Othr>
               </OrgId>
            </Id>
         </InitgPty>
      </GrpHdr>
      <PmtInf>
         <PmtInfId>IDNUMBER</PmtInfId>
         <PmtMtd>TRF</PmtMtd>
         <BtchBookg>false</BtchBookg>
         <NbOfTxs>2</NbOfTxs>
         <CtrlSum>0000.00</CtrlSum>
         <PmtTpInf>
            <InstrPrty>NORM</InstrPrty>
            <SvcLvl>
               <Cd>NURG</Cd>
            </SvcLvl>
            <LclInstrm>
               <Cd>ABC</Cd><!-- This is the payment type, can be ABC or XYZ this value drives the Dbtr/Id/OrgId/Othr/Id value-->
            </LclInstrm>
         </PmtTpInf>
         <ReqdExctnDt>2016-04-06-07:00</ReqdExctnDt>
         <Dbtr>
            <Nm>NAME</Nm>
            <PstlAdr>
               <AdrTp>ADDR</AdrTp>
               <PstCd>ZIP</PstCd>
               <TwnNm>CITY</TwnNm>
               <CtrySubDvsn>STATE</CtrySubDvsn>
               <Ctry>US</Ctry>
               <AdrLine>ADDRESS1</AdrLine>
               <AdrLine>ADDRESS2</AdrLine>
            </PstlAdr>
            <Id>
               <OrgId>
                  <Othr>
                     <Id>1234567890</Id><!-- This field changes based on payment type field above. If ABC, 111111111 if XYZ 222222222 -->
                     <SchmeNm>
                        <Cd>CHID</Cd>
                     </SchmeNm>
                  </Othr>
               </OrgId>
            </Id>
         </Dbtr>
                 </PmtInf>
    </CstmrCdtTrfInitn>
</Document>

the desired output would have the same nodes/format but the value would change here

        <Id>
           <OrgId>
              <Othr>
                 <Id>111111111 (or 222222222 based on ancestor)</Id>          
                 <SchmeNm>
                    <Cd>CHID</Cd>
                 </SchmeNm>
              </Othr>
           </OrgId>
        </Id>

Upvotes: 1

Views: 342

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

Try it this way:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="ns1:Dbtr/ns1:Id/ns1:OrgId/ns1:Othr/ns1:Id">
    <xsl:variable name="cd" select="ancestor::ns1:PmtInf/ns1:PmtTpInf/ns1:LclInstrm/ns1:Cd" />
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="$cd='ABC'">111111111</xsl:when>
            <xsl:when test="$cd='XYZ'">222222222</xsl:when>
        </xsl:choose>       
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions