if

Usage

if expression command if expression command-or-block [ elsif command-or-block ]* [ else command-or-block ]

Description

Executes command-or-block if a given expression expression evaluates to a non-emtpty node-list, true boolean-value, non-zero number or non-empty literal. If the first expression fails, then elsif conditions are tested (if any) and the command-or-block corresponding to the first one of them which is true is executed. If none of the conditions is satisfied, an optional else command-or-block is executed.

Example 34. Display node type

def node_type %n {
  foreach (%n) {
    if ( . = self::* ) { # XPath trick to check if . is an element
      echo 'element';
    } elsif ( . = ../@* ) { # XPath trick to check if . is an attribute
      echo 'attribute';
    } elsif ( . = ../processing-instruction() ) {
      echo 'pi';
    } elsif ( . = ../text() ) {
      echo 'text';
    } elsif ( . = ../comment() ) {
      echo 'comment'
    } else { # well, this should not happen, but anyway, ...
      echo 'unknown-type';
    }
  }
}

Example 35. Check a environment variable

if { defined($ENV{HOME}) } lcd { $ENV{HOME} }