0
我無法使用XSLT從我的XML中刪除空節點。例如,這個地區在XSLT:使用XSLT從XML中刪除空節點
<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>
裏面的一些這些節點的數據完全是空的,但我還是把他們送回去。我試着將match="node()|@*"
模板區域應用到xslt,但是這樣做也沒用。這裏是整個XSLT文件:
<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>
http://stackoverflow.com/questions/11312195/ignoring-empty-elements-with-xsl – Ravish
@Ravish,我必須爲每個節點做到這一點嗎?沒有辦法將某些東西應用於整個轉換嗎? – DDiVita
你應該提供你的輸入文件的樣本。 –