简体   繁体   中英

Remove empty nodes from XML using XSLT

I am having trouble removing empty nodes from my XML using XSLT. For example, this area in the XSLT:

             <RCPNT_NM>
                <snm>
                    <xsl:value-of select="SNM"/>
                </snm>
                <gv_nm>
                    <xsl:value-of select="GVN_NM"/>
                </gv_nm>
                <init>
                    <xsl:value-of select="INIT"/>
                </init>
            </RCPNT_NM>

The data inside some of those nodes is completely empty, but I am still getting them back. I tried applying the match="node()|@*" template area to the xslt, but that didn;t work either. Here is the entire XSLT file:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" version="2.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
    <Return>
        <xsl:apply-templates select="//IPERSON/item"/>
    </Return>
</xsl:template>
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="*[not(@*) and not(*) and (not(text()) )]"/>
<xsl:template match="IPERSON/item">
    <T5018Slip>
        <RCPNT_NM>
            <snm>
                <xsl:value-of select="SNM"/>
            </snm>
            <gv_nm>
                <xsl:value-of select="GVN_NM"/>
            </gv_nm>
            <init>
                <xsl:value-of select="INIT"/>
            </init>
        </RCPNT_NM>
        <sin>
            <xsl:value-of select="SIN"/>
        </sin>
        <rcpnt_bn>
            <xsl:value-of select="RCPNT_BN"/>
        </rcpnt_bn>
        <CORP_PTNRP_NM>
            <l1_nm>
                <xsl:value-of select="CORPL1_NM"/>
            </l1_nm>
            <l2_nm>
                <xsl:value-of select="CORPL2_NM"/>
            </l2_nm>
        </CORP_PTNRP_NM>
        <rcpnt_tcd>
            <xsl:value-of select="RCPNT_TCD"/>
        </rcpnt_tcd>
        <RCPNT_ADDR>
            <addr_l1_txt>
                <xsl:value-of select="ADDR_L1_TXT"/>
            </addr_l1_txt>
            <addr_l2_txt>
                <xsl:value-of select="ADDR_L2_TXT"/>
            </addr_l2_txt>
            <cty_nm>
                <xsl:value-of select="CTY_NM"/>
            </cty_nm>
            <prov_cd>
                <xsl:value-of select="PROV_CD"/>
            </prov_cd>
            <cntry_cd>
                <xsl:value-of select="CNTRY_CD"/>
            </cntry_cd>
            <pstl_cd>
                <xsl:value-of select="PSTL_CD"/>
            </pstl_cd>
        </RCPNT_ADDR>
        <bn>
            <xsl:value-of select="BN"/>
        </bn>
        <sbctrcr_amt>
            <xsl:value-of select="SBCTRCR_AMT"/>
        </sbctrcr_amt>
        <rpt_tcd>
            <xsl:value-of select="RPT_TCD"/>
        </rpt_tcd>
    </T5018Slip>
    <xsl:for-each select="Return/T5018Summary/slp_cnt">
        <xsl:value-of select="SLP_CNT='1'"/>
        <xsl:if test="slp_cnt='1'">
            <T5018Summary>
                <bn>
                    <xsl:value-of select="BN1"/>
                </bn>
                <PAYR_NM>
                    <l1_nm>
                        <xsl:value-of select="L1_NM"/>
                    </l1_nm>
                    <l2_nm>
                        <xsl:value-of select="L2_NM"/>
                    </l2_nm>
                    <l3_nm>
                        <xsl:value-of select="L3_NM"/>
                    </l3_nm>
                </PAYR_NM>
                <PAYR_ADDR>
                    <addr_l1_txt>
                        <xsl:value-of select="PAYRADDR_L1_TXT"/>
                    </addr_l1_txt>
                    <addr_l2_txt>
                        <xsl:value-of select="PAYRADDR_L2_TXT"/>
                    </addr_l2_txt>
                    <cty_nm>
                        <xsl:value-of select="PAYRCTY_NM"/>
                    </cty_nm>
                    <prov_cd>
                        <xsl:value-of select="PAYRPROV_CD"/>
                    </prov_cd>
                    <cntry_cd>
                        <xsl:value-of select="PAYRCNTRY_CD"/>
                    </cntry_cd>
                    <pstl_cd>
                        <xsl:value-of select="PAYRPSTL_CD"/>
                    </pstl_cd>
                </PAYR_ADDR>
                <CNTC>
                    <cntc_nm>
                        <xsl:value-of select="CNTC_NM"/>
                    </cntc_nm>
                    <cntc_area_cd>
                        <xsl:value-of select="CNTC_AREA_CD"/>
                    </cntc_area_cd>
                    <cntc_phn_nbr>
                        <xsl:value-of select="CNTC_PHN_NBR"/>
                    </cntc_phn_nbr>
                    <cntc_extn_nbr>
                        <xsl:value-of select="CNTC_EXTN_NBR"/>
                    </cntc_extn_nbr>
                </CNTC>
                <PRD_END_DT>
                    <dy>
                        <xsl:value-of select="DY"/>
                    </dy>
                    <mo>
                        <xsl:value-of select="MO"/>
                    </mo>
                    <yr>
                        <xsl:value-of select="YR"/>
                    </yr>
                </PRD_END_DT>
                <slp_cnt>
                    <xsl:value-of select="SLP_CNT"/>
                </slp_cnt>
                <tot_sbctrcr_amt>
                    <xsl:value-of select="TOT_SBCTRCR_AMT"/>
                </tot_sbctrcr_amt>
                <rpt_tcd>
                    <xsl:value-of select="RPT_TCD"/>
                </rpt_tcd>
            </T5018Summary>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

You can try the following XSLT.

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

    <xsl:template match="*[not(child::node())]"/>

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

The first template will get rid of any node that is empty.

Second template, lets the other node copy as it is.

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