Check whether the string in the XSLT is empty or empty

Keywords: xml Programming

How to use XSL Is the check value empty or empty?

For example, if the categoryName is empty? What I use when selecting constructs is.

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>

#1 building

The first two handle null values, and the second handle empty strings.

<xsl:if test="USER/FIRSTNAME">
    USERNAME is not null
</xsl:if>
<xsl:if test="not(USER/FIRSTNAME)">
    USERNAME is null
 </xsl:if>
 <xsl:if test="USER/FIRSTNAME=''">
     USERNAME is empty string
 </xsl:if>
 <xsl:if test="USER/FIRSTNAME!=''">
     USERNAME is not empty string
 </xsl:if>

#2 building

If the element is possible in XML, I will test whether the element exists and whether the string length is greater than zero:

<xsl:choose>
    <xsl:when test="categoryName and string-length(categoryName) &gt; 0">
        <xsl:value-of select="categoryName " />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="other" />
    </xsl:otherwise>
</xsl:choose>

#3 building

In my experience, the best way is to:

<xsl:when test="not(string(categoryName))">
    <xsl:value-of select="other" />
</xsl:when>
<otherwise>
    <xsl:value-of select="categoryName" />
</otherwise>

#4 building

I know it's an old question, but between all the answers, I miss a common approach to this use case in the development of XSLT.

I imagine the missing code in OP looks like this:

<xsl:template match="category">
    <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>
</category>

And the input looks like this:

<categories>
    <category>
       <categoryName>Books</categoryName>
    </category>
    <category>
       <categoryName>Magazines</categoryName>
       <categoryName>Periodicals</categoryName>
       <categoryName>Journals</categoryName>
    </category>
    <category>
        <categoryName><!-- please fill in category --></categoryName>
    </category>
    <category>
        <categoryName />
    </category>
    <category />
</categories>

That is, I assume that there can be zero, empty, single or multiple categoryName elements. Using the xsl:choose -style construct to handle all of these situations, or in other words, command wise, can quickly become confusing (especially if the elements can be at different levels!). A typical programming habit in XSLT is to use templates (and therefore T in XSLT), which is declarative rather than imperative (you don'T tell the processor what to do, you tell you what you want to output as long as certain conditions are met). For this use case, it might look like this:

<!-- positive test, any category with a valid categoryName -->
<xsl:template match="category[categoryName[text()]]">
    <xsl:apply-templates />
</xsl:template>

<!-- any other category (without categoryName, "null", with comments etc) -->
<xsl:template match="category">
    <xsl:text>Category: Other</xsl:text>
</xsl:template>

<!-- matching the categoryName itself for easy handling of multiple names -->
<xsl:template match="categoryName">
    <xsl:text>Category: </xsl:text>
    <xsl:value-of select="." />
</xsl:template>

This applies (using any version of XSLT) because the first one above has a higher priority (it has a predicate). The second pass through match template captures any invalid match templates. The third is then responsible for outputting the categoryName value in an appropriate way.

Note that in this case, there is no need to specifically match categories or categories, because the processor will automatically process all children unless we explain otherwise (in this example, the second and third templates do not further process children because there is no xsl: apply templates in).

This method is easier to extend than the imperative method because it automatically handles multiple categories and can extend other elements or exceptions by adding another matching template. There is no programming for if branches.

Note: there is no such thing as null in XML. Yes xsi: nil , but it is rarely used, especially in the untyped scene without a certain pattern.

#5 building

If the node does not have a value available in the input xml, such as under xpath,

<node>
    <ErrorCode/>
</node>

The string() function is converted to a null value. So it works:

string(/Node/ErrorCode) =''

Posted by khaldryck on Tue, 17 Mar 2020 06:02:16 -0700