if

Usage

if xpath|perl-code command

if xpath|perl-code command-block [ elsif command-block ]* [ else command-block ]

Description

Execute command-block if a given xpath or perl-code expression evaluates to a non-emtpty node-list, true boolean-value, non-zero number or non-empty literal. If the first test fails, check all possibly following elsif conditions and execute the corresponding command-block for the first one of them which is true. If none of them succeeds, execute the else command-block (if any).

Example 18. 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';
    }
  }
}