Skip to content

Instantly share code, notes, and snippets.

@Kurukshetran
Forked from ijy/check-null-empty.xsl
Created January 4, 2022 13:15
Show Gist options
  • Save Kurukshetran/e38719f8f3efd8704faba924b5039d76 to your computer and use it in GitHub Desktop.
Save Kurukshetran/e38719f8f3efd8704faba924b5039d76 to your computer and use it in GitHub Desktop.
XSLT: Check if a string is null or empty.
<!--
CHECK IF A STRING IS NULL OR EMPTY
-->
<!-- Example 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>
<!-- Example test case -->
<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>
<!--
To test if the value of a certain node is empty it depends on what is meant 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())])
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment