Check if a parameter exists in XSLT before using it

JustAnotherDeveloper picture JustAnotherDeveloper · May 14, 2012 · Viewed 9.2k times · Source

I am using XPATH to work out if my parameter $test exists. I am using the XPATH builder in ALtova Stylevision and thought I could use something like exists ( $blah) or exists ( string($blah) ) But this ends with

" Undefined variable in expression test="exists( string( $blah) )""

The issue is that sometimes the parameter will exist, and sometimes it won't. I don't want to have to hardcode in a blank parameter!

Is there a way I can say "Check if the parameter XXXX exists" before I use it? It's frustrating as I know how to do this in pretty much every other programming language

Parameter defined:

<xsl:param name="blah"> some text </xsl:param>

These parameters are read in from a seperate file "parameters.xslt". This file may or may not exist, depending on the scenario. I need to check the param actually exists before I attempt to use it.

Answer

Buck Doyle picture Buck Doyle · May 14, 2012

If you’ve declared the parameter, it exists. What you want to know is, does it have a value?

<xsl:choose>
  <xsl:when test="not($blah)">$blah wasn’t passed in</xsl:when>
  <xsl:otherwise>$blah was passed in</xsl:otherwise>
</xsl:choose>