0

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.

RSG
  • 67
  • 5
  • 18
  • Try this: [Why does XSLT output all text by default?](https://stackoverflow.com/q/3360017/1015802) – groverboy Apr 19 '18 at 00:12
  • I tried adding in the xsl - a) it didn't give any output for the input xml file. b) when I tried to convert another xml file which has the 'task' elements then also it didn't give any output. It only gave an empty file in both instances. I need an output when converting a valid input file. please advise. – RSG Apr 19 '18 at 01:33

1 Answers1

2

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.

Joel M. Lamsen
  • 7,143
  • 1
  • 12
  • 14
  • Thanks for your answer Joel. This addition is not giving output for the given input file but when I try to use the xsl to convert another xml file which has 'task' elements in it, then also it is not generating any output. Please advise – RSG Apr 19 '18 at 04:43
  • because your XSL template specifically matches a root element named `Root`. If your XML has a root element named `task` then the output would be empty also. – Joel M. Lamsen Apr 19 '18 at 04:45
  • I am sorry to misinform you. When I tried with an xml file of root element as 'Order', and has child elements of 'cust' and 'task', then also I am getting no output. The above template you gave is for overriding the built in templates right ? please advise. Thank you. – RSG Apr 19 '18 at 05:01
  • your XSLT looks for a root element of `Root` not `Order`. To make it simple, any other input XML that has a root element other than `Root` will have an empty output. – Joel M. Lamsen Apr 19 '18 at 06:10
  • Thanks Joel. I will try that. – RSG Apr 19 '18 at 11:47