9. Macro commands

macro -> macro_name_NCName '(' [ parameters ] ')' '{' block_contents '}'

parameters -> parameter_name_NCName [ ',' parameter_name_NCName ]*

block_contents -> [ command | if | foreach ]*

A macro command is simply a user-defined command. It is invoked by its name just like predefined commands. It can be passed arguments just like predefined commands. Beside its parameters which act like local variables, it can have other local variables declared by the means of special command variable. Like any other command, a macro is executed in the context of the current context node[1].

A macro command is local to the script file containing it. See include to learn how the same macro command may be shared between several script files.

Examples:

macro unstyle-heading(heading) {
    variable("text", string(.));

    for-each $heading//*[(self::b or
                          self::big or
                          self::cite or
                          self::dfn or
                          self::em or
                          self::i or
                          self::q or
                          self::s or
                          self::small or
                          self::strong or
                          self::tt or
                          self::u) and 
                          string(.) = $text and 
                          not(@id)] {
        unwrap-element();
    }
}

macro heading-to-bridgehead() {
    set-attribute("class",
                  concat("bridgehead", substring-after(local-name(), "h")));
    set-element-name("p");
}


[1] The context node of the script or the context node of the body of a for-each.