Skip to content

Instantly share code, notes, and snippets.

@greystate
Created February 8, 2011 23:32
Show Gist options
  • Save greystate/817527 to your computer and use it in GitHub Desktop.
Save greystate/817527 to your computer and use it in GitHub Desktop.
One-liner to assign a variable one of two values, dependent on condition (XSLT 1.0, that is)
<!-- Selects 'YES' if CONDITION is true() -->
<xsl:value-of select="substring('YES|NO', not(CONDITION) * 4 + 1, 3)" />
<!-- Selects 'NO' if CONDITION is true() -->
<xsl:value-of select="substring('YES|NO', boolean(CONDITION) * 4 + 1, 3)" />
<!--
Notes:
* Need both versions because the longer value must be the first of the two
* 4 is the index of the pipe (|) character (or the string-length of the first value plus one)
* 3 is the string-length of the first value
Credit goes to Jeni Tennison - I saw this used a couple of times in her excellent book "XSLT and XPath on the Edge".
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment