xsl-list
[Top] [All Lists]

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

2012-05-13 15:04:12
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 9.2.1.2J 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 
select="normalize-space($metadata/plist/dict/key[text()=$label]/following::node()[1]/text())"/>
      </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 
select="string-length(me:metadata('testkey'))"/>
                      </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" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd";>
<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.
--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--