1

I'm trying to determine if ../../coupon_code is null or empty. I've tried the methods in this thread Check if a string is null or empty in XSLT to no avail. Maybe I'm doing something wrong?

<!--Coupon Code Name and Code-->
<xsl:choose>
    <xsl:when test="not(../../coupon_code)">
        <xsl:if test="../../coupon_code != ''"> 
            <xsl:value-of select="../../coupon_rule_name" /> <xsl:value-of select="../../coupon_code" /><xsl:value-of select="$sepend" />D.PROMOTION<xsl:value-of select="$sepend" />
        </xsl:if> 
    </xsl:when>
    <xsl:otherwise>
    </xsl:otherwise>
</xsl:choose>
<!--End Coupon Code Name and Code-->

I'm doing

<xsl:when test="not(../../coupon_code)">

To determine if it's null. Then, I'm doing

<xsl:if test="../../coupon_code != ''"> 

To determine if it's empty.

However, I'm looking at data where this is clearly populated, and it's not entering the when/if to display the data at all. So it's failing somewhere and I don't know where.

Sometimes, ../../coupon_code will have a coupon code in it, like COUPON122. Sometimes it won't have anything in it.

James
  • 529
  • 1
  • 7
  • 16
  • 1
    Please show a minimal but complete and representative input sample together with the result you want and the one you get. Without any XML input sample and a context for that XSLT snippet we can't tell whether the code makes any sense. In any case, if you check `not(../../coupon_code)` you check that the `coupon_code` element doesn't exist so I don't understand how you expect to enter the `xsl:if` to compare the non-existing element to something. – Martin Honnen Aug 04 '20 at 21:49
  • @MartinHonnen Sometimes, `../../coupon_code` will have a coupon code in it, like COUPON122. Sometimes it won't have anything in it. – James Aug 04 '20 at 21:51
  • 1
    As I said, without context we can't judge that snippet, other than pointing out that `test="not(../../coupon_code)"` checks that the `coupon_code` doesn't exist at all. If the input always has that element then your check means you get the `xsl:otherwise` which is empty executed. – Martin Honnen Aug 04 '20 at 21:54

1 Answers1

1

You can check

<xsl:if test="normalize-space(../../coupon_code)">
  <xsl:value-of select="../../coupon_rule_name" /> <xsl:value-of select="../../coupon_code" /><xsl:value-of select="$sepend" />D.PROMOTION<xsl:value-of select="$sepend" />
</xsl:if>

to have the xsl:value-ofs processed only if the ../../coupon_code element has some non-whitespace content.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110