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

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

Subject: [xsl] xsl:if test = empty string, returns true
From: Jorge <chocolate.camera@xxxxxxxxx>
Date: Sun, 13 May 2012 22:03:57 +0200

According to http://www.w3.org/TR/xpath/#section-Boolean-Functions:

> The boolean function converts [] a string to true if and only if its length
is non-zero.

My XSLT 2.0 stylesheet has an xsl:if element testing a function that seems to
be returning a string of length zero, and yet the test evaluates to true. I am
processing the stylesheet with Saxon-HE with this command line:

> java -jar saxon/saxon9he.jar -it:main -xsl:./test.xsl

This is the stylesheet `test.xsl`:

> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="2.0"
>                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>                 xmlns:me="null">
> 	<xsl:variable name="PLIST" select="'./test.plist'"/>
> 	<!-- Load external plist into variable -->
> 	<xsl:variable name="metadata" select="document(iri-to-uri($PLIST))"/>
> 	<!-- Function to fetch value of a key-string pair in plist -->
> 	<xsl:function name="me:metadata">
> 		<xsl:param name="label"/>
> 		<xsl:value-of
> 	</xsl:function>
> 	<xsl:output method="text" encoding="UTF-8"/>
> 	<xsl:template name="main" exclude-result-prefixes="me">
> 		<xsl:choose>
> 			<xsl:when test="me:metadata('testkey')">
> testkey = "<xsl:value-of select="me:metadata('testkey')"/>"
> testkey's length = <xsl:value-of
> 			</xsl:when>
> 			<xsl:otherwise>
> 				"testkey" is empty or does not exist.
> 			</xsl:otherwise>
> 		</xsl:choose>
> 	</xsl:template>
> </xsl:stylesheet>

As you can see, the stylesheet imports a plist (XML) file, `test.plist`, into
a variable, and calls the function me:metadata() which returns the value of a
certain key in that plist as a string. The plist in question, which I quote
below, does contain the requested key, but its value is blank, and hence the
function should be returning a string of zero length (which is confirmed by
running the function again inside string-length()).

> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
> <plist version="1.0">
> <dict>
> 	<key>testkey</key>
> 	<string></string>
> </dict>
> </plist>

Any idea why the test evaluates to true?

Please note that I am not wondering whether the way I wrote the stylesheet is
the most appropriate way to do what it is supposed to do, but why the test is
evaluating to true.

Current Thread