Created
September 15, 2013 17:14
-
-
Save ijy/6572634 to your computer and use it in GitHub Desktop.
XSLT: Check if a string is null or empty.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
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())]) | |
--> |
Great, Thanks a lot. Continue this way...
I am unable to test for empty string using my below code
<xsl:if test="Name != ''">
I am getting below error
One or more errors occurred. (String literal was not closed.
Name != -->'<-- )
Can you please suggest
<xsl:if test="number($Name) > 0"> this is ok
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi.
Thanks very much for this information:
* Contains no text content: not(string(.))
That helped me a lot in a code for which I wanted to run a for-each select, and in case nothing was selected I wanted to run a second for-each select. Now I've put a condition to the second and is only running in case the first one failed
Thanks again.