RSG
RSG

Reputation: 77

xsl is generating output even when no element is matching

An xsl to convert an xml file is generating output even when no input elements are satisfying the condtiton. the xsl file is as below

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8"/>
    <xsl:template match="Root/Order">
        <xsl:choose>
            <xsl:when test="Order1/task or Order2/task or task">
                <xsl:value-of select="ID"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/custId"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/System"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/Number"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Number"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Status"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Indi"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Code"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="tasks/lno"/><xsl:text>|</xsl:text>
                <xsl:value-of select="tasks/val"/><xsl:text>&#xa;</xsl:text>
            </xsl:when>
            <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
        <xsl:for-each select="task">
                <xsl:value-of select="ID"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/custId"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/System"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/Number"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Number"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Status"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Indi"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Code"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="tasks/lno"/><xsl:text>|</xsl:text>
                <xsl:value-of select="tasks/val"/><xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
        <xsl:for-each select="Order1">
            <xsl:for-each select="task">
                <xsl:value-of select="ID"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/custId"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/System"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/Number"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Number"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Status"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Indi"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Code"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="tasks/lno"/><xsl:text>|</xsl:text>
                <xsl:value-of select="tasks/val"/><xsl:text>&#xa;</xsl:text>
            </xsl:for-each>
        </xsl:for-each>
        <xsl:for-each select="Order2">
            <xsl:for-each select="task">
                <xsl:value-of select="ID"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/custId"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/System"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="cust/Number"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Number"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Status"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Indi"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="make/Code"/><xsl:text>|</xsl:text> 
                <xsl:value-of select="tasks/lno"/><xsl:text>|</xsl:text>
                <xsl:value-of select="tasks/val"/><xsl:text>&#xa;</xsl:text>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Input xml file is as below.

<cust>
        <Date>2018-04-16</Date>
        <name>abc</name>
        <code>xyz10</code>
        <custId>abc123</custId>
        <System>main</System>
        <Number>TANK</Number>
    </cust>

My understanding is as the cust is not matching the defined template match or when condition, there should no be an output. But when I do the conversion output is as below.

2018-04-16
abc
xyz10
abc123
main
TANK

I believe as there are no defined template rules for the input, built in template rules are called during processing and they are generating output. I am not certain whether that is the correct reason or not. request you for an answer. thanks in advance.

Upvotes: 0

Views: 109

Answers (1)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

Add this template:

<xsl:template match="/">
    <xsl:apply-templates select="Root"/>
</xsl:template>

When this is run against your input XML file above, you will get no output.

Upvotes: 2

Related Questions