Search code examples
xmlxslttextoutputbuilt-in

XSLT: Leaving out unnecessary text


Using this built-in template:

<xsl:template match="text()|@*"/>

I'm trying to leave out all the text, which will not appear in the output XML elements.

It works for most of the code, but it doesn't work for this part:

<xsl:template match="NAD_01">
    <xsl:variable name="a" select="NAD_01_3035"/>
    <xsl:if test="$a='BY'">                                  
        <xsl:apply-templates mode="BY"/>  
    </xsl:if>
    <xsl:if test="$a='SE'">
        <xsl:apply-templates mode="SE"/>
    </xsl:if>
    <xsl:if test="$a='ST'">
        <xsl:apply-templates mode="ST"/>
    </xsl:if>
</xsl:template>

<xsl:template match="NAD_01_C080" mode="SE">
    <E1EDKA1>
        <PARVW>LF</PARVW>
        <xsl:if test="exists(NAD_01_3036_01)">                      
            <NAME1><xsl:value-of select="NAD_01_3036_01"/></NAME1>
        </xsl:if>
        <xsl:if test="exists(NAD_01_3036_02)">
            <NAME1><xsl:value-of select="NAD_01_3036_02"/></NAME1>
        </xsl:if>
        <xsl:if test="exists(NAD_01_3036_03)">
            <NAME1><xsl:value-of select="NAD_01_3036_03"/></NAME1>
        </xsl:if>
        <xsl:if test="exists(NAD_01_3036_04)">
            <NAME1><xsl:value-of select="NAD_01_3036_04"/></NAME1>
        </xsl:if>
    </E1EDKA1>
</xsl:template>

<xsl:template match="NAD_01_C082" mode="BY">              
    <EDIDC>
        <SNDRPRN><xsl:value-of select="NAD_01_3039"/></SNDRPRN>
    </EDIDC>
</xsl:template>  

<xsl:template match="NAD_01_C082" mode="SE">         
    <EDIDC>
        <RVNPRN><xsl:value-of select="NAD_01_3039"/></RVNPRN>
    </EDIDC>
</xsl:template>

<xsl:template match="NAD_01_C082" mode="ST">           
    <E1EDKA1>
        <PARVW>WE</PARVW>
        <PARTN><xsl:value-of select="NAD_01_3039"/></PARTN>
    </E1EDKA1>
</xsl:template>

Text in the input elements in the section "NAD_01" and in its childs (for example like "NAD_01_3039", but different ones, which don't have any output elements) stays in the output.

I have no idea where is the problem. Maybe with the variable, maybe with the mode links...

Please, how can I leave out all the unnecessary text even in this "NAD_01" section?

Thank you.


Solution

  • Make sure you add that template separately for any of the used modes:

    <xsl:template match="text()" mode="BY"/>
    <xsl:template match="text()" mode="SE"/>
    <xsl:template match="text()" mode="ST"/>