XSLT apply-templates或基于xml值的调用模板

时间:2016-04-14 15:09:34

标签: xml xslt

XML / XPath / XSLT问题。我有一个伟大的祖父母节点,应该驱动更深的孙子节点的价值。应复制所有其他节点

这是XML(来自供应商,我无法控制格式)

付款类型(位于PmtInf / PmtTpInf / LclInstrm / Cd值中可以是ABC或XYZ,根据该值,PmtInf / Dbtr / Id / OrgId / Othr / Id值应为111111111或222222222

我可以成功地与xslt匹配

$(document).ready(function() {
  if ($(".errors").length) {
      $('#myform').addClass("fs-form-overview");
      $('#myform').removeClass("fs-form-full");
  }
});

但我在较高级别写出的任何元素都处于同一级别(可以预期)

如果我匹配较低级别,我无法找到&#34;节点值越高 <xsl:template match="CstmrCdtTrfInitn/PmtInf/PmtTpInf/LclInstrm[iso3:Cd='ABC']"> &GT;

我已经尝试将值设置为变量进行比较,我尝试过使用xslt之类的

<xsl:template match="PmtInf/Dbtr/Id/OrgId/Othr"

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

"//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>

1 个答案:

答案 0 :(得分:1)

以这种方式尝试:

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>