Uwe Allner
Uwe Allner

Reputation: 3467

Why is this choose-when-test always true?

In an XSLT file I have defined a function

<xsl:function name="my:isValidDate">
    <xsl:param name="orgDate" />

    <xsl:value-of select="boolean(string-length($orgDate)=10)" />
</xsl:function>

to test whether a date string has a valid length. When I use this function

        <xsl:choose>
            <xsl:when test="my:isValidDate(ValidFrom)">
                <xsl:call-template name="formatDate"><xsl:with-param name="orgDate" select="ValidFrom"></xsl:with-param></xsl:call-template>
            </xsl:when>
            ...
        </xsl:choose>

it seems to always be true; even empty tags like <ValidFrom /> pass the test and result in strange outputs, as the template must not be called on empty or invalid date strings. When I output ValidFrom for debugging issues, it is empty as expected. I also tried variants like string-length($orgDate)=10 or <xsl:value-of select="boolean($orgDate!='')" /> in the function, but nothing seems to work. What am I missing?

EDIT: This condition is correctly evaluated, when I inline the function?!

Upvotes: 0

Views: 275

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

Basically <xsl:value-of select="boolean(string-length($orgDate)=10)" /> creates a text node with a string of the boolean value, to return a boolean value use <xsl:sequence select="string-length($orgDate)=10" />.

Upvotes: 2

Related Questions