1

I'm finding that while this test returns true:

test="string-length(foo) = 0"

However, for some reason, both of these tests are returning false:

test="foo = ''"

test="foo = null"

Any idea what might be going on? Is there some other state that foo could be in that would result in a 0 length, while still not being equal to '' or null?

Additionally - if I output:

X<xsl:value-of select="foo" />X

outputs: XX

eejai42
  • 736
  • 2
  • 12
  • 24
  • 1
    See [http://stackoverflow.com/questions/825831/check-if-a-string-is-null-or-empty-in-xslt][1] [1]: http://stackoverflow.com/questions/825831/check-if-a-string-is-null-or-empty-in-xslt – Noam Rathaus Nov 20 '13 at 14:28
  • Thank you - I found that exact same article moments ago. Specifically, my misunderstanding was assuming that not(foo) and foo = null would return the same results and apparently that's not correct. If the element is absent, it is not equal to null. Instead, testing for a missing element must be tested for with not(foo). Thanks! – eejai42 Nov 20 '13 at 15:20
  • I guess there is not _null_ in XSLT, read Martin Honnen's answer. – Fernando Nov 20 '13 at 17:05

2 Answers2

2

If foo is an empty node-set then string(foo) is "", and string-length(foo) is zero, but foo = '' is false. XPath is full of surprises.

The explanation is that foo = "" doesn't mean string(foo) = "" as you might expect, but rather some $F in foo satisfies $F = "" (which you can write in full in XPath 2.0). And if foo is an empty set then the existential test is clearly false.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

Well if you do test="foo = null" you compare the foo child element(s) of the context node to the null child element(s) of the context node.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110