Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string is null or empty in XSLT

How can I check if a value is null or empty with XSL?

For example, if categoryName is empty? I'm using a when choosing construct.

For example:

<xsl:choose>     <xsl:when test="categoryName !=null">         <xsl:value-of select="categoryName " />     </xsl:when>     <xsl:otherwise>         <xsl:value-of select="other" />     </xsl:otherwise> </xsl:choose> 
like image 722
raklos Avatar asked May 05 '09 16:05

raklos


People also ask

How to check not empty in XSLT?

How can I check if a value is null or empty with XSL? For example, if categoryName is empty? Explanation: The argument to the not() function above is false() exactly when there is no categoryName child ("null") of the context item, or the (single such) categoryName child has string value -- the empty string.

What is normalize space in XSLT?

The normalize-space() function It does three things: It removes all leading spaces. It removes all trailing spaces. It replaces any group of consecutive whitespace characters with a single space.

How do you find the length of a string in XSLT?

Name. string-length() Function — Returns the number of characters in the string passed in as the argument to this function. If no argument is specified, the context node is converted to a string and the length of that string is returned.

How do I concatenate in XSLT?

Concat function in XSLT helps to concatenate two strings into single strings. The function Concat() takes any arguments that are not the string is been converted to strings. The cardinality function passed over here in each argument should satisfy zero or one function() respectively.


1 Answers

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.

like image 191
steamer25 Avatar answered Sep 30 '22 15:09

steamer25