MsNichols
MsNichols

Reputation: 1133

XSLT/XPath test for 'Null'

I have the following which checks for a string within the xml... however, I need to create another test to check for 'Null' or no text within the variable $validItems at the 1st line...

<xsl:if test="$validItems[(Caption | CalltoAction)[string(.)]]">
 <div class="text">
   <xsl:choose>
     <xsl:when test="$horizontal">
       <div class="holder">
         <div class="frame">
           <div class="slides-descriptions">
             <xsl:apply-templates select="$validItems" mode="horizontal"/>
           </div>
           <div class="switcher"/>
         </div>
       </div>
     </xsl:when>
     <xsl:otherwise>
       <div class="slides-descriptions">
         <xsl:apply-templates select="$validItems" mode="vertical"/>
       </div>
       <div class="switcher"/>
     </xsl:otherwise>
   </xsl:choose>
 </div>
</xsl:if>

How would I go about testing the variable xsl:if test=$validItems?

Upvotes: 7

Views: 30028

Answers (3)

Ring
Ring

Reputation: 2309

None of these answers worked for me, I don't know if its because I'm using xpath via ODK xforms. I did get it working using this solution:

string-length(string(/path/to/node)) = 0

I have tested this on dates and numbers.

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163587

There is no such thing as "Null" in the XPath data model.

As for "no text within the variable $validItems" it would help to know the type of this variable. If it's a single element node (as might appear from the code sample you have shown), and what you want to test is that it has no text node children, the test would be not($ValidItems/text()). If you want to test that it has no text node descendants, try not(string($validItems)) or equivalently, $validItems=''.

Upvotes: 6

LarsH
LarsH

Reputation: 28004

If I understand what you're asking for,

<xsl:if test="$validItems[(Caption | CalltoAction)[not(string(.))]]">

will do it. In other words, "if there is an element in the $validItems node-set that has a Caption or CalltoAction child element whose string value is empty". You could also say

<xsl:if test="$validItems[(Caption | CalltoAction)[. = '']]">

Upvotes: 7

Related Questions