blika7
blika7

Reputation: 21

xslt: check if a variable is not blank

The code below validate an input (for i.e 12012016 as 12.01.2016),

<xsl:template name="handleDate">    
  <xsl:param name="input"/>   
  <xsl:value-of select="substring($input, 9, 2)" />
  <xsl:text>.</xsl:text><xsl:value-of select="substring($input, 6, 2)" />
  <xsl:text>.</xsl:text><xsl:value-of select="substring($input, 1, 4)" />
</xsl:template>

but whenever a date is blank (not filled), as a result I got only 2 dots(..) in the output, and when I try to import it in other system I got an error, because (..) is not a valid date. I tried this, but still doesn't work:

<xsl:template name="handleDate">
  <xsl:param name="input"/>
  <xsl:choose>
    <xsl:when test="input!= ''">
      <xsl:value-of select="substring($input, 9, 2)" />
      <xsl:text>.</xsl:text><xsl:value-of select="substring($input, 6, 2)" />
      <xsl:text>.</xsl:text><xsl:value-of select="substring($input, 1, 4)" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select=" '' " />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>             

<xsl:call-template name="handleDate">
  <xsl:with-param name="input" select="Field[@guid='123']/@xmlConvertedValue" />
</xsl:call-template>

Input is for i.e:

name;date;name2

and the output should be like:

test1;;test2 (if date is blank)

Upvotes: 2

Views: 1446

Answers (1)

Eir&#237;kr &#218;tlendi
Eir&#237;kr &#218;tlendi

Reputation: 1190

In your test expression, you forgot the $ prefix for variable and parameter names:

<xsl:when test="input!= ''">

As a result, input here is evaluated as an element name instead of the name of a variable or parameter. What you need instead is:

<xsl:when test="$input!= ''">

Try that and let us know how it goes.

Upvotes: 2

Related Questions