简体   繁体   中英

How to choose a value based on if element exists for xslt

I am setting up an xslt transformation for our banking integration and I need to change a value of an element based on whether another element exists.

I can default the value using the following

<xsl:template  match="wdt:Document/wdt:CstmrCdtTrfInitn/wdt:PmtInf/wdt:CdtTrfTxInf/wdt:RmtInf/wdt:Strd/wdt:RfrdDocInf/wdt:Tp/wdt:CdOrPrtry/wdt:Cd" priority="3">
        <xsl:text>CINV</xsl:text>
</xsl:template>

when I try this:

<xsl:template match="wdt:Document/wdt:CstmrCdtTrfInitn/wdt:PmtInf/wdt:CdtTrfTxInf/wdt:RmtInf/wdt:Strd/wdt:RfrdDocInf/wdt:Tp/wdt:CdOrPrtry/wdt:Cd" priority="4">
  <xsl:if test="*[local-name()='CdtNoteAmt']">
   <xsl:value-of select="CREN"/>
  </xsl:if>
 </xsl:template>

I get an error.

My xml looks like this:

<RmtInf>
               <Strd>
                  <RfrdDocInf>
                     <Tp>
                        <CdOrPrtry>
                           <Cd>SOAC</Cd>
                        </CdOrPrtry>
                     </Tp>
                     <Nb>CS82001LF</Nb>
                     <RltdDt>2019-08-20</RltdDt>
                  </RfrdDocInf>
                  <RfrdDocAmt>
                     <DuePyblAmt Ccy="USD">820.01</DuePyblAmt>
                     <RmtdAmt Ccy="USD">820.01</RmtdAmt>
                  </RfrdDocAmt>
                  <CdtrRefInf>
                     <Ref>CS82001LF</Ref>
                  </CdtrRefInf>
               </Strd>
               <Strd>
                  <RfrdDocInf>
                     <Tp>
                        <CdOrPrtry>
                           <Cd>SOAC</Cd>
                        </CdOrPrtry>
                     </Tp>
                     <Nb>CS-CrNote82001</Nb>
                     <RltdDt>2019-08-20</RltdDt>
                  </RfrdDocInf>
                  <RfrdDocAmt>
                     <CdtNoteAmt Ccy="USD">50</CdtNoteAmt>
                  </RfrdDocAmt>
                  <CdtrRefInf>
                     <Ref>CS-CrNote82001</Ref>
                  </CdtrRefInf>
               </Strd>
            </RmtInf>

I would need the following element to have a value of CINV or CREN depending on if CdtNoteAmt element exists:

<Tp>
    <CdOrPrtry>
        <Cd>CINV</Cd>
    </CdOrPrtry>
</Tp>

If you want to check a CdtNoteAmt exists anywhere in the XML, you could do this...

<xsl:if test="//*[local-name()='CdtNoteAmt']">

Or better still, since you seem to dealing with known namespaces, you could do this...

<xsl:if test="//wdt:CdtNoteAmt">

On the other hand, if you want to check CdtNoteAmt exists under the same Strd as the current Cd element is under, do this...

<xsl:if test="ancestor::wdt:Strd//*[local-name()='CdtNoteAmt']">

Or this...

<xsl:if test="ancestor::wdt:Strd//wdt:CdtNoteAmt">

Note, to output "CERN" literally, you need to do this, otherwise you will be trying to be outputing the content of a element named "CERN"

<xsl:value-of select="'CREN'"/>

Or do this

<xsl:text>CERN</xsl:text>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM