Wednesday, June 27, 2012

Element Not Null check in XSLT:

This basically includes two checks :
1. Element not present / existing.
2. Element existing and not empty.

Solution 1:

        <xsl:if test="not(ns1:VisitElement/ns1:visitSequence) or
string-length(ns1:VisitElement/ns1:visitSequence)=0">
            <xsl:value-of select="string('HELLO')"/>
        </xsl:if>

Explanation : In the above example visitSequence is checked for no existence (or) if it exists if its length is zero. In this case we are replacing the value with string called " HELLO"

Another way for doing the same is as below :

Solution 2: 


        <xsl:if test="not(ns1:VisitElement/ns1:visitSequence) or
                                 ns1:VisitElement/ns1:visitSequence[.!='']">
            <xsl:value-of select="string('HELLO')"/>
        </xsl:if>

The expression [.!='']  means that the current node (represented by dot) is not equal(!=) to an empty string('').