XSLT-XML转换并删除空标签

时间:2019-02-06 09:41:41

标签: xml xslt xslt-1.0

在一个XSLT文件中,我需要转换XML并删除生成的XML上的空标签。

我正在尝试向我的XSLT代码中添加其他模板,以删除空元素。使用XSLT 1.0,我发现我需要使用mode属性来首先执行转换,然后通过将初始结果存储在变量中从中删除空标记。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common">
<xsl:strip-space elements="*" />
<xsl:output indent="yes" />

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

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

<xsl:template match="/">
    <xsl:variable name="vrtfPass1Result">
        <xsl:apply-templates/>
</xsl:variable>

<xsl:apply-templates mode="mPass2"
    select="ext:node-set($vrtfPass1Result)/*"/>
</xsl:template>

<xsl:template match="/Root/Element">
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:DRAFT1auth.052.001.01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:DRAFT1auth.052.001.01 file://LON-SLFILE04/MDingle$/My%20Documents/SFTR/Schema/DRAFT_SFTR_documentation/DRAFT1auth.052.001.01.xsd">
    <SctiesFincgRptgTxRpt>
        <TradData>
            <New>
                <TechRcrdId>a</TechRcrdId>
                <CtrPtyData>
                    <RptgDtTm><xsl:value-of select="@Reporting_timestamp" /></RptgDtTm>
                    <RptSubmitgNtty><xsl:value-of select="@Report_submitting_entity" /></RptSubmitgNtty>
                    <CtrPtyData>
                        <RptgCtrPty>
                            <Id>
                                <LEI><xsl:value-of select="@Reporting_counterparty" /></LEI>
                            </Id>
                            <Ntr>
                                <xsl:if test="@Nature_of_the_reporting_counterparty = 'N'" >
                                    <NFI><xsl:value-of select="@Nature_of_the_reporting_counterparty" /></NFI>
                                </xsl:if>
                                <FI>
                                    <xsl:if test="@Nature_of_the_reporting_counterparty = 'F'" >
                                        <xsl:value-of select="@Nature_of_the_reporting_counterparty" />
                                    </xsl:if>
                                    <Clssfctn><xsl:value-of select="@Sector_of_the_reporting_counterparty" /></Clssfctn>
                                    <InvstmtFndClssfctn><xsl:value-of select="@Additional_sector_classification" /></InvstmtFndClssfctn>
                                </FI>
                            </Ntr>
                            <Brnch>
                                <Id>
                                    <xsl:if test="string-length(Branch_of_the_reporting_counterparty) &gt; 3" >
                                        <LE><xsl:value-of select="@Branch_of_the_reporting_counterparty" /></LE>
                                    </xsl:if>
                                    <xsl:if test="string-length(Branch_of_the_reporting_counterparty) &lt; 3" >
                                        <Ctry><xsl:value-of select="@Branch_of_the_reporting_counterparty" /></Ctry>
                                    </xsl:if>
                                </Id>
                            </Brnch>
                            <Sd><xsl:value-of select="@Counterparty_side" /></Sd>
                        </RptgCtrPty>
                        <OthrCtrPty>
                            <Id>
                                <xsl:if test="string-length(Branch_of_the_other_counterparty) &gt; 3" >
                                        <LE><xsl:value-of select="@Branch_of_the_other_counterparty" /></LE>
                                </xsl:if>
                                <xsl:if test="string-length(Other_counterparty) &lt; 21" >
                                        <LEI><xsl:value-of select="@Other_counterparty" /></LEI>
                                </xsl:if>
                                <xsl:if test="string-length(Other_counterparty) &gt; 21" >
                                        <Clntld><xsl:value-of select="@Other_counterparty" /></Clntld>
                                </xsl:if>                                   
                            </Id>
                            <xsl:if test="string-length(Branch_of_the_other_counterparty) &lt; 3" >
                                    <Ctry><xsl:value-of select="@Branch_of_the_other_counterparty" /></Ctry>
                            </xsl:if>
                            <CtryCd><xsl:value-of select="@Country_of_the_other_Counterparty" /></CtryCd>
                        </OthrCtrPty>
                        <NttyRspnsblForRpt><xsl:value-of select="@Entity_responsible_for_the_report" /></NttyRspnsblForRpt>
                        <TxCtrPtyData>
                            <RpTrad>
                                <Bnfcry><xsl:value-of select="@Beneficiary" /></Bnfcry>
                                <TrptyAgt><xsl:value-of select="@Tri-party_agent" /></TrptyAgt>
                                <Brkr><xsl:value-of select="@Broker" /></Brkr>
                                <ClrMmb><xsl:value-of select="@Clearing_member" /></ClrMmb>
                                <SttlmPties>
                                    <CntrlSctiesDpstryPtcpt><xsl:value-of select="@CSD_participant_or_indirect_participant" /></CntrlSctiesDpstryPtcpt>
                                    <IndrctPtcpt><xsl:value-of select="@CSD_participant_or_indirect_participant" /></IndrctPtcpt>
                                </SttlmPties>
                                <AgtLndr><xsl:value-of select="@Agent_lender" /></AgtLndr>
                            </RpTrad>
                        </TxCtrPtyData>                         
                    </CtrPtyData>
                </CtrPtyData>
                <LnData>
                    <UnqTradIdr><xsl:value-of select="@Unique_Transaction_Identifier__x0028_UTI_x0029_" /></UnqTradIdr>
                    <EvtDt><xsl:value-of select="@Event_date" /></EvtDt>
                    <CtrctTp><xsl:value-of select="@Type_of_SFT" /></CtrctTp>
                    <ExctnDtTm><xsl:value-of select="@Execution_timestamp" /></ExctnDtTm>
                    <TxLnData>
                        <RpTrad>
                            <ClrSts>
                                <Clrd>
                                    <RptTrckgNbc><xsl:value-of select="@Report_tracking_number" /></RptTrckgNbc>
                                    <xsl:if test="@Cleared = 'true'" >
                                        <xsl:value-of select="@Cleared" />
                                    </xsl:if>
                                    <ClrDtTm><xsl:value-of select="@Clearing_timestamp" /></ClrDtTm>
                                    <CCP><xsl:value-of select="@CCP" /></CCP>
                                    <PrtflCd><xsl:value-of select="@Portfolio_code" /></PrtflCd>
                                </Clrd>
                                <xsl:if test="not(@Cleared = 'true')" >
                                    <NonClrd><xsl:value-of select="@Cleared" /></NonClrd>
                                </xsl:if>
                            </ClrSts>
                            <TradgVn><xsl:value-of select="@Trading_venue" /></TradgVn>
                            <MstrAgrmt>
                                <Tp>                                        
                                    <xsl:if test="@Master_Agreement_Type_Enumerations_Check = 'true'" >
                                        <Tp>
                                            <xsl:value-of select="@Master_agreement_type" />
                                        </Tp>
                                    </xsl:if>
                                    <xsl:if test="not(@Master_Agreement_Type_Enumerations_Check = 'true')" >
                                        <Prtry>
                                            <xsl:value-of select="@Master_agreement_type" />
                                        </Prtry>
                                    </xsl:if>                                   
                                </Tp>
                                <OthrMstrAgrmt><xsl:value-of select="@Other_master_agreement_type" /></OthrMstrAgrmt>
                                <Vrsn><xsl:value-of select="@Master_agreement_version" /></Vrsn>                                    
                            </MstrAgrmt>
                            <ValDt><xsl:value-of select="@Value_date__x0028_Start_date_x0029_" /></ValDt>
                            <MinNtcePrd><xsl:value-of select="@Minimum_notice_period" /></MinNtcePrd>
                            <EarlstCallBckDt><xsl:value-of select="@Earliest_call-back_date" /></EarlstCallBckDt>
                            <GnlColl><xsl:value-of select="@General_collateral_indicator" /></GnlColl>
                            <DlvryByVal><xsl:value-of select="@DBV_indicator" /></DlvryByVal>
                            <CollDlvryMtd><xsl:value-of select="@Method_used_to_provide_collateral" /></CollDlvryMtd>
                            <Term>
                                <Fxd>
                                    <MtrtyDt><xsl:value-of select="@Maturity_date__x0028_End_date_x0029_" /></MtrtyDt>
                                    <TermntnOptn><xsl:value-of select="@Termination_optionality" /></TermntnOptn>
                                </Fxd>
                                <Opn><xsl:value-of select="@Open_term" /></Opn>
                            </Term>
                            <IntrstRate>
                                <Fxd>
                                    <Rate><xsl:value-of select="@Fixed_rate" /></Rate>
                                    <DayCntBsis>
                                        <xsl:if test="@Day_Count_Convention_Enumerations_Check = 'true'" >
                                            <Cd><xsl:value-of select="@Day_count_convention" /></Cd>                                                
                                        </xsl:if>
                                        <xsl:if test="not(@Day_Count_Convention_Enumerations_Check = 'true')" >
                                            <Prtry><xsl:value-of select="@Day_count_convention" /></Prtry>
                                        </xsl:if>                                           
                                    </DayCntBsis>
                                </Fxd>
                                <Fltg>
                                    <RefRate>
                                        <xsl:if test="@Floating_Rate_Enumerations_Check = 'true'" >
                                            <Indx><xsl:value-of select="@Floating_rate" /></Indx>                                               
                                        </xsl:if>
                                        <xsl:if test="not(@Floating_Rate_Enumerations_Check = 'true')" >
                                            <Nm><xsl:value-of select="@Floating_rate" /></Nm>
                                        </xsl:if>
                                    </RefRate>
                                    <Term>
                                        <Unit><xsl:value-of select="@Floating_rate_reference_period_-_time_period" /></Unit>
                                        <Val><xsl:value-of select="@Floating_rate_reference_period_-_multiplier" /></Val>
                                    </Term>
                                    <PmtFrqcy>
                                        <Unit><xsl:value-of select="@Floating_rate_payment_frequency_-_time_period" /></Unit>
                                        <Val><xsl:value-of select="@Floating_rate_payment_frequency_-_multiplier" /></Val>
                                    </PmtFrqcy>
                                    <RstFrqcy>
                                        <Unit><xsl:value-of select="@Floating_rate_reset_frequency-_time_period" /></Unit>
                                        <Val><xsl:value-of select="@Floating_rate_reset_frequency_-_multiplier" /></Val>
                                    </RstFrqcy>
                                    <BsisPtSprd><xsl:value-of select="@Spread" /></BsisPtSprd>
                                    <MrgnLnAmt>
                                        <xsl:attribute name = "Ccy">
                                            <xsl:value-of select="@Margin_lending_currency" />
                                        </xsl:attribute>
                                        <xsl:value-of select="@Margin_lending_currency_amount" />                                           
                                    </MrgnLnAmt>
                                    <RateAdjstmnt>
                                        <Rate><xsl:value-of select="@Adjusted_rate" /></Rate>
                                        <AdjstmntDt><xsl:value-of select="@Rate_date" /></AdjstmntDt>
                                    </RateAdjstmnt>
                                </Fltg>
                            </IntrstRate>
                            <PrncplAmt>
                                <ValDtAmt><xsl:value-of select="@Principal_amount_on_value_date" /></ValDtAmt>
                                <MtrtyDtAmt><xsl:value-of select="@Principal_amount_on_maturity_date" /></MtrtyDtAmt>
                                <Ccy><xsl:value-of select="@Principal_amount_currency" /></Ccy>
                            </PrncplAmt>
                            <NetXpsrCollstnInd><xsl:value-of select="@Collateralisation_of_net_exposure" /></NetXpsrCollstnInd>
                        </RpTrad>
                        <SctiesLndg>
                            <AsstTp>
                                <xsl:value-of select="@Type_of_asset" />
                                <Scty>
                                    <Id><xsl:value-of select="@Security_Identifier" /></Id>
                                    <Clssfctn><xsl:value-of select="@Classification_of_a_security" /></Clssfctn>
                                    <Qty><xsl:value-of select="@Quantity_or_nominal_amount" /></Qty>
                                    <QtyOrNmnlVal>
                                        <NmnlVal>
                                            <Ccy><xsl:value-of select="@Currency_of_nominal_amount" /></Ccy>
                                        </NmnlVal>
                                    </QtyOrNmnlVal>
                                    <UnitPric>
                                        <xsl:value-of select="@Security_or_commodity_price" />
                                        <Ccy><xsl:value-of select="@Price_currency" /></Ccy>
                                    </UnitPric>
                                    <Qlty><xsl:value-of select="@Security_quality" /></Qlty>
                                    <Mtrty><xsl:value-of select="@Maturity_of_the_security" /></Mtrty>
                                    <Issr>
                                        <JursdctnCtry><xsl:value-of select="@Jurisdiction_of_the_issuer" /></JursdctnCtry>
                                        <LEI><xsl:value-of select="@LEI_of_the_issuer" /></LEI>
                                    </Issr>
                                    <Tp>
                                        <Cd><xsl:value-of select="@Security_type" /></Cd>
                                    </Tp>
                                </Scty>
                                <Cmmdty>
                                    <Qty>
                                        <UnitOfMeasr><xsl:value-of select="@Unit_of_measure" /></UnitOfMeasr>
                                    </Qty>
                                </Cmmdty>
                            </AsstTp>
                            <LnVal><xsl:value-of select="@Loan_value" /></LnVal>
                            <MktVal><xsl:value-of select="@Market_value" /></MktVal>
                            <RbtRate>
                                <Rate><xsl:value-of select="@Fixed_rebate_rate" /></Rate>
                                <FltgRbtRate>
                                    <RefRate>
                                        <Indx><xsl:value-of select="@Floating_rebate_rate" /></Indx>
                                    </RefRate>
                                    <Term>
                                        <Unit><xsl:value-of select="@Floating_rebate_rate_reference_period_-_time_period" /></Unit>
                                        <Val><xsl:value-of select="@Floating_rebate_rate_reference_period_-_multiplier" /></Val>
                                    </Term>
                                    <PmtFrqcy>
                                        <Unit><xsl:value-of select="@Floating_rebate_rate_payment_frequency_-_time_period" /></Unit>
                                        <Val><xsl:value-of select="@Floating_rebate_rate_payment_frequency_-_multiplier" /></Val>
                                    </PmtFrqcy>
                                    <RstFrqcy>
                                        <Unit><xsl:value-of select="@Floating_rebate_rate_reset_frequency-_time_period" /></Unit>
                                        <Val><xsl:value-of select="@Floating_rebate_rate_reset_frequency-_multiplier" /></Val>
                                    </RstFrqcy>
                                    <BsisPtSprd><xsl:value-of select="@Spread_of_the_rebate_rate" /></BsisPtSprd>
                                </FltgRbtRate>
                            </RbtRate>
                            <LndgFee><xsl:value-of select="@Lending_fee" /></LndgFee>
                            <ExclsvArrgmnt><xsl:value-of select="@Exclusive_arrangements" /></ExclsvArrgmnt>
                        </SctiesLndg>
                        <MrgnLndg>
                            <OutsdngMrgnLnAmt><xsl:value-of select="@Outstanding_margin_loan" /></OutsdngMrgnLnAmt>
                            <Ccy><xsl:value-of select="@Base_currency_of_outstanding_margin_loan" /></Ccy>
                            <ShrtMktValAmt><xsl:value-of select="@Short_market_value" /></ShrtMktValAmt>
                        </MrgnLndg>
                    </TxLnData>
                </LnData>
                <CollData>
                    <SctiesLndg>
                        <Uncollsd><xsl:value-of select="@Uncollateralised_SL_flag" /></Uncollsd>
                    </SctiesLndg>
                    <RpTrad>
                        <CollValDt><xsl:value-of select="@Value_date_of_the_collateral" /></CollValDt>
                        <Tp>                                
                            <xsl:value-of select="@Type_of_collateral_component" />
                            <Csh>
                                <xsl:attribute name = "Ccy">
                                    <xsl:value-of select="@Cash_collateral_currency" />
                                </xsl:attribute>
                                <xsl:value-of select="@Cash_collateral_amount" />
                            </Csh>
                            <Scties>
                                <Id>
                                    <Id><xsl:value-of select="@Identification_of_a_security_used_as_collateral" /></Id>
                                    <ClssfctnTp><xsl:value-of select="@Classification_of_a_security_used_as_collateral" /></ClssfctnTp>
                                    <QtyOrNmnlVal>
                                        <NmnlVal>
                                            <xsl:attribute name = "Ccy">
                                                <xsl:value-of select="@Currency_of_collateral_nominal_amount" />
                                            </xsl:attribute>
                                        </NmnlVal>
                                    </QtyOrNmnlVal>
                                    <HrcutOrMrgn><xsl:value-of select="@Haircut_or_margin" /></HrcutOrMrgn>
                                    <Qlty><xsl:value-of select="@Collateral_quality" /></Qlty>
                                    <Mtrty><xsl:value-of select="@Collateral_security_maturity" /></Mtrty>
                                    <Issr>
                                        <JursdctnCtry><xsl:value-of select="@Collateral_jurisdiction_of_the_issuer" /></JursdctnCtry>                                           
                                        <LEI><xsl:value-of select="@Collateral_LEI_of_the_issuer" /></LEI>
                                    </Issr>
                                    <Tp>
                                        <xsl:if test="@Collateral_Type_Enumerations_Check = 'true'" >
                                            <Cd><xsl:value-of select="@Collateral_type" /></Cd>
                                        </xsl:if>
                                        <xsl:if test="not(@Collateral_Type_Enumerations_Check = 'true')" >
                                            <Prtry><xsl:value-of select="@Collateral_type" /></Prtry>
                                        </xsl:if>
                                    </Tp>
                                    <AvlblForCollReuse><xsl:value-of select="@Availability_for_collateral_reuse" /></AvlblForCollReuse>
                                </Id>
                            </Scties>
                            <Cmmdty>
                                <Qty>
                                    <UnitOfMeasr><xsl:value-of select="Collateral_unit_of_measure" /></UnitOfMeasr>
                                </Qty>
                            </Cmmdty>
                        </Tp>
                        <BsktIdr>
                            <NotAvlbl>NTAV</NotAvlbl>
                        </BsktIdr>
                    </RpTrad>
                </CollData>
                <xsl:value-of select="@Action_type" />
                <LvlTp><xsl:value-of select="@Level" /></LvlTp>
                <SplmtryData>
                    <PlcAndNm>a</PlcAndNm>
                    <Envlp>
                        <auto-generated_for_wildcard xmlns=""/>
                    </Envlp>
                </SplmtryData>
            </New>
            <CollUpd>
                <CollData>
                    <TxCollData>
                        <RpTrad>
                            <BsktIdr>
                                <xsl:if test="@Collateral_basket_identifier = 'NTAV'" >
                                            <NotAvlbl><xsl:value-of select="@Collateral_basket_identifier" /></NotAvlbl>
                                        </xsl:if>
                                        <xsl:if test="not(@Collateral_basket_identifier = 'NTAV')" >
                                            <Id><xsl:value-of select="@Collateral_basket_identifier" /></Id>
                                        </xsl:if>
                            </BsktIdr>
                        </RpTrad>
                    </TxCollData>
                </CollData>
            </CollUpd>
        </TradData>
        <SplmtryData>
            <PlcAndNm>a</PlcAndNm>
            <Envlp>
                <auto-generated_for_wildcard xmlns=""/>
            </Envlp>
        </SplmtryData>
    </SctiesFincgRptgTxRpt>
</Document>
</xsl:template> 

<xsl:template match="/*" mode="mPass2">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates mode="mPass2"/>
    </xsl:copy>
</xsl:template>

<xsl:template match=
    "*[not(@*|*|comment()|processing-instruction()) 
     and normalize-space()=''
      ]" mode="mPass2"/>

</xsl:stylesheet>

以下是结果的一部分:

<RptgCtrPty>
              <Id>
                <LEI>G5GSEF7VJP5I7OUK5572</LEI>
              </Id>
              <Ntr>
                <FI>
                  <Clssfctn></Clssfctn>
                  <InvstmtFndClssfctn></InvstmtFndClssfctn>
                </FI>
              </Ntr>
              <Brnch>
                <Id>
                  <Ctry></Ctry>
                </Id>
              </Brnch>
              <Sd></Sd>
</RptgCtrPty>

我希望:

<RptgCtrPty>
              <Id>
                <LEI>G5GSEF7VJP5I7OUK5572</LEI>
              </Id>              
</RptgCtrPty>

转换工作正常,只是删除需要工作的空标签。

感谢您的帮助, 扫罗

2 个答案:

答案 0 :(得分:2)

尝试将最终的mPass2模板更改为此。...

<xsl:template 
     match="*[not(.//@*|.//comment()|.//processing-instruction()) and normalize-space()='']" 
     mode="mPass2"/>

因此,这将检查所有后代,而不是直接子代。 (当前节点上的normalize-space将已应用于所有后代文本节点的串联)

答案 1 :(得分:0)

由于您的输入未知,因此在转换xml之前是否有这样的空节点。

您可以将模板更改为:

<xsl:template match="@*|node()" mode="mPass2">
<xsl:if test="normalize-space(.) != '' or ./@* != ''">
    <xsl:copy>
        <xsl:copy-of select="@*" />
        <xsl:apply-templates select="node()|@*" mode="mPass2"/>
    </xsl:copy>
</xsl:if>