🔊 I'm running ffconf: UK's best JS & webdev conf. Find out more & get tickets 📢

How to default a variable in XSLT

(edit)

Since I couldn't find this anywhere on the web, and I'm working on a project that has had me very quickly learn XSLT, here's how to default a value in XSLT - useful if you're looking to grab a variable via the query string, and it may not be there in the first place.

<xsl:variable name="show_comments">
  <xsl:choose>
    <xsl:when test="//QUERY_STRING/show_comments"><xsl:value-of select="//QUERY_STRING/show_comments"/></xsl:when>
    <xsl:otherwise>0</xsl:otherwise> <!-- default value -->
  </xsl:choose>
</xsl:variable>

Note: //QUERY_STRING is a made up variable

I know XSLT might be a bit random for me, but client wants: client gets :-)

Posted 15-Aug 2008 under code & code & xml & xslt.

I am Remy Sharp

I'm a JavaScript developer working professionally on the web since 1999. I run my own consultancy, build products, run training, speak at conferences and curate the UK's best JavaScript event.

Subscribe

Posts, web tech insights, exclusive workshop and training discounts and more, direct to your inbox.

Comments