XPath gotcha with empty()
While writing the XSLT stylesheet for the Bailey project XML data, I stumbled across a misleading 'gotcha' when using the XPath function empty(). Basically, empty() will ALWAYS return false as long as the element being tested exists. So, if your XML chunk looks like this:
<alias />
And your XPath looks like this (in this case, within XSLT):
<xsl:when test="empty(./alias)">
That test will always return false since <alias> exists. To test whether an element is empty, use the string() function. For example, if we want to determine whether <alias> is empty (which it is):
<xsl:when test="not(string(./alias))">
string() will return the string value of the element, which, in this case, is empty (but not empty() - hah).
P.S.: Martin reminded me that empty() is actually for sequences, which explains the weirdness when used with an element. But that, of course, leads one to wonder why empty() doesn't throw an error when used with a non-sequence...