Check if a string is null or empty in XSLT
Absent of any other information, I'll assume the following XML:
<group>
<item>
<id>item 1</id>
<CategoryName>blue</CategoryName>
</item>
<item>
<id>item 2</id>
<CategoryName></CategoryName>
</item>
<item>
<id>item 3</id>
</item>
...
</group>
A sample use case would look like:
<xsl:for-each select="/group/item">
<xsl:if test="CategoryName">
<!-- will be instantiated for item #1 and item #2 -->
</xsl:if>
<xsl:if test="not(CategoryName)">
<!-- will be instantiated for item #3 -->
</xsl:if>
<xsl:if test="CategoryName != ''">
<!-- will be instantiated for item #1 -->
</xsl:if>
<xsl:if test="CategoryName = ''">
<!-- will be instantiated for item #2 -->
</xsl:if>
</xsl:for-each>
From Empty Element:
To test if the value of a certain node is empty
It depends on what you mean by empty.
- Contains no child nodes:
not(node())
- Contains no text content:
not(string(.))
- Contains no text other than whitespace:
not(normalize-space(.))
- Contains nothing except comments:
not(node()[not(self::comment())])
test="categoryName != ''"
Edit: This covers the most likely interpretation, in my opinion, of "[not] null or empty" as inferred from the question, including it's pseudo-code and my own early experience with XSLT. I.e., "What is the equivalent of the following Java?":
// Equivalent Java, NOT XSLT
!(categoryName == null || categoryName.equals(""))
For more details e.g., distinctly identifying null vs. empty, see johnvey's answer below and/or the XSLT 'fiddle' I've adapted from that answer, which includes the option in Michael Kay's comment as well as the sixth possible interpretation.