[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] xsl:if test = empty string, returns true


Subject: Re: [xsl] xsl:if test = empty string, returns true
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Mon, 14 May 2012 15:52:31 +0100

You're not alone in finding this confusing. Some highly experienced people have given you incorrect information on this thread.

	<!-- Function to fetch value of a key-string pair in plist -->
	<xsl:function name="me:metadata">
		<xsl:param name="label"/>
		<xsl:value-of select="normalize-space($metadata/plist/dict/key[text()=$label]/following::node()[1]/text())"/>
	</xsl:function>

This function is NOT returning a string, and it is NOT returning a document node as some have suggested. It is returning a text node, because that is what<xsl:value-of> creates. Unlike xsl:variable, xsl:function does not wrap the text node in a document node; in the absence of an "as" attribute on xsl:function, it returns the text node unchanged.

Although I always recommend using an "as" attribute on xsl:function and xsl:param to make it clear what the inputs and output to the function are expected to be, the simplest fix to this function is to change<xsl:value-of> to<xsl:sequence>. It's a common mistake to use xsl:value-of when there is no intention or need to create a text node. The normal default way of returning a result from a function should be to use xsl:sequence.

>Any idea why the test evaluates to true?

The effective boolean value of a node is always true. In fact, Saxon can work out that this function will always return a text node, and it will probably therefore decide that the EBV of the function call is true without even calling the function.

Michael Kay
Saxonica


Current Thread